![](/img/trans.png)
[英]delete function delets the last record from table. js jquery.updated
[英]Only the last record in sql table is being updated from a form
我已經實現了LIKE功能,但是無論用戶單擊哪行的LIKE按鈕,表中的最后一條記錄都將得到更新。
我在這里和其他地方都看過與此類似的其他問題,並嘗試了建議的補救措施,但沒有任何效果。 任何幫助表示贊賞!
這將輸出表,並在LIKE按鈕的列中顯示:
<table class="table table-bordered">
<tr>
<th>Word ID</th>
<th>User ID</th>
<th>User Name</th>
<th>Word</th>
<th>Meaning</th>
<th>Example</th>
<th>Likes</th>
</tr>';
foreach ($data as $row)
{
echo '<tr>';
foreach ($row as $value)
{
echo '<td>';
echo $value;
echo '</td>';
}
echo '<td>
<form method="POST" action="'.$_SERVER["PHP_SELF"].'">
<input type="hidden" name="LIKE" value="'.$row['wordID'].'">
<input id="'.$row['wordID'].'" type="submit" class="btn btn-success" value="LIKE">
</form>
</td>';
echo '</tr>';
}
echo '</table>';
我的更新代碼是:
if($_POST['submit'])
{
$sql = "UPDATE vocab SET likes = likes+1 where wordID = '{$row['wordID']}'";
$stmt = $db->prepare($sql);
我已經嘗試了所有方法,包括添加諸如此類的javascript,以將所需的ID獲取到更新代碼。 沒運氣!
echo '<script type="text/javascript">
function submitForm(){
var selectButton = document.getElementById( "'.$row['wordID'].'" );
selectButton.click();
}
</script>';
選項1:我將更改:
<input name="like" value="'.$row['wordID'].'"
type="submit" class="btn btn-success">
並且:
if (!empty($_POST['like'])) {
$sql = "UPDATE vocab SET likes = likes+1 where wordID = '".$_POST['like']."'";
$stmt = $db->prepare($sql);
...
選項2:要顯示為按鈕值,請嘗試以下操作:
<input name="like['.$row['wordID'].']" value="LIKE"
type="submit" class="btn btn-success">
並在php文件中:
if (!empty($_POST['like'])) {
foreach ($_POST['like'] as $key => $value) {
$sql = "UPDATE vocab SET likes = likes+1 where wordID = '".$key."'";
$stmt = $db->prepare($sql);
...
}
}
選項3:
<input name="like_'.$row['wordID'].'" value="LIKE"
type="submit" class="btn btn-success">
和:
if (!empty($_POST)) {
foreach($_POST as $key => $value) {
if (substr($key, 0, 5) == 'like_') {
$sql = "UPDATE vocab SET likes = likes+1 where wordID = '".substr($key, 5)."'";
$stmt = $db->prepare($sql);
...
但是請謹慎使用SQL注入。 在執行查詢或使用參數執行准備好的查詢之前,應清理$ _POST ['like']值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.