Sizwe
Sizwe

Reputation: 108

Update table if user clicks edit link

Im trying to get my query to update upon user click This one here works but it doesnt wanna update I Says Query Empty

I edited the code as follows

  if ($action == "edit"){
if ($_SERVER['REQUEST_METHOD'] == 'POST') { 



    $first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];
    $email = $_POST['email'];

    $result=mysql_query("UPDATE user SET first_name='$first_name',last_name='$last_name',email='$email' WHERE id=$id");
     $sql = mysql_query($result) or die (mysql_error());
}

$sql=mysql_query("select * from user where id='$id'");

while ($row=mysql_fetch_array($sql)){                                             
    $id = $row['id'];
    $first_name = $row['first_name'];
    $last_name = $row['last_name'];
    $email = $row['email'];


    echo("<form name='edit' method='post' action='?action=edit'>");
    echo("<input type='hidden' name='?action=edit'>");
    echo("<table class=main cellspacing=0 cellpadding=5 >");
    echo("<tr><td>Name: </td><td align='right'><input type='text' name='first_name' value='$first_name'></td></tr>");
    echo("<tr><td>Surname: </td><td align='right'><input type='text'  name='last_name' value='$last_name'></td></tr>");
    echo("<tr><td>Email: </td><td align='right'><input type='text' name='email' value='$email'></td></tr>");
    echo("<tr><td></td><td><div align='right'><input type='submit'></div></td></tr>");
    echo("</table>");
}
       }  


echo("<br>");                                                                                                                                                                                                                                                                                                               
echo("</form>");

I added $sql = mysql_query($result) or die (mysql_error());

the above line shows what time of error the query encounted while executing... Any Ideas as to why the query is empty and wheres there is data in the table?

This Code will run upon the clicking of this link

echo"<a href='test.php?action=edit&id=$id'>Edit</a>";

Upvotes: 0

Views: 599

Answers (2)

Srikanth Kolli
Srikanth Kolli

Reputation: 912

if (isset($_POST["hdn_edit"]) && $_POST["hdn_edit"]=="edit")
{
$first_name = $_POST['first_name'];
$last_name = $_POST['last_name'];
$email = $_POST['email'];
$id=$_POST["hdn_id"];
$result=mysql_query("UPDATE user SET first_name='$first_name',last_name='$last_name',email='$email' WHERE id=$id");   
}
$sql=mysql_query("select * from user where id='$id'");

while ($row=mysql_fetch_array($sql)){                                             
$id = $row['id'];
$first_name = $row['first_name'];
$last_name = $row['last_name'];
$email = $row['email'];
echo("<form name='edit' method='post' action=''>");
echo("<input type='hidden' name='hdn_edit' value='edit'>");
echo("<input type='hidden' name='hdn_id' value='$id'>");
echo("<table class=main cellspacing=0 cellpadding=5 >");
echo("<tr><td>Name: </td><td align='right'><input type='text' name='first_name' value='$first_name'></td></tr>");
echo("<tr><td>Surname: </td><td align='right'><input type='text'  name='last_name' value='$last_name'></td></tr>");
echo("<tr><td>Email: </td><td align='right'><input type='text' name='email' value='$email'></td></tr>");
echo("<tr><td></td><td><div align='right'><input type='submit'></div></td></tr>");
echo("</table>");
}
 echo("<br>");                                                                                                                                                                                                                                                                                                               
 echo("</form>");

Upvotes: 0

Zim84
Zim84

Reputation: 3497

You do not catch the value of id. As you have it written in the URL, it is transmitted via GET, the rest of your data is via POST. So you either have to transmit as a POST var as well:

<input type="hidden" name="id" value="$id"/>

And catch it via PHP:

$id = $_POST['id'];

or let it the way it is and catch it as a GET-variable:

$id = $_GET['id'];

BUT: this code is horribly insecure. I advise you to search for sql injection and similar topics..

Upvotes: 1

Related Questions