简体   繁体   中英

PHP SQL Table Edit row with inline link

I have a table that is working perfectly fine, however, it currently sends the user to a phpmyedit screen no matter which edit link is clicked. What I want to accomplish is clicking the inline link to directly edit that particular row or 'id'. Here is an example of my code:

$result = mysqli_query($con,"SELECT * FROM `TABLE_NAME` ORDER BY ID DESC");
echo "<table>";
echo "<table border='1' style='margin:200px 200px 500px 50px;'>
<tr>
<th>ID</th>
<th>TABLE HEADER</th>
<th>TABLE HEADER</th>
<th>TABLE HEADER</th>
<th>TABLE HEADER</th>
<th>TABLE HEADER</th>
<th>TABLE HEADER</th>
<th>TABLE HEADER</th>
<th>TABLE HEADER</th>
<th>Edit</th>
</tr>";

while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td>" . $row['ID'] . "</td>";
  echo "<td>" . $row['rowName'] . "</td>";
  echo "<td>" . $row['rowName'] . "</td>";
  echo "<td>" . $row['rowName'] . "</td>";
  echo "<td>" . $row['rowName'] . "</td>";
  echo "<td>" . $row['rowName'] . "</td>";
  echo "<td>" . $row['rowName'] . "</td>";
  echo "<td>" . $row['rowName'] . "</td>";
  echo "<td>" . $row['rowName'] . "</td>";
  echo "<td><a href='http://phpmyedit.com/editexample".$row['']."'>Edit</a></td><tr>";
  echo "</tr>";
  }
echo "</table>";

?>

So basically I just need to allow the edit link to directly edit that id in the row it was clicked in.

**EDIT: I'm using PHPMyEdit to edit/update the data in the database table, and would like to continue using PHPMyEdit.

Thanks in advance.

echo "<td><a href='edit.php?id=".$row['ID']."'>Edit</a></td><tr>";

In edit.php you have to create form which values will be loaded from database:

<?php 

if (isset($_POST['fieldName']){
  mysqli_query($con, 'UPDATE `TABLE_NAME` SET fieldName='.$_POST['fieldName'].' WHERE ID='.intval($_GET['id']);
  exit;
}

$row = mysqli_fetch_assoc(mysqli_query($con, 'SELECT * FROM `TABLE_NAME` WHERE ID='.intval($_GET['id']))); ?>
<form method="POST">
<input type="text" name="fieldName" value="<?php echo $row['fieldName'] ?>">
<!--    -->
<input type="submit" value="save edit">
</form>

You need improve this pseudocode with data filtering, add more fields, include db connection etc.

I think you need a javascript code to trigger Edit mode.

But first, you need to give your table fields unique IDs while executing the while loop.

$counter=1;
while($row = mysqli_fetch_array($result))
  {
  echo "<tr>";
  echo "<td><div id='ID_$counter'>" . $row['ID'] . "</div></td>";
  echo "<td><div id='TH1_$counter'>" . $row['rowName'] . "</div></td>";
  echo "<td><div id='TH2_$counter'>" . $row['rowName'] . "</div></td>";
...
  echo "<td><button onclick='triggerEdit($counter)'>Edit this row</button></td><tr>";

$counter++;
}

then using javascript create the edit and update functions:

    function triggerEdit(rowNumber) {
            document.getElementById('id_'+rowNumber).innerHTML = "<input type=text id='SOMEIDx'><button onclick='update(SOME_IDx)'>update</button>";
            document.getElementById('TH1_'+rowNumber).innerHTML = "<input type=text id='SOMEIDx2'><button onclick='update(SOME_IDx2)'>update</button>";
// don't forget to assign the IDs
        }

If you want the old values, you can access them using document.getElementById().innerHTML before you change them.

Finally, Construct an update function that sends the data using AJAX, given the ID of the field.

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