I'm trying to make a function that would delete from a mysql database a record from a specific table specified by the user and the row which will be used in the WHERE clause depends on the table chose.
This is what I have:
$table=$_POST['table'];
$title=$_POST['title'];
if($table=="clients")
$row="fullname";
else if($table=="books")
$row="title";
else if($table="actors")
$row="fullname";
else if($table="employees")
$row="fullname";
$result = mysqli_query($con,"DELETE from $table WHERE $row='{$title}'") or die (mysqli_error($con));
if($result) {
$_SESSION['updateinfo']='deleted';
header("location:index.php");
} else {
$_SESSION['updateinfo']='not deleted!';
header("location:index.php");
}
Well, it works fine when I try to delete from clients,books,actors, but it does not work for employees.
I get the message " deleted ", but its still there.No error, no nothing. Also I would have to add 10 more those tables, is there a better way to do this?
I tried to echo $table, $title and $row and it seems that the SELECT from the HTML form only passes to the PHP script the correct value for the first 3 options, for the rest of them it passes a wrong value, below is my html form
<select name="table" style="padding:5px;margin:5px;width:200px;">
<option value="clients" selected="selected">Clients</option>
<option value="books">Books</option>
<option value="actors">Actors</option>
<option value="employees">Em</option>
<option value="other1">smthother</option>
<option value="other2">smtoher2</option>
</select>