I have a web page that outputs data from my sql database. Each row has a delete button that should delete the data in this particular row. I am having a issue where when I click the delete button it always deletes the row with the first/lowest ID regardless of which button I click. It doesnt delete the row I want it to delete. Here is my code:
HTML
<form action="process.php" method="post">
<?php
$sql = "
SELECT *
FROM playerTeam
";
$result = mysqli_query($connection, $sql);
?>
<table>
<?php
if (mysqli_num_rows($result) > 0) {
while ($row = mysqli_fetch_assoc($result)) { ?>
<tr>
<td><input type="text" name="id" value="<?php echo $row['id']?>"></td>
<td><input type="text" name="fName[]" value="<?php echo $row['firstName']?>"></td>
<td><input type="text" name="sName[]" value="<?php echo $row['surName']?>"></td>
<td><input type="text" name="team[]" value="<?php echo $row['team']?>"></td>
<td><input type="submit" name="delete" value="Delete"></td>
<td><input type="submit" name="update" value="Update"></td>
</tr>
<?php
}
}
?>
</table>
</form>
process.php
if (isset($_POST['delete'])) {
$id = $_POST['id'];
$sql = "DELETE FROM playerTeam WHERE id='$id'";
if (mysqli_query($connection, $sql)) {
echo "Record deleted";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($connection);
}
}
please could someone help me. Thanks
Because you delete the value of last id in
$id = $_POST['id'];
$sql = "DELETE FROM playerTeam WHERE id='$id'";
the value of $_POST['id'] is equal to last row in <?php echo $row['id']?>
in each run of the while the $_POST['id'] value replaced by new $row['id'] so the last on would be read in $_POST['id']
As you are using while($row = mysqli_fetch_assoc($result)), this will loop to the last row in your MySQL Table, thus every time, it will have the last id of your table.
You can code it in a way that your delete script will get the id. So, your delete button for each row will be have the row id, e.g. process.php?1, process.php?2, process.php?3 and so on.
tablepage.php (not sure of your page's real name)
Replace this line:
<td><input type="submit" name="delete" value="Delete"></td>
With this:
<td><p><a href="/process.php?id=<?php echo $id; ?>"></td>
process.php?id=1
// this will get `id=1` and thus your `id` will be `1`, and it will delete row `1`.
$id = $_GET['id'];
$sql = "DELETE FROM playerTeam WHERE id='$id'";
if (mysqli_query($connection, $sql)) {
echo "Record deleted";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($connection);
}
Hope that helps, thanks!
In actual in this condition ,use ajax and jquery is much more easier i think .But do only by php gonna be use logic code
$i = 0;
while($row = mysqli_fetch_assoc($result))
{
?>
<tr>
<td><input type="text" name="id_<?= $i ?>" value="<?php echo $row['id']?>"></td>
<td><input type="text" name="fName[]" value="<?php echo $row['firstName']?>"></td>
<td><input type="text" name="sName[]" value="<?php echo $row['surName']?>"></td>
<td><input type="text" name="team[]" value="<?php echo $row['team']?>"></td>
<td><input type="submit" name="delete[<?= $i ?>]" value="Delete"></td>
<td><input type="submit" name="update[<?= $i ?>]" value="Update"></td>
</tr>
<?php
$i++; //for unique num for each row
}
process.php
if (isset($_POST['delete'])){
$delete_id = array_keys($_POST['delete']);//print array key as a array
$delete_id = $delete_id[0];//so get array key
$id = $_POST["id_$delete_id"];//get its relative id
.....
}
Related
I have the following code to display and modify a simple sqlite table
<?php
$db = new SQLite3("my_sqlite.db");
$query = "SELECT rowid, * FROM students";
$result = $db->query($query);
if( isset($_POST['submit_data']) ){
// Gets the data from post
$name = $_POST['name'];
$email = $_POST['email'];
$query = "UPDATE students set name='$name', email='$email'";
if( $db->exec($query) ){
echo "Data is updated successfully.";
}else{
echo "Sorry, Data is not updated.";
}
}
?>
<table border="1">
<form action="" method="post">
<tr>
<td>Name</td>
<td>Email</td>
</tr>
<?php while($row = $result->fetchArray()) {?>
<tr>
<td><input name="name" type="text" value="<?php echo $row['name'];?>"></td>
<td><input name="email" type="text" value="<?php echo $row['email'];?>"></td>
</tr>
<?php } ?>
<input name="submit_data" type="submit" value="Update Data">
</form>
</table>
PROBLEM: When I change some of the information and update, the whole column changes into the same change. E.g.: if I write a the name Nick, every name changes into Nick.
First, you should only do updates for one record at a time so each record needs its own update button. Attached is the corresponding rʔwid of the record. you can use:
<input type="hidden" name="rowid" value="$row['rowid]">
You should add a WHERE clause to the update statement to know exactly which records should be updated.If you omit the WHERE clause, ALL records will be updated!
When I refresh page table row deleted and last query deleted when I click on any delete button of other row. Why is this happened?
This is table which I displayed
<?php
global $wpdb;
$table= 'wp_contact_form';
$result = $wpdb->get_results ( "SELECT * FROM $table" );
if(!empty($result)){
foreach ( $result as $print ) {
?>
<tr>
<td><?php echo $print->id;?></td>
<td><?php echo $print->names;?></td>
<td><?php echo $print->emails;?></td>
<td><?php echo $print->gender;?></td>
<td><?php echo $print->age;?></td>
<td><input type="submit" value="Edit" id="" name="update"></td>
<td><input type="submit" value="delete" id="delete" name="delete"></td>
</tr>
<?php
}
}
?>
this is delete query
$id= $print->id;
if(isset($_POST['delete']))
{
$result = $wpdb->delete($table, array('id' => $id));
if(!empty($result))
{
echo "success";
}
}
error screenshot - https://prnt.sc/wdg3xv
You're not specifying which ID should be deleted if $_POST['delete'] is set, right now you're asking WP to just delete something.
You need to wrap your elements in a form and add a field that contains the exact ID of the element you want to delete, like this:
<td>
<input type="submit" name="delete" value="delete" />
<input type="hidden" name="targetItem" value="<?php echo $print->id;?>" />
</td>
Then in your PHP use that field's ID to delete it from the DB:
if (isset($_POST['delete']))
$result = $wpdb->delete($table, array('id' => $_POST['targetItem']));
Situation: List of question will be read from the database and display inside a table. I can add new row or remove row that I wanted. The data that will be submitted will be store using an array. Update and insert are works perfectly as shown below.
Problems: If user delete some rows on the table, how can I delete those data inside the database.
Sorry for bad grammar/english. Thanks for helping!
Html code:
<table class="msgBox" id="simulator2">
<tr>
<th></th>
<th class="msgSubject">QUESTION</th>
</tr>
<?php
$qry="SELECT ques_item, ques_id FROM log_question WHERE eva_id = '$action'";
$result=mysqli_query($con, $qry);
$count=mysqli_num_rows($result);
$i=1;
while($rows=mysqli_fetch_array($result)){
$eQuesID = $rows['ques_id'];
$eQues = $rows['ques_item'];
?>
<tr class="table-row">
<td><input type="checkbox" name="chk" /></td>
<td class="dataBox"><input type="text" name="eQues[]" value="<?php echo $eQues; ?>" /><input type="hidden" name="eQuesID[]" value="<?php echo $eQuesID; ?>" /></td>
</tr>
<?php $i++;
} $tempCount = $i-1;
?>
</table>
<br />
<center>
<input type="button" value="Add Row" onclick="addRow('simulator2')" />
<input type="button" value="Delete Row" onclick="deleteRow('simulator2')" />
</center>
Php code:
$a = 0;
foreach (array_combine($eQues, $eQuesID) as $eQuest => $eQuestID){
if ($a < $tempCount){ // Update Question
$sql="UPDATE log_question SET ques_item='$eQuest' WHERE ques_id='$eQuestID'";
$test=mysqli_query($con, $sql);
//Check whether the query was successful or not
if(!$test) {
echo("Error description: " . mysqli_error($con));
exit();
}
} else { //Insert New Question
$newsql="INSERT INTO log_question VALUES('', '$eID', '$eQuest')";
$newtest=mysqli_query($con, $newsql);
//Check whether the query was successful or not
if(!$newtest){
echo("Error description: " . mysqli_error($con));
exit();
}
} $a++;
}
I have this form and out of this form, I can get 2 values from the radio buttons with post. But, I want to able to send another value winch is in my while loop the $fieldname variable with my form. I just don't know how to do that.
This my code:
$result = mysqli_query($con,"SELECT * FROM Velden");
while($row = mysqli_fetch_array($result)) {
echo "<div>";
echo "<h1>".$row['name']."</h1>";
echo "<h3>".$row['locatie']."</h3>";
echo '<img src="images/'.$row['photo'].'" width="120px" height="120px"/>';
echo "<p>".$row['aanwezig']."</p>";
$namefield = $row['name'];
$players = mysqli_query($con, "SELECT name, user_status FROM veld_user WHERE user_status=1 AND name='$namefield'");
echo "veld: ".$row['name']."<br />";
$number = mysqli_num_rows($players);
echo "Aantal spelers aanwezig: ".$number."<br /><br />";
?>
<form action="" method="post" id="registerForm">
<table class="form imageFrom">
<tr>
<td><input checked type="radio" name="status" value="1"/> aanwezig</td> <?php if (isset($_POST['status']) && $_POST['status']=='1') echo ' STATUS="aanwezig"';?>
<td><input checked type="radio" name="status" value="0"/> afwezig</td><?php if (isset($_POST['status']) && $_POST['status']=='0') echo ' STATUS="afwezig"';?>
</tr>
<tr>
<td><input type="submit" name="submit" value="submit" class="knop"/></td>
</tr>
</table>
</form><?php
echo"</div>";
}
And this is the code were i get the post. And update my database
if(isset($_POST['submit'])){
if (isset($_POST['status']) && $_POST['status']=='1'){
$sql = "UPDATE veld_user SET user_status = 1 WHERE id=".$user->data()->id;}
elseif (isset($_POST['status']) && $_POST['status']=='0'){
$sql = "UPDATE veld_user SET user_status = 0 WHERE id= ".$user->data()->id;}
if (mysqli_query($con, $sql)) {
Session::flash('home', 'update success');
} else {
echo "Error updating record: " . mysqli_error($con);
}}
Btw thnx guys...input hidden made it work
<input type="hidden" name="fieldname" value="<?php echo $namefield?>" />
$as = mysql_query('SELECT u.id,u.username,c.score FROM user u, course c WHERE u.id = c.userid ');
echo '<form action="score.php" method="post"><table>';
while($row = mysql_fetch_array($as)
{
$uid = $row['id'];
$username = $row['username'];
$score = $row['score'];
echo '<tr><td>'.$username.'</td>
<td><input type="hidden" name="uid" value='.$uid.'>
<input type="text" name="score" value='.$score.'>
</td>
</tr>
}
echo '<tr><td><input type="submit" name="submit" value="update"></td></tr>';
echo '</table></form>';
if($_SERVER['REQUEST_METHOD == 'POST']
{
$uid = $_POST['uid'];
$score = $_POST['score'];
$sql = mysql('UPDATE user SET c.score = '.$score.' WHERE c.userid = '.$uid.'');
}
course table
userid score
4 45%
3 30%
5 80%
It wasn't updating to table. And I tried to echo the variables, It was showing only the last row, but I edited for user 3 Can anyone suggest where I went wrong
Your reusing the same input, so it will only submit the last one
change
echo '<tr><td>'.$username.'</td>
<td><input type="hidden" name="uid" value='.$uid.'>
<input type="text" name="score" value='.$score.'>
</td>
</tr>
to
echo '<tr><td>'.$username.'</td>
<td><input type="hidden" name="uid['.$uid.']" value='.$uid.'>
<input type="text" name="score['.$uid.']" value='.$score.'>
</td>
</tr>
ALSO
if(sizeof($_POST)>0)
{
if(is_array($_POST['uid']))
{
while(list($key,$value)=each($_POST['uid'])
{
$sql="UPDATE user SET score='".mysql_real_escape_string($_POST['score'][$key])."' WHERE userid=".intval($value);
mysql_query($sql);
}
}
}