...

View Full Version : Adding to a database



briintex1
06-01-2004, 08:16 PM
I was wondering where I might have gone wrong on adding information to a database, the country_id is an autonumber, so I don't have to add it. But when a user puts in a name of the country, it adds it to the database, once they click the submit button, and it returns them back to this page. It passes the variable through the url I think but I am not for sure about that one either? But any suggestions are helpful...



<?php
$db = mysql_connect("localhost", "tableName", "PW")or die(mysql_error());

mysql_select_db("database")or die(mysql_error());

$obj = mysql_query("SELECT * FROM country");
$obj=mysql_fetch_array($obj);

INSERT INTO COUNTRY (country_id, country_name) VALUES($country) ?>

}

echo '</select>';
}
?>
<html>
<head
</head>
<title><? echo $obj[1] ?></title>

<FORM ACTION="index.php" METHOD=POST>
Please enter the name of the country to put into the database.
<p>TITLE:<INPUT TYPE="text" NAME="country" SIZE=25 MAXLENGTH=25>
<p><INPUT TYPE="submit">
</FORM>

<?php echo $country ?>
<body>
</body>
</html>

MrShed
06-02-2004, 10:55 PM
INSERT INTO COUNTRY (country_id, country_name) VALUES($country) ?>

}




I might just be being dumb here, but any particular reason why this line isn't in a mysql_query() function?

boeing747fp
06-03-2004, 01:13 AM
$db = mysql_connect("localhost", "tableName", "PW")or die(mysql_error());

try this......




<?php
$db = mysql_connect("localhost", "ServerUserName", "ServerPassWord");
mysql_select_db("DatabaseName",$db);

$select = mysql_query("SELECT * FROM `country`");
$obj=mysql_fetch_array($select);

mysql_query("INSERT INTO `country` (`country_id`, `country_name`) VALUES ('','$country')") or die(mysql_error());

}

echo '</select>';
}
?>
<html>
<head>
</head>
<title><? echo $obj[1]; ?></title>

<FORM ACTION="index.php" METHOD=POST>
Please enter the name of the country to put into the database.
<p>TITLE:<INPUT TYPE="text" NAME="country" SIZE=25 MAXLENGTH=25>
<p><INPUT TYPE="submit">
</FORM>

<?php echo $country; ?>
<body>
</body>
</html>



One question, tho..... where did u set $country ? you should have somewhere above the INSERT thing a line like $country = $_POST['country'];



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum