Simply trying to populate a combo box with field from DB.

Hi guys, first time on here so apologies if i haven’t put this in the right place.

OK, the scenario is to have a simple webpage that displays a combo box showing all CustID’s in my DB (which is stored on PHPMYADMIN). When i select a value from the combo box and press the delete button, it should delete all information for that particular customer.

I just cannot get it to populate my combobox at all and feel i am very close, just probably a tiny error here or there.

[php]<?php
$con = mysql_connect(“localhost”,“cwoodhouse2”,“cwoodhouse2”);
if (!$con)
{
die('Could not connect: ’ . mysql_error());
}

mysql_select_db(“cwoodhouse2”, $con);

mysql_close($con);

if ( isset( $_POST[‘submit’] ) ) {

$to_delete_q=“DELETE FROM customers WHERE SName=’” .$_POST[‘todelete’] . “’;”;

mysql_query($to_delete_q);

} else {

echo “”;

echo “”;

$result = mysql_query(“SELECT * FROM customers ORDER BY SName”);

for ($r=0; $r < mysql_num_rows($result); $r++)
{

$myarray = mysql_fetch_assoc($result);

echo “<option value=’” . $myarray[‘SName’] . “’>” . $myarray[‘SName’] . “” ;
}
echo “”;

echo “”;

}

?>
[/php]

I hope my code makes sense, this is my first ever attempt at using PHP and MYSQL so be gentle.

I Sit and wait, and hope. :slight_smile:

you have used mysql_close($con); before execution of query so it will close the connection with database and the query execution goes to fail because there is no connection

so put that statement at the end of all query execution or do not use that it will automatically close it

<?php $con = mysql_connect("localhost","cwoodhouse2","cwoodhouse2"); if (!$con) { die('Could not connect: ' . mysql_error()); } mysql_select_db("cwoodhouse2", $con); if ( isset( $_POST['submit'] ) ) { $to_delete_q="DELETE FROM customers WHERE SName='" .$_POST['todelete'] . "';"; mysql_query($to_delete_q); } else { echo ""; echo ""; $result = mysql_query("SELECT * FROM customers ORDER BY SName"); for ($r=0; $r < mysql_num_rows($result); $r++) { $myarray = mysql_fetch_assoc($result); echo "" . $myarray['SName'] . "" ; } echo ""; echo ""; } ?>
Sponsor our Newsletter | Privacy Policy | Terms of Service