Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Populating a Mysql database through a drop down box using php

Status
Not open for further replies.

nichols

Technical User
May 24, 2001
92
GB
I am a novice in php and I am trying to populate a mysql table using a drop down box which is taking data from another mysql table. Below is my php script.

It actually works for the cat_name which is just a simple text post. When I select the category it selects it but just does not post in in the mysql database.

below is my script, you will have to excuse my code as I am only just starting using php.

<?php



if (isset($_POST['submit'])) { // Handle the form.

$message = NULL; // Create an empty new variable.




// Check for a Supplier name.
if (empty($_POST['supplier_name'])) {
$sn = FALSE;
$message .= '<p>You forgot to enter your Supplier name!</p>';
} else {
$sn = $_POST['supplier_name'];
}

// Check for a Category.




if ($sn ) { // If everything's OK.

// Register the user in the database.
require_once ('mysql_connect.php'); // Connect to the db.

// Make the query.
$query = "INSERT INTO suppliers (supplier_name) VALUES ('$sn')";
$result = @mysql_query ($query); // Run the query.
if ($result) { // If it ran OK.

// Send an email, if desired.
echo '<p><b>You have been registered!</b></p>';
exit(); // Quit the script.

} else { // If it did not run OK.
$message = '<p>You could not be registered due to a system error. We apologize for any inconvenience.</p><p>' . mysql_error() . '</p>';
}

mysql_close(); // Close the database connection.

} else {
$message .= '<p>Please try again.</p>';
}

} // End of the main Submit conditional.

// Print the message if there is one.
if (isset($message)) {
echo '<font color="red">', $message, '</font>';
}
?>

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<fieldset><legend>Enter your information in the form below:</legend>

<p><b>Supplier Name:</b> <input type="text" name="supplier_name" size="30" maxlength="30" value="<?php if (isset($_POST['supplier_name'])) echo $_POST['supplier_name']; ?>" /></p>

<?php
// Register the user in the database.
require_once ('mysql_connect.php'); // Connect to the db.


$query2 = mysql_query("SELECT cat_name FROM category");?>
<select name=cat_name>
<?php while ($r = mysql_fetch_array($query2))
{
$catname = $r["cat_name"];
echo "<option value=$catname>$catname</option>";
}
echo "</select>";

?>


</fieldset>


<div align="center"><input type="submit" name="submit" value="Register" /></div>

</form><!-- End of Form -->



Also does anyone know the php alternative to the asp data grid as i want to output some of my database data into a table on in the middle of the page.

Hope you guys can shed some light on this one
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top