Go Back   CodingForums.com > :: Server side development > PHP

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 08-16-2012, 08:43 AM   PM User | #1
coatse
New to the CF scene

 
Join Date: Aug 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
coatse is an unknown quantity at this point
Post PHP Form Listbox not insert into mysql

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 Code:
<?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

Last edited by coatse; 08-17-2012 at 05:19 AM.. Reason: Is Resolved
coatse is offline   Reply With Quote
Old 08-16-2012, 03:01 PM   PM User | #2
sunfighter
Senior Coder

 
Join Date: Jan 2011
Location: Missouri
Posts: 2,364
Thanks: 18
Thanked 348 Times in 347 Posts
sunfighter is on a distinguished road
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/
sunfighter is offline   Reply With Quote
Old 08-17-2012, 05:18 AM   PM User | #3
coatse
New to the CF scene

 
Join Date: Aug 2012
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
coatse is an unknown quantity at this point
Smile Thanks For you support

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 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['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 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
coatse is offline   Reply With Quote
Reply

Bookmarks

Tags
listbox, mysql, php, populate

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 02:25 AM.


Advertisement
Log in to turn off these ads.