...

View Full Version : Resolved PHP Form Listbox not insert into mysql



coatse
08-16-2012, 09:43 AM
Hi I am new to this thread, but i have been using php for sometime now.

This is the first time i have tried to fetch data from a mysql table and populate a listbox with it.

When i click submit the form submits and says success full as it should but the listbox data is missing.

my code is below

HTML FORM

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<form action="addrest1.php" method="POST">
<table width="264" border="0" cellspacing="2">
<tr>
<td width="180">&nbsp;</td>
<td width="10">&nbsp;</td>
<td width="62">&nbsp;</td>
</tr>
<tr>
<td><label>Resturant Name: </label>&nbsp;
<label for="name"></label>
<input type="text" name="name" id="name"></td>
<td>&nbsp;</td>
<td><input type="submit" name="first5" id="first5" value="ADD"></td>
</tr>
<tr>
<td>Location:
<input type="text" name="location" id="location"></td>
<td>&nbsp;</td>
<td><input type="button" name="first6" id="first6" value="UPDATE"></td>
</tr>
<tr>
<td>Address:
<input type="text" name="address" id="address"></td>
<td>&nbsp;</td>
<td><input type="button" name="first7" id="first7" value="DELETE"></td>
</tr>
<tr>
<td>Email:
<input type="text" name="email" id="email"></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>Telephone:
<input type="text" name="telephone" id="telephone"></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>Cuisine Type:
<?php
mysql_connect("localhost", "root", "coatez") or die(mysql_error());
mysql_select_db("restaurant") or die(mysql_error());
$query="SELECT type FROM cuisine_type";
$result = mysql_query($query);

echo '<select name="cuisine_id" id="cuisine_id">';
while($nt=mysql_fetch_array($result)){ //Array or records stored in $nt
echo '<option name="select" value="' . $nt['cuisine_id'] . '">' . $nt['type'] . '</option>';
/* Option values are added by looping through the array */
}
echo '</select>'; // Closing of list box
?>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>Signature Dish:
<input type="text" name="signature_dish" id="signature_dish"></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>Price Range:
<input type="text" name="price_range" id="price_range"></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td>Payment Type:
<input type="text" name="payment_type" id="payment_type"></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
<tr>
<td><input type="button" name="first" id="first" value="Prev">
<input type="button" name="first2" id="first2" value="Next"></td>
<td>&nbsp;</td>
<td>&nbsp;</td>
</tr>
</table>
</form>
</body>
</html>

addrest1.php



<?php

$host="localhost"; // Host name
$username="******"; // Mysql username
$password="******"; // Mysql password
$db_name="restaurant"; // Database name
$tbl_name="restaurant"; // Table name

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect");
mysql_select_db("$db_name")or die("cannot select DB");

// Get values from form
$name=$_POST["name"];
$location=$_POST["location"];
$address=$_POST["address"];
$email=$_POST["email"];
$telephone=$_POST["telephone"];
$cuisine_id=$_POST["cuisine_id"];
$signature_dish=$_POST["signature_dish"];
$price_range=$_POST["price_range"];
$payment_type=$_POST["payment_type"];

// Insert data into mysql
$sql="INSERT INTO $tbl_name(name, location, address, email, telephone, cuisine_id, signature_dish, price_range, payment_type)VALUES('$name', '$location', '$address', '$email', '$telephone', '$cuisine_id', '$signature_dish', '$price_range', '$payment_type')";
$result=mysql_query($sql);

// if successfully insert data into database, displays message "Successful".
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='insert.php'>Back to main page</a>";
}

else {
echo "ERROR";
}
?>

<?php
// close connection
mysql_close();
?>


as you can see i just need the listbox to work now

Thanks in Advanced:thumbsup::)

sunfighter
08-16-2012, 04:01 PM
Normally a single drop down can and should be hard coded to save loading time. It's when you need to populate a second drop down based on the first selection that you need a database for options. But look at this for how to do it. http://css-tricks.com/dynamic-dropdowns/

coatse
08-17-2012, 06:18 AM
I appreciate your support, As i have fixed the error of my insert error.

For anyone that wants to now, make sure when your making a relation between two tables that you don't insert into a id field and make sure the code is in the correct format.


<?php
mysql_connect("localhost", "root", "coatez") or die(mysql_error());
mysql_select_db("restaurant") or die(mysql_error());
$query="SELECT type FROM cuisine_type";
$result = mysql_query($query);

echo '<select name="cuisine_id" id="cuisine_id">';
while($nt=mysql_fetch_array($result)){ //Array or records stored in $nt
echo '<option name="select" value="' . $nt['cuisine_id'] . '">' . $nt['type'] .
//The line above is wrong
'</option>';
/* Option values are added by looping through the array */
}
echo '</select>'; // Closing of list box
?>

This script was looking for the table field cuisne_id

this is the correct code


<?php
mysql_connect("localhost", "root", "coatez") or die(mysql_error());
mysql_select_db("restaurant") or die(mysql_error());
$query="SELECT type FROM cuisine_type";
$result = mysql_query($query);

echo '<select name="cuisine_id" id="cuisine_id">';
while($nt=mysql_fetch_array($result)){ //Array or records stored in $nt
echo '<option name="select" value="' . $nt['type'] . '">' . $nt['type'] . '</option>';
/* Option values are added by looping through the array */
}
echo '</select>'; // Closing of list box
?>

Thanks for your help:thumbsup::)



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum