Vineet Basantani Vineet Basantani - 6 months ago 19
MySQL Question

Unable to delete entry from table where value contains a space in the middle?

I'm able to delete the entries which have a single word like 'Tomato' or 'Potato'.
Unable to delete entries with multiple words like 'pumpkin soup'.

Why is that?

Here's my PHP:

<?php
if(!empty($_POST['event_name_box']))
{
$del_event=$_POST['event_name_box'];
$sth = $conn->prepare("DELETE FROM event WHERE event_name=:del");
$sth->execute(array(':del'=>$del_event));
header( "Refresh: 0;" );
}
?>


And this is how I'm loading the values in drop down:

<select name="event_name_box" value="event_name_box" style="width:220px; padding-left:40px;font-size:18px;font-family:Roboto;">
<?php
$sth = $conn->prepare('Select event_name From event');
$sth->execute();
$data = $sth->fetchAll();
foreach ($data as $row ){
if($row['event_name']!="")
echo " <option id=\"EventName\" name=\"EventName\" value=".$row['event_name'].">".$row['event_name']."</option>";
}
?>
</select><br>
<input type="submit" name="submit" style="width:50%;padding:10px" value="Delete"</input>

Answer

The problem is in your HTML (specifically, the <option> tag). Your value isn't quoted, so the space is breaking the value sent in the POST. Change the echo on your option to something like this:

echo '<option value="' . $row['event_name'] . '">' . $row['event_name'] . '</option>';

(You also shouldn't need the name and id on your <option> tags.)