1

I have a TABLE in db named recall. I have a column in that table called RECALL.

My SQL query needs to update the column named RECALL with value of the input $row['RECALL'] where the name of input is $row['id'].

How do I get the $row[RECALL] and $row[id] from this line:

echo "<tr><td>Recall Position: </td><td valign = 'bottom'><input type='text' value='" . $row['RECALL'] . "' name='" . $row['id'] . "' /></td> </tr>;

Into my SQL query?

Full code below:

<form method="post" action="<?php $_PHP_SELF ?>">
<?php
ini_set("display_errors",1);
require_once '../includes/db2.php';
if(! $connection )
{
  die('Could not connect: ' . mysql_error());
}
$sql = "SELECT id,ElectoralDistrict, Party, 
       Candidate, RECALL
FROM recall
WHERE Elected='yes'";

mysql_select_db('newdb');
$retval = mysql_query( $sql, $connection );
if(! $retval )
{
die('Could not get data: ' . mysql_error());
}
while($row = mysql_fetch_array($retval, MYSQL_ASSOC))
{
echo "<table><tr><td>Electoral District: </td><td>                 {$row['ElectoralDistrict']}  </td></tr> ";
 if ($row['Party'] == "New Democrat") {
echo "<tr><td>Party Affiliation: </td><td><SPAN STYLE='background-color:    #ff6600'> {$row['Party']} </span></td></tr> ";
} elseif ($row['Party'] == "Liberal") {
echo "<tr><td>Party Affiliation: </td><td><SPAN STYLE='background-color:     #ff6666'> {$row['Party']} </span></td></tr> ";
} elseif ($row['Party'] == "Progressive Conservative") {
echo "<tr><td>Party Affiliation: </td><td><SPAN STYLE='background-color:    #4747d1'> {$row['Party']} </span></td></tr> ";
} else {
echo "<tr><td>Party Affiliation: </td><td><SPAN STYLE='background-color: #ffffb3'> {$row['Party']} </span></td></tr> ";
}
echo "<tr><td>Candidate / MPP: </td><td> {$row['Candidate']} </td></tr> ";
echo "<tr><td>Recall Position: </td><td valign = 'bottom'><input      type='text' value='" . $row['RECALL'] . "' name='" . $row['id'] . "' /></td>    </tr></table> ".
     "--------------------------------<br>";
}

echo "Fetched data successfully\n <br>";
//mysql_close($connection);
?>

<?php // update
ini_set("display_errors",1);
//require_once '../includes/db2.php';
if(! $connection )
{
  die('Could not connect: ' . mysql_error());
}
if(isset($_POST['update']))
{
$recallp = $row['RECALL'];
$rowid = @$_POST['id']; 
echo "why is this empty", $rowid ;   // this line to see if rowid empty

$sql = "UPDATE recall
        SET RECALL = '$recallp'
        WHERE ID = '$rowid' ";

mysql_select_db('newdb');
$retval = mysql_query( $sql, $connection );
if(! $retval )
{
die('Could not change: ' . mysql_error());
}
else
{
echo "Recall Data Updated<br><br>";
}
}

?>

<input name = "update" type = "submit" id = "update" value = "Update">
Remi Guan
  • 21,506
  • 17
  • 64
  • 87
TO Man
  • 21
  • 3

1 Answers1

0

You should add new hidden html input with name is "id" and value is is from database

ytdm
  • 1,069
  • 1
  • 12
  • 15