简体   繁体   中英

Save button for each row in the table. PHP MYSQL

Please help, each row in the table has save button for editing/adding content. I have 3 textboxes in each row,PersoninCharge,PIC_Comments and Status. The user can add/edit these textboxes whenever they click the save button on that particular row. The problem is, whenever I add/edit data in one row, the save button can't read what particular row I edited.

The save button is perfectly running if I searched the invoice number first, but what I want is to directly edit/add the data in each row without searching it first.

Here's the code:

For the table:

<?php
if (mysqli_num_rows($result) > 0) 
{
while($row = mysqli_fetch_array($result)) 
{       
 echo"<tr class=output2>";
 echo "<td>$row[1]</td>";
 echo "<td>$row[2]</td>";
 echo "<td>$row[3]</td>";
 echo "<td>$row[4]</td>";
 echo "<td>$row[5]</td>";
 echo "<td>$row[6]</td>";
 echo "<td>$row[7]</td>";
 echo "<td>$row[8]</td>";
 echo "<td>$row[9]</td>";
 echo "<td>$row[10]</td>";
 echo "<td>$row[11]</td>";
 echo "<td>$row[12]</td>";
 echo "<td><input type='text' name='pic' value='$row[17]'></td>";
 echo "<td><input type='text' name='comt' value='$row[18]'></td>";
 echo "<td><input type='text' name='stat' value='$row[19]'></td>";
 echo "<td><form name='update' method='POST'><input type='submit' name='save_btn' value='SAVE' style='font-size:1em;'/></form></td>";
 echo "<td><input type='hidden' name='idtxt' value='$row[0]'/></td>";
 echo "</tr>"; 
 }
}
else
{
echo '<h3>No result found! </h3><br>';
}
$con->close();

For Save button:

if(isset($_POST['save_btn']))
 {                  
 $query2="UPDATE invalid_invoice SET UpdateBy='".$_SESSION['login_user']."', UpdateDateTime=NOW(), PersoninCharge='".$_POST['pic']."', PIC_Comments='".$_POST['comt']."', Status='".$_POST['stat']."' WHERE ID='".$_POST['idtxt']."'";      
 $con->query($query2);
 $con->close();
 echo '<h3 class="datasuccess">Data successfully added!</h3>';

 }

I have edited your form. please have a look

<?php
if (mysqli_num_rows($result) > 0) 
{
while($row = mysqli_fetch_array($result)) 
{       
 echo"<form name='update' method='POST'><tr class=output2>";
 echo "<td>$row[1]</td>";
 echo "<td>$row[2]</td>";
 echo "<td>$row[3]</td>";
 echo "<td>$row[4]</td>";
 echo "<td>$row[5]</td>";
 echo "<td>$row[6]</td>";
 echo "<td>$row[7]</td>";
 echo "<td>$row[8]</td>";
 echo "<td>$row[9]</td>";
 echo "<td>$row[10]</td>";
 echo "<td>$row[11]</td>";
 echo "<td>$row[12]</td>";
 echo "<td><input type='text' name='pic' value='$row[17]'></td>";
 echo "<td><input type='text' name='comt' value='$row[18]'></td>";
 echo "<td><input type='text' name='stat' value='$row[19]'></td>";
 echo "<td><input type='submit' name='save_btn' value='SAVE' style='font-size:1em;'/></td>";
 echo "<td><input type='hidden' name='idtxt' value='$row[0]'/></td>";
 echo "</tr></form>"; 
 }
}
else
{
echo '<h3>No result found! </h3><br>';
}
$con->close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM