table: test1
----------
|id |name|
----------
|46 |BOB |
----------
table: test2
------------------------
|id |name_id |job |
------------------------
|133 |46 |NOJOB |
------------------------
|134 |46 |NOJOB |
------------------------
|135 |46 |NOJOB |
------------------------
Hello, i have a problem to update multiple rows on the “test2.job” respectively based on the “test2.name_id which is resulted from the mysql_insert_id(); function from “test1.id” in the INSERT query and my table structure as above.
I have the form that will populated depends on how much record i have in “test2” table with a condition of “WHERE name_id = ‘”.$_GET[‘id’].”‘” and i have specified “localhost/rfps/ztransposto.php?id=46” in the browser url address and the form will shows as what is in the test2 table above.
The thing is when i try to change and update the first and the second row it will update nothing as it only will update if i change the last row value and updates the other two rows both first and second row with the same value. Below are the source code:
1-update form page
<?php require_once('Connections/rfps.php'); ?>
<?php
//initialize the session
if (!isset($_SESSION)) {
session_start();
}
?>
<html>
<head>
</head>
<body>
<?php
$sql = "SELECT * FROM test2 WHERE name_id = '".$_GET['id']."'";
$result = mysql_query($sql) or die($sql."<br/><br/>".mysql_error());
$i = 0;
echo '<table width="50%">';
echo '<tr>';
echo '<td>ID</td>';
echo '<td>Name ID</td>';
echo '<td>Job</td>';
echo '</tr>';
echo "<form name='form_update' method='post' action='ztranspostop.php'>\n";
while ($job = mysql_fetch_array($result)) {
echo '<tr>';
echo "<td>{$job['id']}<input type='hidden' name='id[$i]' value='{$job['id']}' /></td>";
echo "<td>{$job['name_id']}<input type='hidden' name='name_id[$i]' value='{$job['name_id']}' /></td>";
echo "<td><input type='text' size='40' name='job[$i]' value='{$job['job']}' /></td>";
echo '</tr>';
++$i;
}
echo '<tr>';
echo "<td><input type='submit' value='submit' /></td>";
echo '</tr>';
echo "</form>";
echo '</table>';
?>
</body>
</html>
2-update query page
<?php require_once('Connections/rfps.php'); ?>
<?php
//initialize the session
if (!isset($_SESSION)) {
session_start();
}
$size = count($_POST['job']);
$i = 0;
while ($i < $size) {
$job= $_POST['job'][$i];
$name_id = $_POST['name_id'][$i];
$query = "UPDATE test2 SET job = '$job' WHERE name_id = '$name_id'";
mysql_query($query) or die ("Error in query: $query");
echo "$job<br /><br /><em>Updated!</em><br /><br />";
++$i;
}
?>
Can anyone point out which part of the codes i got it wrong? Thanks in advance!
I think you are missing the connection between the test2.id field in the form and using it in the update query. In fact, you probably don’t need the
WHERE name_id =clause if you include theWHERE id =instead. This completes the association between a row in your form (the values of $i on that page) and the particular job row being updated (the values of $i on the subsequent page). Otherwise, you are only updating based on an attribute which they will all have in common (name_id) 🙂