0

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

4
Contributors
4
Replies
21
Views
4 Years
Discussion Span
Last Post by Dramz0r
0

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

0

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

Edited by TonyG_cyprus

0

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!

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.