Note that there are some explanatory texts on larger screens.

plurals
  1. PODeleting a row from a database table using checkbox
    primarykey
    data
    text
    <p>I want to display a record from a database in a table with checkboxes in every row. That checkboxes will determine if the user wants to delete that specific row if it will be checked. I was able to display the data from the DB but when i press the delete button nothing happen. Im not sure but i think the error is in my deleting part of the code but i could be wrong. Im not sure. Anyway, here is the code, i just remove some lines</p> <pre><code> &lt;?php $link = mysql_connect("localhost","root", "123"); if(!$link) { die('Failed to connect to server: ' . mysql_error()); } $db= mysql_select_db("abc"); if(!$db) { die("Unable to select database"); } $search = $_POST['search']; $searchbox = $_POST['searchbox']; $query = ("SELECT $search FROM table where $search = '$searchbox'"); $result=mysql_query($query); if($result) { if(mysql_num_rows($result)&lt;=0) { echo "&lt;script type='text/javascript'&gt;alert('The entry does not exist'); location.href = 'admin_home.php';&lt;/script&gt;"; } } switch ($search) { case 'studentnumber': $result = mysql_query("SELECT * FROM table WHERE $search = '$searchbox'"); if($result){ echo "&lt;table class='hovertable'&gt; &lt;tr&gt; &lt;caption&gt;Student Records&lt;/caption&gt; &lt;th colspan='1'&gt; Student Number&lt;/th&gt; &lt;th colspan='8'&gt; $searchbox&lt;/th&gt; &lt;/tr&gt; &lt;tr&gt; &lt;th&gt;Delete&lt;/th&gt; &lt;th&gt;Student Number&lt;/th&gt; &lt;th&gt;College&lt;/th&gt; &lt;th&gt;Course&lt;/th&gt; &lt;th&gt;Status&lt;/th&gt; &lt;th&gt;Last Name&lt;/th&gt; &lt;th&gt;First Name&lt;/th&gt; &lt;th&gt;Middle Name&lt;/th&gt; &lt;th&gt;Address&lt;/th&gt; &lt;th&gt;Gender&lt;/th&gt; &lt;th&gt;Civil Status&lt;/th&gt; &lt;th&gt;Religion&lt;/th&gt; &lt;th&gt;Email Address&lt;/th&gt; &lt;th&gt;Month&lt;/th&gt; &lt;th&gt;Day&lt;/th&gt; &lt;th&gt;Year&lt;/th&gt; &lt;th&gt;Father's Name&lt;/th&gt; &lt;th&gt;Father's Occupation&lt;/th&gt; &lt;th&gt;Mother's Name&lt;/th&gt; &lt;th&gt;Mother's Name&lt;/th&gt; &lt;/tr&gt;"; while($row = mysql_fetch_array($result)) { echo "&lt;tr onmouseover=\"this.style.backgroundColor='#ffff66';\" onmouseout=\"this.style.backgroundColor='#d4e3e5';\"&gt;"; ?&gt; &lt;td&gt;&lt;input name="need_delete[&lt;? echo $rows['id']; ?&gt;]" type="checkbox" id="checkbox[&lt;? echo $rows['id']; ?&gt;]" value="&lt;? echo $rows['id']; ?&gt;"&gt;&lt;/td&gt; &lt;?php echo "&lt;td&gt;" . $row['studentnumber'] . "&lt;/td&gt;"; echo "&lt;td&gt;" . $row['college'] . "&lt;/td&gt;"; echo "&lt;td&gt;" . $row['course'] . "&lt;/td&gt;"; echo "&lt;td&gt;" . $row['status'] . "&lt;/td&gt;"; echo "&lt;td&gt;" . $row['lname'] . "&lt;/td&gt;"; echo "&lt;td&gt;" . $row['fname'] . "&lt;/td&gt;"; echo "&lt;td&gt;" . $row['mname'] . "&lt;/td&gt;"; echo "&lt;td&gt;" . $row['address'] . "&lt;/td&gt;"; echo "&lt;td&gt;" . $row['gender'] . "&lt;/td&gt;"; echo "&lt;td&gt;" . $row['civilstatus'] . "&lt;/td&gt;"; echo "&lt;td&gt;" . $row['religion'] . "&lt;/td&gt;"; echo "&lt;td&gt;" . $row['emailaddress'] . "&lt;/td&gt;"; echo "&lt;td&gt;" . $row['month'] . "&lt;/td&gt;"; echo "&lt;td&gt;" . $row['day'] . "&lt;/td&gt;"; echo "&lt;td&gt;" . $row['year'] . "&lt;/td&gt;"; echo "&lt;td&gt;" . $row['father'] . "&lt;/td&gt;"; echo "&lt;td&gt;" . $row['fatheroccupation'] . "&lt;/td&gt;"; echo "&lt;td&gt;" . $row['mother'] . "&lt;/td&gt;"; echo "&lt;td&gt;" . $row['motheroccupation'] . "&lt;/td&gt;"; } echo "&lt;/table&gt;"; echo "&lt;/div&gt;"; } break; default: echo "&lt;script type='text/javascript'&gt;alert('An error occur'); location.href = 'admin_home.php';&lt;/script&gt;"; } ?&gt; &lt;input name="delete" type="submit" id="delete" value="Delete"&gt; &lt;?php // Check if delete button active, start this if ( ! empty($_POST['delete'])) { foreach ($_POST['need_delete'] as $id =&gt; $value) { $sql = 'DELETE FROM students` WHERE id='.(int)$id; mysql_query($sql); } echo "&lt;script type='text/javascript'&gt;alert('Record Successfully Deleted');location.href = 'admin_home.php';&lt;/script&gt;"; exit(); } mysql_close(); ?&gt; </code></pre> <p>Thanks in advance !!</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload