有没有办法在用户单击链接时更新 MySQL 数据库中的数据? 我正在创建一个投票应用程序。它允许用户存储问题以便立即使用或供以后使用。当用户查看他创建的问题时,他/她可以单击一个链接,该链接将更新数据库中的列,该列将根据问题 ID 存储 1 或 0。 (1表示该问题可以投票,0则相反)。我已经编写了从数据库检索问题的代码。现在我正在尝试编写通过单击链接来更新问题数据的代码。
我不知道如何获取用户单击链接的问题 ID,以便我可以更新数据库。 如果我的解释不清楚,请原谅。英语是我的第二语言,我正在努力更好地沟通。
最佳答案
你可以这样做:
<?php
$username = $_COOKIE['username'];
$sql = "SELECT * FROM QUESTIONS WHERE username ='$username'";
$result = mysql_query($sql) or die(mysql_error());
if (mysql_num_rows($result) > 0) {
//---------- new
if(isset($_GET['voteID']))
{
$viteID = (int) $_GET['voteID'];
// your update query
}
//--------
?>
<table width="50%" cellpadding="5" cellspacing="0" border="1">
<tr><td><b>Question ID</b></td><td><b>Question</b></td><td><b>Active/Inactive</b></td></tr>
<?php
$html = '';
while ($row = mysql_fetch_row($result)) {
$html .= '<tr>';
$html .= '<td>' . $row[0] . '</td>';
$html .= '<td>' . $row[1] . '</td>';
$activity = $row[3];
// ---- new
$html .= '<a href="'.$_SERVER['PHP_SELF'].'?id='.$row[0].'">Vote Up</a>';
//-------
//if active = 1, do the following
if ($activity == 1) {
$html .= '<td>' . '<a href=markAsInActive.php>Active</a>' . '</td>';
} else {
$html .= '<td>' . 'Inactive' . '</td>';
}
$html .='</tr>';
}
echo $html;
?>
</table>
<?php
}
?>
关于php - 使用 PHP 和 HTML 单击链接将数据更新到 mysql 数据库,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16491600/