Cannot delete row in database using PHP - php

I can't seem to delete row in database by id in php
I think the the id is not passed to the $_POST['delete']
however, the popup "Your data is deleted" is displayed, but the data is not deleted.
So I'm not sure where is the error in this code.
I also try to delete the data by its id
for example: Delete book where no='4';
and the code seems to run fine because the data is deleted in the database.
<html>
<script>
function confirmDelete() {
return confirm('Are you sure?');
}
</script>
<!DOCTYPE html>
<head>
<form action="test.php" method="POST">
<br><br><br>
<table bordercolor="#FFCC66" align="center" bgcolor="#FFFFFF">
<tr>
<th>No</th>
<th>Title</th>
<th>Author</th>
<th>Year</th>
<th>Donor's Name</th>
<th>Call Number</th>
<th>Date Received</th>
<th>Handled By</th>
<th></th>
<th></th>
</tr>
<?php
include ('config.php');
$view=mysqli_query($conn,"SELECT * FROM book");
?>
<?php while($v=mysqli_fetch_array($view)){ ?>
<tr>
<td>
<?php echo $v["no"];?>
</td>
<td>
<?php echo $v["title"];?>
</td>
<td>
<?php echo $v["author"];?>
</td>
<td>
<?php echo $v["year"];?>
</td>
<td>
<?php echo $v["donorname"];?>
</td>
<td>
<?php echo $v["callnum"];?>
</td>
<td>
<?php echo $v["datereceived"];?>
</td>
<td>
<?php echo $v["handledby"];?>
</td>
<td><input type="submit" name="delete" value="Delete" onclick="return confirmDelete('Are you sure?');" /></td>
</tr>
<?php
} ?>
</tr>
</table>
<br><br>
</form>
</body>
</html>
<?php
if(isset($_POST['delete']))
{
include('config.php');
$no =$v["no"];
$d=mysqli_query($conn,"DELETE FROM `book` WHERE no='$no'");
if ($d)
{
echo "<script type='text/javascript'> alert('Your data is deleted!'); </script>";
echo"<meta http-equiv='Refresh' content='0' >";
}
else
{
echo "<script type='text/javascript'> alert('Your data cannot delete!'); </script>";
}
mysqli_close($conn);
}
?>

Change the submit element to
<td>
<input type="submit" name="delete" value="<?php echo $v['no'];?>" onclick="return confirmDelete('Are you sure?');" />
</td>
and
$no = $_POST["delete"];

Another solution si to add a hidden input with your value.
<td>
<?php echo $v["no"];?>
<input type="hidden" value="<?php echo $v["no"];?>" />
</td>
In your php you will find the value in $_POST['no']
This solution is better to pass multiple arguments in POST like a captcha or a confirmation (checkbox).

logic is not correct, while you press the delete button, all the data will be passed along with submitting because your tag is outside of the loop.
As my opinion, you should use ajax like functionality here, or follow this method.
<?php while($v=mysqli_fetch_array($view)){ ?>
<form action="test.php" method="POST">
<tr>
<td>
<?php echo $v["no"];?>
<input type="hidden" value="<?php echo $v["no"];?>" name="no" >
</td>
<td><input type="submit" name="delete" value="Delete" onclick="return confirmDelete('Are you sure?');" /></td>
</tr>
</form>
<?php } ?>
and in your post call use $no = $_POST['no']; instead of $no =$v["no"];

Related

How to Post some datas into mySQL server

I am trying to get some informations from users such as name midterm final grades and post them into database which I am using the MySQL server.
The problem is when I press the Add to DD button nothing changes and the data does not go into my table in database.
here is my main code:
<html>
<body>
<table border ="1">
<tr>
<td>Name</td>
<td><input id="name"> </input></td>
</tr>
<tr>
<td>Midterm</td>
<td> <input id="midterm"> </input></td>
</tr>
<tr>
<td>Final</td>
<td> <input id="final"> </input></td>
</tr>
<tr>
<td>Grade</td>
<td> <input id="grade"> </input></td>
</tr>
<td><input type="button" onclick="calculate()" value="Calculate"></td>
<td> <input type="submit" value="Add to DB"> </td>
</tr>
</table>
</body>
<script type="text/javascript">
function calculate(){
var mid=document.getElementById('midterm').value;
var fin=document.getElementById('final').value;
var grade=mid*(0.3)+fin*(0.7);
document.getElementById('grade').value=grade;
}
</script>
</html>
and this is also the code that inserts datas :
<html>
<body>
<form method="get" action="grade.php">
<input type="text" value="Welcome to Student Grades Calculator">
<br>
<input type="submit" value="GO">
</form>
</body>
</html>
<?php
$name=$_POST['name'];
$midterm=$_POST['midterm'];
$final=$_POST['final'];
$grade=$_POST['grade'];
echo 'Hey';
$connect= mysql_connect('localhost','root','','test');
if(mysqli_connect_errno())
{
echo "Failed to connect to MySQL:".mysql_connect_errno();;
}
$s = "INSERT INTO(name,midterm,final,grade) VALUES('rr','33','33','33')";
$sql = "INSERT INTO(name,midterm,final,grade) VALUES ('$name','$midterm','$final','$grade')";
mysqli_query($connect , $sql);
mysqli_close($connect);
?>
You need to set your variables to $_GETnot $_POST
$name=$_GET['name'];
$midterm=$_GET['midterm'];
$final=$_GET['final'];
$grade=$_GET['grade'];
I also recommend using post instead of get just for security

Can you update in a div tag

I have a little question about something. I have some forms, where I send the input to a MySQL database. I get the return from the database, out in some div tags. Here I have 2 buttons. Button number 1 can delete the row, and button number 2 should have a function, where I can update a specific row, if I want to change the content of the row. But can I update a div tag? I can see on the net, that a lot of people use tables to do that.
<html>
<head>
<link rel="stylesheet" type="text/css" href="css/arrangeTables.css">
</head>
<body>
<?php
include 'connection.php';
if(isset($_POST['addto'])){
// Insert to database
$date = $_POST['date'];
$day = $_POST['day'];
$fromtime = $_POST['fromtime'];
$totime = $_POST['totime'];
$sql = "INSERT INTO addWorkTime(date, day, fromtime, totime) VALUES('$date', '$day', '$fromtime', '$totime')";
$result = mysql_query($sql, $dbhandle) or die(mysql_error($dbhandle));
// Update of the row
if(isset($_POST['update'])){
$hidden = mysql_real_escape_string($_POST['hidden']);
$UpdateQuery = "UPDATE addWorkTime
SET date='$_POST[date]',
day='$_POST[day]',
fromtime='$_POST[fromtime]',
totime='$_POST[totime]'
WHERE p_id='$_POST[hidden]'";
$update = mysql_query($UpdateQuery, $dbhandle) or die(mysql_error($dbhandle));
if($update) {
echo "Succes";
} else {
echo "Der er en fejl";
}
}; // brace for if(isset($_POST['update']))
if($result){
echo "Insert successful.";
}
}; // brace for if(isset($_POST['addto']))
if(isset($_POST['delete'])){
$hidden = mysql_real_escape_string($_POST['hidden']);
$DeleteQuery = "DELETE FROM addWorkTime WHERE p_id=$hidden";
$delete = mysql_query($DeleteQuery, $dbhandle) or die(mysql_error($dbhandle));
if($delete){
echo "Delete successful";
}
}
//Return records from database
$result = mysql_query("SELECT p_id, date, day, fromtime, totime FROM addWorkTime");
?>
<form method="post">
<h3>Add your worktime to database</h3><br>
Date:
<input type="date" name="date"><br><br>
Day
<select name="day">
<option value="Mandag">Mandag</option>
<option value="Tirsdag">Tirsdag</option>
<option value="Onsdag">Onsdag</option>
<option value="Torsdag">Torsdag</option>
<option value="Fredag">Fredag</option>
<option value="Lørdag">Lørdag</option>
<option value="Søndag">Søndag</option>
</select>
From time:
<input type="time" name="fromtime">
To time:
<input type="time" name="totime">
<input type="submit" name="addto" value="submit"><br><br>
<!-- Return from the database -->
<h3>Return from database:</h3><br>
<!-- headers -->
<table>
<tr>
<th class="column0">Primary Key</th>
<th class="column1">Date</th>
<th class="column2">Day</th>
<th class="column3">From</th>
<th class="column4">To</th>
</tr>
</table>
</form>
<!--loop through through the database -->
<?php while($row = mysql_fetch_array($result)): ?>
<form method="post">
<table>
<tr>
<td class="resultcolumn0"><?php echo $row{'p_id'};?></td>
<td class="resultcolumn1"><?php echo $row{'date'};?><br></td>
<td class="resultcolumn2"><?php echo $row{'day'};?></td>
<td class="resultcolumn3"><?php echo $row{'fromtime'};?></td>
<td class="resultcolumn4"><?php echo $row{'totime'};?></td>
<td><input type="hidden" name="hidden" value="<?php echo $row{'p_id'}?>"><?php echo $row{'p_id'}?></td>
<td><input type="submit" name="update" value="Update"></td>
<td><input type="submit" name="delete" value="Delete"></td>
</tr>
</table>
</form>
<?php endwhile; ?>
</body>
</html>
If you want to update your cell table like your recording, you can do a tricky way like this code snippet:
function change1() {
var inp1 = document.getElementById('myTable').rows[1].cells[0];
inp1.innerHTML = "<input type='text' name='inp1'>";
var inp2 = document.getElementById('myTable').rows[1].cells[1];
inp2.innerHTML = "<input type='text' name='inp2'>";
}
function change2() {
var inp1 = document.getElementById('myTable').rows[2].cells[0];
inp1.innerHTML = "<input type='text' name='inp1'>";
var inp2 = document.getElementById('myTable').rows[2].cells[1];
inp2.innerHTML = "<input type='text' name='inp2'>";
}
table, td {
border: 1px solid black;
}
<table id="myTable">
<tr>
<td>column 1</td>
<td>column 2</td>
<td>action</td>
</tr>
<tr>
<td>Row2 cell1</td>
<td>Row2 cell2</td>
<td>
<button onclick="change1()">Update</button>
</td>
</tr>
<tr>
<td>Row3 cell1</td>
<td>Row3 cell2</td>
<td>
<button onclick="change2()">Update</button>
</td>
</tr>
</table>
<button onclick="alert('whatever function to save the edit')">Save</button>
you can improve that to give <form> tag to your <table> tag and save that using another Save button. Or you can use if conditional to your update button for change to textfield and post it to your database.
<form method="post" action='function_to_update_or_delete.php'>
<table>
<tr>
<td class="resultcolumn0"><?php echo $row{'p_id'};?></td>
<td class="resultcolumn1"><?php echo $row{'date'};?><br></td>
<td class="resultcolumn2"><?php echo $row{'day'};?></td>
<td class="resultcolumn3"><?php echo $row{'fromtime'};?></td>
<td class="resultcolumn4"><?php echo $row{'totime'};?></td>
<td><input type="hidden" name="hidden" value="<?php echo $row{'p_id'}?>"><?php echo $row{'p_id'}?></td>
<td><button onclick="<?php echo change($some_information_to_your_cell)?>">Update</button></td>
<td><input type="submit" name="delete" value="Delete"></td>
</tr>
</table>
</form>
And, please try to make a function dynamically each row, don't hardcode it like function change1(), function change2(), function change3(), etc
Hope this will help you out. :)

Form only sending the last row from a form and not the selected one

I have a table where each row in the table has a button that allows you to delete that particular row from the database. Although somehow my form only submits the last row and no the one I selected. Please ignore the deprecated tags, well aware of the issues.
<?php
$result = mysql_query("SELECT * FROM booking");
while($row = mysql_fetch_array($result))
{
?>
<form class='table-form' id='form' method='post'>
<tr>
<input id="bookid" type="hidden" name="bookid" value="<?php echo ($row['booking_id']); ?>" />
<td>
<?php echo ($row['booking_id']);?>
</td>
<td>
<?php echo ($row['user_id']); ?>
</td>
<td>
<?php echo ($row['event_id']); ?>
</td>
<td>
<?php echo ($row['payment_type']); ?>
</td>
<td>
<?php echo ($row['booking_date']); ?>
</td>
<center><button type="submit" id="submit" name="submit">Cancel</button><center>
<td>
</td>
</tr>
<?php
}
?>
</form>
</table>
</div>
<?php
if (isset($_POST['bookid'])){
$id = ($_POST['bookid']);
$result = mysql_query("DELETE FROM booking
WHERE booking_id = '$id'");
}
?>
If you preffer programing this way, move </form> above
<?php
}
?>
(to cycle body).
Your code needs a lot of clean up, style cleaning and html validation. :-P

Removing text via js and data posting

<script type="text/javascript">
function removeLink()
{
document.getElementById("tab2").deleteRow(i);
}
</script>
</head>
<body>
<form action="forth.php" method="post">
<table width="600" border="1" id="tab2">
<?php
foreach($_POST as $post2)
{
?>
<tr>
<td>
<?php
echo $post2.'<br />';
?>
</td>
<td>Remove</td>
</tr>
<?php
}
?>
<tr>
<td><input type="submit" value="Next" /></td>
<td> </td>
</tr>
</table>
</form>
</body>
this is my page third.php it redirect user to new page forth.php on forth.php i have following code
<?php
print_r($_POST);
foreach($_POST as $key_post)
{
echo $key_post.'<br>';
}
?>
the issue is on page forth.php it doesn't print anything even when I did print_r($_POST); it returned me empty array like Array(), and help why data is not saved.
I assume it's empty because you don't actually have any <input name="foo" type="bar /> fields other than the submit button, so nothing is actually being posted.
You'll need to add some hidden fields:
foreach($_POST as $key => $post2)
{
?>
<tr>
<td>
<?php echo $post2.'<br />'; ?>
<input type="hidden" name="<?php echo $key;?>" value="<?php echo $post2;?>" />
</td>
</tr>
... etc

How to retrieve value from the Check boxes?

I'm trying to get the emails corresponding to the checkbox using the following codes. But, I'm not getting the correct checked emails in the new variable. Can anyone please check ??
<?php
include("connection.php");
$username=$_SESSION['username'];
$query=mysql_query("SELECT * FROM contacts WHERE username='$username'");
$num=mysql_num_rows($query);
$info=mysql_fetch_array($query);
$i=0;
$msg='';
?>
<table width="672" border="0">
<?php
$i=0;
while($info)
{
?>
<form action="compose.php" method="post">
<tr style="font-size:14px;">
<td width="21" bgcolor="#f2f2f2"> <input type="checkbox" name="add" onSelect="<?php $msg=$msg.$info['email'].", ";?>"/> </td>
<td width="229" bgcolor="#f2f2f2"> <?php echo $info['email']; ?> </td>
<td width="408" bgcolor="#f2f2f2"> <?php echo $info['name']; ?> </td>
</tr>
<?php
$info=mysql_fetch_array($query);
$i++;
}
$_SESSION['contacts']=$msg;
?>
<tr><td></td><td></td><td><br />
<input class="new-button" type="submit" value="Insert & Compose" name="submit" /></td>
</tr>
</form>
</table>
To get any value back for checkboxes they must have a value=. In your case you probably would want the value to be the according email address.
One problem with your code is using onSelect= instead of value=, and second you didn't print the actual value into the page. Rewrite it to:
<td width="21" bgcolor="#f2f2f2">
<input type="checkbox" name="add"
value="<?php print $info['email']; ?>"/> </td>
If you need the $msg variable to do something, assemble it after the output.
<input type="checkbox" name="add" value="<?php echo $msg.$info['email'];?>"/>
checkbox does not have onSelect event probobly you got value in mind and in PHP code you should echo and what .", " is for?

Categories