Hi there,
Pretty new to PHP and having a problem when deleting data from my MySQL Table after populating it into a listbox, seletcing it and using a button to delete the selected row.

Heres the code:

<?php 

//create and execute query
$query = 'SELECT product_ID FROM products ORDER BY product_ID';
$result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());

//create selection list
echo "<select name='products'>\n";
echo "<option value='Select event'>Select an event to be edited\n";
while($row = mysql_fetch_row($result))
{
    $eventSelect = $row[0];
    $eventid = $row[1]; //set this to the column of the primary key
    echo "<option value='$eventid'>$eventSelect</option>\n";
}
echo "</select>"

?>                  
<input name="delete" type="submit" class="submitForm" value="Delete" id="delete">
</td>



<?php
// Delete event data
if ($_REQUEST['delete']){



//define variables
$eventSelect = $_POST['eventSelect'];
echo $eventSelect;

//create and execute query
$query = "DELETE FROM products WHERE product_ID = '$eventSelect' LIMIT 1";
$result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());

}
?>

Any Suggestions?.
Thanks

You will have to re-populate the select options in the list box when you click the delete button - reload the page.

Your form processing code should be looking for the value of $_POST['products'], not $_POST['eventSelect'].

Line 9 has no closing </option>
and the whole thing should surely be in a <form>...</form> for any data to be $_POST ed

hey guys, done everything that's been mentioned.
But having a new problem now, everything is being deleted fine and dandy.. But when one is deleted a new row will appear.
Any thoughts?

New Code:

<form method="post">
<?php 

//create and execute query
$query = 'SELECT id, product_ID, name FROM products ORDER BY product_ID';
$result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());

//create selection list

echo "<select name='products'>\n";
echo "<option value='Select event'>Select an event to be edited\n </option>";
while($row = mysql_fetch_row($result))
{
    $prodid = $row[0];
    $eventSelect = $row[1];
    $eventid = $row[2]; //set this to the column of the primary key
    echo "<option value=$prodid>$eventSelect,$eventid</option>\n";
}
echo "</select>"

?>                  
<input name="delete" type="submit" value="Delete" id="delete">




<?php
// Delete event data
if ($_REQUEST['delete']){


//define variables
$eventSelect = $_POST[products];


echo $eventSelect;

//create and execute query
$query = "DELETE FROM products WHERE id = $prodid LIMIT 1";
echo $query;
$result = mysql_query($query) or die ('Error in query: $query. ' . mysql_error());

}
?>

</form>

Thanks for the help!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.