Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Reply
 
Thread Tools Search this Thread
  #1  
Old 30-10-2009
Member
 
Join Date: Oct 2009
Posts: 1
php/mysql dropdown values are empty

Hi, I have written the following code (yes, I am a newbie) in php. The form displays ok, but the dropdown values in the select field are empty. Any help is much appreciated. fyi, the 'state' table simply has all US state abbreviations

<html>
<style type="text/css">
<!--
.state {
width: 50px;
}
-->
</style>
<link href="quote.css" rel="stylesheet" type="text/css">
<body>
<?php
if (isset ($_POST['submit']))
{

$usr = "root";
$pwd = "";
$db = "quote";
$host = "localhost";

$con = mysql_connect($host,$usr,$pwd);

if (!$con)
{
die('Could not connect: ' . mysql_error());
}



$SQL = " INSERT INTO info ";
$SQL = $SQL . " (city, state) VALUES ";
$SQL = $SQL . " ('$_POST[city]', '$_POST[state]') ";

if (!mysql_db_query($db,"$SQL",$con))
{
die('Error: ' . mysql_error());
}

echo "1 record added";
mysql_close($con);

}
else
{
?>

<form method="POST" action="';
echo htmlentities($_SERVER['$PHP_SELF']);
echo '" enctype="multipart/form-data">
City:<input type="text" size="15" maxlength="30" name="city">:<br />
<select name="state">
<?php

$con = mysql_connect("localhost","root","");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}

mysql_select_db("quote", $con);

$result = mysql_query("SELECT state FROM states");

foreach($result as $row)
{

echo "<option value=\"" .$row['state']. "</option>\n";

}

?>
</select>

<br /><input type="submit" name="submit" value="Submit!">
</form>
<?php
}

?>

</body>
<html>
Reply With Quote
  #2  
Old 30-10-2009
Member
 
Join Date: May 2008
Posts: 685
Re: php/mysql dropdown values are empty

PHP Code:
<html>
<style type="text/css">
<!--
.state {
width: 50px;
}
-->
</style>
<link href="quote.css" rel="stylesheet" type="text/css">
<body>
<?php
if (isset ($_POST['submit']))
{

$usr "root";
$pwd "";
$db "quote";
$host "localhost";

$con mysql_connect($host,$usr,$pwd);

if (!
$con)
{
die(
'Could not connect: ' mysql_error());
}



$SQL " INSERT INTO info ";
$SQL $SQL " (city, state) VALUES ";
$SQL $SQL " ('$_POST[city]', '$_POST[state]') ";

if (!
mysql_db_query($db,"$SQL",$con))
{
die(
'Error: ' mysql_error());
}

echo 
"1 record added";
mysql_close($con);

}
else
{
?>

<form method="POST" action="';
echo htmlentities($_SERVER['$PHP_SELF']);
echo '" enctype="multipart/form-data">
City:<input type="text" size="15" maxlength="30" name="city">:<br />
<select name="state">
<?php

$con 
mysql_connect("localhost","root","");
if (!
$con)
{
die(
'Could not connect: ' mysql_error());
}

mysql_select_db("quote"$con);

$result mysql_query("SELECT state FROM states");

foreach(
$result as $row)
{

echo 
"<option value=\"" .$row['state']. "</option>\n";

}

?>
</select>

<br /><input type="submit" name="submit" value="Submit!">
</form>
<?php
}

?>

</body>
<html>
First of all, always use quotes (present at the top left corner above). In your case, use PHP quotes. It simply creates a better understanding.

And regarding your problem, you have not ended your html tag (at the end of your code) and this may be one of the reason for your problem.
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags: , ,



Thread Tools Search this Thread
Search this Thread:

Advanced Search


Similar Threads for: "php/mysql dropdown values are empty"
Thread Thread Starter Forum Replies Last Post
Excel 2010: Can VLOOKUP return multiple non-empty values from a table? marathon MS Office Support 3 21-01-2012 01:43 AM
W32 registry values are not getting matched by the default values Angrzej Networking & Security 5 19-05-2011 12:23 PM
Unable to install MySQL and MySQL Workbench on Linux mint 10 Zared Windows Software 4 13-01-2011 01:08 AM
Mysql Error : Can't connect to local mysql server through socket ' var lib mysql mysql.sock' 2 roshan45 Software Development 3 07-11-2009 09:36 PM
Dropdown menus SalVatore Software Development 3 17-10-2008 05:53 PM


All times are GMT +5.5. The time now is 10:27 PM.