1

I need a form that allows a user to edit data from an SQL database.

The main page of my form contains a table where any available data is shown and, also, there are edit and delete options available. Sample:

{
$row=mysql_fetch_array($result);
echo "<tr>";
echo "<td>".$row["name"]."</td>";
echo "<td>".$row["occuptation"]."</td>";
echo '<td><a href="edit.php"><img src="edit.png"/></a></td>';
echo '<td><a href="delete.php"><img src="delete.png"/></a></td>';
echo "</tr>";
}

When the user clicks the edit.png I need it to direct to edit.php where the data from the whole row selected (whichever row the user chooses to click edit or delete from) shows up in a form (like default values). I have no idea how to achieve this.

Any help is appreciated! Please tell me if i'm being confusing and I will try to explain further.

6
  • so what's the problem? selecting the data? displaying the data? I always use the same page and form for add and edit, clean and easy.
    – user557846
    Jun 13, 2012 at 2:59
  • I don't think I understand. Unless you're trying to go dynamic like Dagon said--what's wrong with just a link that goes to "edit.php"? Jun 13, 2012 at 3:01
  • He needs the data to be passed as well, aka name and occupation Jun 13, 2012 at 3:02
  • Ah, yes. And you have a good solution. Jun 13, 2012 at 3:03
  • @Dagon Unfortunately, I don't know how to do this in one page. I'm a beginner. Jun 13, 2012 at 3:54

3 Answers 3

2

What you'll want to do is include an identifier as a query string parameter in the edit and delete links:

echo '<td><a href="edit.php?id=' . $row["id"] . '"><img src="edit.png"/></a></td>';
echo '<td><a href="delete.php?id=' . $row["id"] . '"><img src="delete.png"/></a></td>';

(Of course, I'm assuming the name of your identifier. But you get the idea.)

This will tell those pages which record they should display. The pages would get the identifier with:

$_GET["id"]

A few things you'll want to consider:

  • Make sure to check for a proper identifier value in $_GET["id"] before you try to do anything with it. Never assume it's valid without checking.
  • DO NOT simply concatenate the value into a SQL string. Use something like PDO to build queries with parameters. What you're trying to avoid here is something called a SQL injection vulnerability.
  • Make sure the user is permitted to access the record in question for the operation in question (edit or delete) before you display anything. Never assume that the link came directly from something your code output on a page. Anybody can just as easily manually change the id value in the URL.
0
0

What you could do is include the data in the href of the a tag

{
    $row=mysql_fetch_array($result);
    echo "<tr>";
    echo "<td>".$row["name"]."</td>";
    echo "<td>".$row["occuptation"]."</td>";
    echo '<td><a href="edit.php?name={$row['name']}&occupation={$row['occupation']}"><img src="edit.png"/></a></td>';
    echo '<td><a href="delete.php"><img src="delete.png"/></a></td>';
    echo "</tr>";
}

The same could be done for delete.php if data also needs to be sent to that script.

1
  • 1
    Agree, although the row id would ideally be better.
    – Steve H
    Jun 13, 2012 at 3:06
0
 $selectQuery = mysqli_query($database, "SELECT * FROM products");
   if($selectQuery){
              while ($row =mysqli_fetch_array($selectQuery)){
              <td><a href="delete.php?editid=<?php echo $row['id'];?>">
              <img src="delete.png"/></a></td><?php } }?>

echo '<td><a href="delete.php?id=' . $row["id"] . '"><img src="delete.png"/></a></td>';
1
  • 1
    Welcome to Stack Overflow, @Nazrul Islam. Perhaps some explanatory text would assist in relating how this code answers the OP's question.
    – Degan
    Jul 5, 2017 at 14:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.