hama
hama

Reputation: 57

specify updated record ID after $_POST in php

I have a page in Php with multiple row, at the right side of each row there is a save button, after editing any row when click on the save how to post the specified record ID to another page to update this record on MySQL.

thank you

Upvotes: 0

Views: 61

Answers (2)

hama
hama

Reputation: 57

this is my code

editing.php

$query="SELECT * FROM emp";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();
?>

<div align = "center">
<table cellspacing="0" border = "2" cellpadding = "4" class="page-break" width="100%">
<form method="POST" action="update.php">
<tr>
<th></th>
<th><font size = "3"  >name</font></th>
<th><font size = "3"  >ID</font></th>

</tr>

<?php
$i=0;
while ($i < $num) {
$f1=mysql_result($result,$i,"ID_e");
$f2=mysql_result($result,$i,"name");
?>

<tr>
<td ><input type="submit" value="save" name="B1" ></td>
<td align = "center"><input type="text" size="8px" value = "<?php echo $f2; ?>" name="T2"></td>
<td align = "center"><input type="text" size="8px" value = "<?php echo $f1; ?>" name="T1"></td>

</tr>
<?php
$i++;
}
?>

Update.php

if (isset($_POST['B1'])) {

    $id = $_POST['T1'];         //.... how to get updated row ID ?
    $name = $_POST['T2'];


    mysql_query("UPDATE emp SET ID_e = '$id', name = '$name' WHERE ID_e = '$id' ");
}


?>
<script type="text/javascript">
window.location = 'editing.php'; 

Upvotes: 0

arkascha
arkascha

Reputation: 42925

When creating the table in the "page" by means of php you can take care to create each row as a html form and to define the "save" button as a submit button. Then clicking the button will result in the form getting sent as a request.

Another approach is to use javascript for this: you can add a handler function to the click event of the button. Inside the handler you know which button (so which row) has been clicked and can use a selector to gather all vallues from the row. You can then make either a post request to some script, or, even more elegant, make a background ajax request. That would allow just to confirm the success of the save action without having to reload the whole page.

Many examples for both approaches can be found here on StackExchange or by a simple google search. Also a look into the php documentation is a very good starting point: http://php.net/manual/en/tutorial.forms.php

Upvotes: 1

Related Questions