Deleting a row in a database using php - php

I want to delete a row in a database that I have added but when i try to click the delete button in my index.php it says record deleted but when I click ok, it does not delete the record, here are my codes:
index.php:
$con = mysql_connect("localhost","pma");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("class_schedule", $con);
$result = mysql_query("SELECT * FROM schedule");
?>
<center>
<table border="1" width="800">
<tr>
<td colspan='8'><input type="text" width='300'/>
<input type="button" value="Search" onclick="location='search.php'" />
<input type="button" value="View All" onclick="window.location.href=''"/>
<input type="button" value="Add" onclick="location='add.php'"/></td>
</tr></table>
<?php
echo "<center><table border='1' width='800'>
<tr>
<td><center>Time</td>
<td><center>Subject</td>
<td><center>Course</td>
<td><center>Section</td>
<td><center>Day</td>
<td><center>Room</td>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['time'] . "</td>";
echo "<td>" . $row['subject'] . "</td>";
echo "<td>" . $row['course'] . "</td>";
echo "<td>" . $row['section'] . "</td>";
echo "<td>" . $row['day'] . "</td>";
echo "<td>" . $row['room'] . "</td>";
?>
<td><input type="submit" value="Edit" name="edit" />
<form action="delete.php" method="post">
<input type="submit" value="Delete" name="delete" onclick="location='delete.php'" />
</td>
</form>
<?php
echo "</tr>";
}
echo "</table></center>";
mysql_close($con);
?>
And this is my delete.php:
<?php
require "connect.php";
$deltime=$_POST["deltime"];
mysql_query("DELETE FROM schedule WHERE deltime='$deltime'");
mysql_close($con);
?>
<div>
<p align="center"><b>Record Deleted</b><br/>
<form method="post">
<center><input type="submit" name="submit" value="Ok" formaction="index.php" /></center>
</form>
</p></div></form>
What am I missing, and what should I remove and add?

your id was not getting in delete.php file so just remove your delete button and replace with this code:
<td><a href="delete.php? id=<?php echo $row['id'];?>">Delete</td>

Modify the following line
mysql_query("DELETE FROM schedule WHERE deltime='$deltime'");
As
mysql_query("DELETE FROM schedule WHERE deltime='$deltime'", $con);

Try to send the date through one link to the delete.php
<td><a href = 'delete.php'?date=<?php echo $row['time'];?>>DELETE</a></td>

Related

PHP submit buttons/structure?

here is a bit of code im working on. Everything works expect that part where i enter ID, then it shows me information in that ID row and also 2 boxes pop where i can write new value of variable. There is also 2 buttons, one which comfirms the edit which has been written into boxes and second one to delete that row from database.
Im still at kinda basics at programming as you can see and im quessing there is something wrong with structure in this part of code?
Can anyone help me to make these 2 submit buttons work as they should.
Thanks you all. :)
if(isset($_POST['idnumber'])) {
$idnumber = protect($_POST['idnumber']);
global $idnumber;
if($idnumber == ""){
echo "Please supply all fields!";
}
else{
?>
<table cellpadding="2" cellspacing="2">
<br />
<tr>
<td width="50px"><b>ID</b></td>
<td width="150px"><b>Current name</b></td>
<td width="200px"><b>Current description</b></td>
<td width="100px"><b>Time added</b></td>
<td width="100px"><b>Current status</b></td>
</tr>
<?php
$get_tasks = mysqli_query($con, "SELECT id FROM task WHERE id='$idnumber'") or die(mysql_error());
while($row = mysqli_fetch_assoc($get_tasks)){
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
$get_taskname = mysqli_query($con, "SELECT taskname FROM task WHERE id='$idnumber'") or die(mysql_error());
$task_name = mysqli_fetch_assoc($get_taskname);
echo "<td>" . $task_name['taskname'] . "</td>";
$get_description = mysqli_query($con, "SELECT description FROM task WHERE id='$idnumber'") or die(mysql_error());
$description_text = mysqli_fetch_assoc($get_description);
echo "<td>" . $description_text['description'] . "</td>";
$get_dateadded = mysqli_query($con, "SELECT dateadded FROM task WHERE id='$idnumber'") or die(mysql_error());
$date_added = mysqli_fetch_assoc($get_dateadded);
echo "<td>" . $date_added['dateadded'] . "</td>";
$get_status = mysqli_query($con, "SELECT status FROM task WHERE id='$idnumber'") or die(mysql_error());
$cur_status = mysqli_fetch_assoc($get_status);
if($cur_status['status'] == 1) {
echo "<td>" . "Unfinished" . "</td>";
}
else{
echo "<td>" . "Finished" . "</td>";
}
echo "</tr>";
}
if(isset($_POST['edittask'])) {
$editname = protect($_POST['editname']);
mysqli_query($con, "UPDATE task SET taskname=$editname WHERE id='$idnumber'") or die(mysql_error());
}
elseif(isset($_POST['deletetask'])) {
mysqli_query($con, "DELETE FROM task WHERE id='$idnumber'") or die(mysql_error());
}
?>
<form action="test2.php" method="POST" >
<td width="50px"><b></b></td>
<td width="150px"><b>New name: <input type="text" name="editname"/></b></td>
<td width="200px"><b>New desc: <input type="text" name="editdesc"/></b></td>
<td width="100px"><b></b></td>
<td width="100px"><b>D</b></td>
<input type="submit" name="edittask" value="Edit task"/> <br />
<input type="submit" name="deletetask" value="Delete task"/> <br />
</form>
<?php
}
}
?>
</table>
<br /><br />
<form action="test2.php" method="POST" >
Insert ID of task to edit: <input type="text" name="idnumber"/>
<input type="submit" name="ids" value="Find task"/> <br />
</form>

Cant seem to EDIT/MODIFY my php table by id

<?php
$con = mysql_connect("localhost","root");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("database", $con);
if(!isset($_POST['submit'])){
$result = mysql_query("SELECT * FROM pleasework ORDER BY ID");
$row = mysql_fetch_array($result);
}
?>
<form action="?php echo $_SERVER['PHP_SELF'];?>" id="form2" method="post" name="form2">
<img id="close1" src="X.png" width="25" height="25" onclick ="div_hide1()">
<h2><font size="6">Please change existing data</font></h2>
<hr>
<br>
<font color="yellow">Change Name to: </font><input type="text" name="New" value="<?php echo $row['Name'];?>"/><br><br>
<font color="yellow"> Change Cause to: </font> <input type="text" name="New1" value="<?php echo $row['Cause'];?>"/><br><br>
<font color="yellow">Change Symptom to: </font><input type="text" name="New2" value="<?php echo $row['Symptom'];?>"/><br><br>
<font color="yellow"> Change Gene_affected to: </font><input type="text" name="New3"value="<?php echo $row['Gene_affected'];?>" /><br><br>
<input type="hidden" name="id" value="<?php echo $_GET['ID'];?>"/>
<input type="submit" onclick="clicked(event)" />
</form>
<?php
if(isset($_POST['submit'])){
mysql_query("UPDATE pleasework SET Name= '$_POST[New]' WHERE ID='$_POST[id]'");
mysql_query("UPDATE pleasework SET Cause= '$_POST[New1]' WHERE ID='$_POST[id]'");
mysql_query("UPDATE pleasework SET Symptom= '$_POST[New2]' WHERE ID='$_POST[id]'");
mysql_query("UPDATE pleasework SET Gene_affected= '$_POST[New3]' WHERE ID='$_POST[id]'");
echo "Change Successful<br>" ;
header("Location: databse.php");
mysql_close($con);
}
else {}
?>
This is my php file.
while($row = mysql_fetch_array($result))
{
echo "<TR>";
echo "<TD>" . $row['ID'] ."</TD>";
echo "<TD>" . $row['Name'] . " </TD>";
echo "<TD>" . $row['Cause'] . " </TD>";
echo "<TD>" . $row['Symptom']. " </TD>";
echo "<TD>" . $row['Gene_affected'] . " </TD>";
echo "<TD><font color='red'>Delete row</font> </TD>";
echo "<TD><font color='red'>modify</font> </TD>";
echo "</TR>";
}
And this is the section which has a modify button that links to the edit.php file. The error here is that is doesnt bring over the values in the table to the editing page and then submitting the form doesnt work too. help please
Your code appears a bit confused.
First of all, why to put the modify routine after output the form? Especially since after modify you send the header function, that fails if previously there are some output.
Note also a typo: you forgot to properly open the php tag in the form declaration. Change-it in this way:
<form action="<?php echo $_SERVER['PHP_SELF'];?>" id="form2" method="post" name="form2">
The main problem is that you check if the $_POST[submit] if set, but this is not set, due to the absence of attribute name.
Change it in this way:
<input type="submit" name="submit" onclick="clicked(event)" />
Now your script should work (I don't have tested the sql).
Please also note that your UPDATE routine is redundant: you can reduce the 4 statement to only one in this way:
$result = mysql_query
(
"UPDATE pleasework SET Name='{$_POST[New]}', Cause='{$_POST[New1]}', Symptom='{$_POST[New2]}', Gene_affected='{$_POST[New3]}' WHERE ID={$_POST[id]}"
);
About PHP Original MySQL API:
This extension is deprecated as of PHP 5.5.0, and has been removed as of PHP 7.0.0
NOTE: mysql_* deprecated, so try to use PDO or mysqli_*.
Simple way:
<?php
if(isset($_POST['submit'])){
$result = mysql_query("UPDATE pleasework
SET Name='".$_POST['New']."',
Cause='".$_POST['New1']."',
Symptom='".$_POST['New2']."',
Gene_affected='".$_POST['New3']."'
WHERE ID=".$_POST['id'].");
if($result ){
echo "Change Successful<br>" ;
header("Location: databse.php");
}
mysql_close($con);
}
YOUR PHP:
while($row = mysql_fetch_array($result))
{ $spaces = " ";
echo "<TR>";
echo "<TD>" . $row['ID'] ."</TD>";
echo "<TD>" . $row['Name'] . $spaces."</TD>";
echo "<TD>" . $row['Cause'] . $spaces."</TD>";
echo "<TD>" . $row['Symptom']. $spaces."</TD>";
echo "<TD>" . $row['Gene_affected'] . $spaces."</TD>";
echo "<TD><a href='delete.php?id=".$row['ID'] ."'>";
echo "<font color='red'>Delete row</font></a>".$spaces."</TD>";
echo "<TD><a href='edit.php?id=" . $row['ID'] ."'>";
echo "<font color='red'>modify</font></a>".$spaces."</TD>";
echo "</TR>";
}

Get value in hidden field, and display it in another page. What's wrong with my code?

This is a table of students' ID numbers, with a button after each ID.
When I click on a button, I want it to open a new page called "score.php", and display the selected ID.
But the code doesn't work. It only show the text "ID", but not the number.
Here is "index.php"
<html>
<head>test</head>
<body>
<form method="post" action="score.php">
<?php
$result = mysql_query("SELECT * FROM term3_2556")
or die(mysql_error());
echo "<table border='1'>";
echo "<tr> <th>Student ID</th> </tr>";
while($row = mysql_fetch_array( $result )) {
echo "<tr>";
echo '<td>' . $row['student_id'] . '<input type="hidden" name="student_id" value=" ' . $_POST['student_id'] . ' " /> <button type="submit" name="btn_student_id" >Select</button> </td> ';
echo '</tr>';
}
echo "</table>";
?>
</form>
</body>
</html>
And here is "score.php"
<head>test</head>
<body>
<?php
$student_id = $_POST["student_id"];
echo '<p> ID: '.$student_id.'</p>';
?>
</body>
Since you are using a <button>, there is no need to use a <input type="hidden">. Just add the student_id as the button value -
<button type="submit" name="btn_student_id" value=" ' . $row['student_id'] . ' " >Select</button>
Then in you php just get the value from the clicked on button -
<?php
$student_id = $_POST["btn_student_id"];
echo '<p> ID: '.$student_id.'</p>';
?>
your index.php file will be:
<html>
<head>test</head>
<body>
<form method="post" action="score.php">
<?php
$result = mysql_query("SELECT * FROM term3_2556")
or die(mysql_error());
echo "<table border='1'>";
echo "<tr> <th>Student ID</th> </tr>";
while($row = mysql_fetch_array( $result )) {
echo "<tr>";
echo '<td>' . $row['student_id'] . '<input type="hidden" name="student_id" value=" ' . $row['student_id'] . ' " /> <button type="submit" name="btn_student_id" >Select</button> </td> ';
echo '</tr>';
}
echo "</table>";
?>
</form>
</body>
</html>

Why wont this first form created by a php loop work?

I have a form that is generated in a table by a php loop. The form works fine except for the first one generated. I can't seem to figure out why. Here is my code, am i missing a closing somewhere?
while($row = mysql_fetch_array($result))
{
$msisdn = $row['msisdn'];
$messageid = $row['messageid'];
echo "<tr>";
echo "<td style='width:70px;'><center>" . $row['message-timestamp'] . "</td>";
echo "<td><center>" . $row['terpname'] . "</td>";
echo "<td>";
echo "<div class='layer1'>
<p class='heading'><B><U><font size='2' color='blue'>Reply</font></u></b> </p>
<div class='content'>
<fieldset >
<form name='reply' method='post' action='reply.php'>
<textarea rows='4' cols='50' name='response' value=''></textarea>
<input type='hidden' name='phonenumber' value='$msisdn' /><BR>
<input type='hidden' name='messageid' value='$messageid' /><BR>
<input type='submit' name='search' class='btn btn-info btn-large' value='Send' />
</form>
</fieldset>
</div>
</div>
";
echo "-->" . $row['text'];
//Select responses to this text message
$sqlselect = "SELECT * FROM li_appointments.li_outbound_sms
where messageid = '" . $row['messageid'] . "';";
$subresult = mysql_query($sqlselect);
//Loop through results and display them
while($row = mysql_fetch_array($subresult))
{
echo "<BR><--" . $row['sender'] . "(" . $row['datetime'] . "): " . $row['message'];
}
echo "</td>";
echo "<td style='width:10px;'><center><input type='checkbox' class='db' onclick='resetSelectAlldb();'name='database[]', value='$messageid'></center></td>";
echo "</tr>";
}
you should use prepare statements but anyway you can't use $row twice in the same function.
while($row = mysql_fetch_array($result))
{
$msisdn = $row['msisdn'];
$messageid = $row['messageid'];
echo "<tr>";
echo "<td style='width:70px;'><center>" . $row['message-timestamp'] . "</td>";
echo "<td><center>" . $row['terpname'] . "</td>";
echo "<td>";
echo "<div class='layer1'>
<p class='heading'><B><U><font size='2' color='blue'>Reply</font></u></b> </p>
<div class='content'>
<fieldset >
<form name='reply' method='post' action='reply.php'>
<textarea rows='4' cols='50' name='response' value=''></textarea>
<input type='hidden' name='phonenumber' value='$msisdn' /><BR>
<input type='hidden' name='messageid' value='$messageid' /><BR>
<input type='submit' name='search' class='btn btn-info btn-large' value='Send' />
</form>
</fieldset>
</div>
</div>
";
echo "-->" . $row['text'];
//Select responses to this text message
$sqlselect = "SELECT * FROM li_appointments.li_outbound_sms
where messageid = '" . $row['messageid'] . "';";
$subresult = mysql_query($sqlselect);
//Loop through results and display them
while($row2 = mysql_fetch_array($subresult))
{
echo "<BR><--" . $row2['sender'] . "(" . $row2['datetime'] . "): " . $row2['message'];
}
echo "</td>";
echo "<td style='width:10px;'><center><input type='checkbox' class='db' onclick='resetSelectAlldb();'name='database[]', value='$messageid'></center></td>";
echo "</tr>";
}

Get a mySQL line item id with PHP

Right now I have a table that displays menu items and I want an Admin to be able to delete and edit a particular line item. My delete.php code works correctly if I have the id equal to a particular number, but I want the id to be equal to the id of whichever row the delete button is in. So my question is how do I get that id number? Because what I'm doing now is not correct.
Here is my delete.php file:
<?php
$con = mysql_connect("localhost", "root", "");
if(!$con)
{
die('Could not connect: ' .mysql_error());
}
mysql_select_db("bics_place", $con);
echo "mysql connected";
$myid = $_POST['id'];
$sql = "DELETE FROM menu WHERE id='$myid'";
echo "sql = $sql";
if(!mysql_query($sql, $con))
{
die('Error: ' .mysql_Error());
}
echo "1 record deleted";
header("location:admin_menu.php");
mysql_close($con);
?>
This is the table being made in admin_menu.php
$result = mysql_query("SELECT * FROM menu");
echo "<table border='1' id='menu'>
<form method='post' action='delete.php'>
<tr>
<th> Id </th>
<th> Type </th>
<th> Product Name </th>
<th> Price </th>
<th></th>
<th></th>
</tr>";
while($row = mysql_fetch_assoc($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['type'] . "</td>";
echo "<td>" . $row['productName'] . "</td>";
echo "<td>" . $row['price'] . "</td>";
echo "<td>" . '<input type="submit" name="delete" value="Delete">' . "</td>";
echo "<td>" . '<input type="submit" name="edit" value="Edit">' . "</td>";
echo "</tr>";
}
echo "</form>";
echo "</table>";
Take on hidden field in your form.Then button onclick event set id into hidden field before submit.
<script>
function setId(idValue){
document.getElementById('myid').value=idValue;
}
</script>
echo "<table border='1' id='menu'>
<form method='post' action='delete.php'>
<input type="hidden" name="id" id="myid" value="" />
<tr>
<th> Id </th>
<th> Type </th>
<th> Product Name </th>
<th> Price </th>
<th></th>
<th></th>
</tr>";
while($row = mysql_fetch_assoc($result))
{
$myID = $row["id"];
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['type'] . "</td>";
echo "<td>" . $row['productName'] . "</td>";
echo "<td>" . $row['price'] . "</td>";
echo "<td>" . '<input type="submit" name="delete" value="Delete" onClick="setId('.$myID.');">' . "</td>";
echo "<td>" . '<input type="submit" name="edit" value="Edit">' . "</td>";
echo "</tr>";
}
echo "</form>";
echo "</table>";
Add a hidden field - id in the form :
while($row = mysql_fetch_assoc($result)) {
//your <td>'s here
echo '<input type="hidden" name="id" value="{$row[id]}">';
// echoes for form submit
}
Note : mysql_* functions are deprecated. You should use mysqli_ functions instead. Read here

Categories