从表单输入到SQL查询获取变量

时间:2015-12-31 22:28:43

标签: php html mysql

我在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">

1 个答案:

答案 0 :(得分:0)

你应该添加新的隐藏的html输入,名称是&#34; id&#34;而价值来自数据库