我在db中有一个名为recall
的TABLE。我在该表中有一个名为RECALL
的列。
我的SQL查询需要使用输入RECALL
的值$row['RECALL']
更新名为$row['id']
的列。
如何从此行获取$row[RECALL]
和$row[id]
:
echo "<tr><td>Recall Position: </td><td valign = 'bottom'><input type='text' value='" . $row['RECALL'] . "' name='" . $row['id'] . "' /></td> </tr>;
进入我的SQL查询?
以下完整代码:
<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">
答案 0 :(得分:0)
你应该添加新的隐藏的html输入,名称是&#34; id&#34;而价值来自数据库