[英]How to update a MySQL column based on a checkbox input
我是這個論壇的新成員。 以及使用PHP進行編碼,以便作為新手,我希望得到您的所有幫助。
我面臨的問題如下。
我有一個帶有以下字段的MySQL表“ Announce”
id
----------
advert
----------
date
----------
file
----------
approv
----------
從頁面填充的數據-在其中所有列均獲得其值(批准除外)的值。 要填充該字段,有一個新的PHP頁面。 批准字段根據其復選框的勾號獲得“批准”值
我面臨的問題是,我無法讀取顯示ID的文本框的值並獲取相應的復選框值,以使特定記錄獲得批准並更新到MySQL表。
我寫的代碼如下
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org
/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Annpouncements | Pending List</title>
<style type="text/css">
.textinput {
height: 20px;
width: 20px;
border-top-width: 0px;
border-right-width: 0px;
border-bottom-width: 0px;
border-left-width: 0px;
border-top-style: none;
border-right-style: none;
border-bottom-style: none;
border-left-style: none;
}
</style>
</head>
<body>
<table width="800" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td><table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td> </td>
</tr>
<tr>
<td height="164"><form id="form1" name="form1" method="post" action="">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
<tr>
<td width="11%" height="31" align="center"><label>Id</label></td>
<td width="15%" height="31" align="center"><label>Date</label></td>
<td width="52%" align="center"><label>Title</label></td>
<td width="22%" align="center"><label>Status</label></td>
</tr>
<?php
//Open the table announce from the database and list date in descending order
$result = mysql_query("SELECT * FROM announce ORDER by date DESC" )or
die(mysql_error());
//Define a variable to get the rows of the table
$ann = mysql_fetch_array($result);
//Define a variable to get the no of rows
$num = mysql_num_rows($result);
$i=0;
while($i<$num) {?>
<?php $approv[$i]= mysql_result($result,$i,"approv"); ?>
<tr>
<?php if($approv[$i] !== "approved"){?>
<td height="36" align="center"><input name="id" type="text"
class="textinput" id="id" value="<?php echo mysql_result($result,$i,"id");
?>" /></td>
<?php $ids = mysql_result($result,$i,"id");
//$inp = $_POST["id"][$i];
//echo 'Input value : ' .$inp. '<br/>' ?>
<td height="36" align="center"><label>
<?php echo mysql_result($result,$i,"date"); ?></label></td>
<td align="center"><label><?php echo mysql_result($result,$i,"advert");
?></label></td>
<td align="center"></label><input type="checkbox" name="approv[]" />
<label for="approv"></label></td>
<?php $idan = mysql_query("SELECT * FROM announce WHERE id == $ids"); ?>
<?php
if (isset($_POST['button']))
{
$apprv = $_POST["approv"];
//echo 'id = '.$ids.'<br/>';
$how_many = count($apprv);
//echo 'Row selected' .$how_many. '<br/>';
foreach ($_POST['approv'] as $apprValue)
$txtvalue[] = $_POST[$apprValue];
echo 'txtvalue = ' .$txtvalue. '<br/>';
mysql_query("UPDATE announce SET approv = 'approved'WHERE id ==
$idan ");
}
}
?>
<?php } ?>
</tr>
<?php
$i++;
}
?>
<tr>
<td height="44"> </td>
<td> </td>
<td align="center"> </td>
<td align="center"><input type="submit" name="button" id="button"
value="Submit" /></td>
</tr>
</table>
</form></td>
</tr>
</table></td>
</tr>
</table>
因此,請幫助我獲得正確的解決方案,以僅針對已檢查的那些行使用“已批准”值更新表。
期待您的寶貴幫助
我在您的查詢中發現的一些問題
這個
SELECT * FROM announce WHERE id == $ids
應該
SELECT * FROM announce WHERE id = '$ids'
和這個
UPDATE announce SET approv = 'approved'WHERE id == $idan
應該
UPDATE announce SET approv = 'approved' WHERE id = '$idan'
您的復選框也沒有value屬性
<input type="checkbox" name="approv[]" value="<echo your table row id here>" />
然后使用
foreach($_POST['approv'] as $apprValue)
{
mysql_query(UPDATE announce SET approv = 'approved' WHERE id = '$apprValue');
}
完全注意,請不要再使用mysql_ *函數。 他們將很快被棄用。 最好去mysqli
或PDO
好的,所以這里有很多問題,但是我要扼殺這個問題,因為代碼行會生成您的復選框。 特別是以下位:
<input name="id" type="text" class="textinput" id="id" value="<?php echo mysql_result($result,$i,"id"); ?>" />
<input type="checkbox" name="approv[]" />
由於多種原因,這並不是創建所需信息的可靠方法。
$_POST['id']
僅包含一個值。 它不會創建ID到批准狀態的映射,這似乎是您的意圖。 $_POST['approv']
將是一個索引為零的數組,因此您沒有在不保留id
與循環計數的映射的情況下將復選框與id
相關聯的有用方法。 (但是,如果要執行此操作,只需使用PHP提供的構造即可。) 好的,現在我們知道我們需要進行一些更改。 首先,我們將嘗試使復選框起作用。 但是我們仍然會遇到一些代碼結構的問題。 尤其是因為看起來您根本不了解請求在PHP中的工作方式。
首先,我們需要一種方法來確保我們知道每個ID的批准狀態。 這實際上很容易補救。 我們可以更改:
<input type="checkbox" name="approv[]" />
至
<input type="checkbox" name="approv[]" value="<?= $id ?>" />
這樣,當您遍歷$_POST['approv']
,實際上會得到一個ID,而不僅僅是偏移量。 這將使您在底部保持相同的for循環。
但是還有問題
沒有理由在渲染循環中查看$ _POST( while($i<$num)
)。 使用PHP構造對您有利。 foreach($result_rows as $row)
比您的代碼更慣用和可讀:
//Define a variable to get the rows of the table
$ann = mysql_fetch_array($result);
//Define a variable to get the no of rows
$num = mysql_num_rows($result);
$i=0;
while($i<$num) {?>
... // do work
... // Do something with $_POST. <-- wrong!!!
<? } ?>
應該更像是:
if (empty($_POST)) {
// Only run the query when we need to display information
$result = mysql_query("SELECT * FROM announce ORDER by date DESC") or die(mysql_error());
//Define a variable to get the rows of the table
$result_rows = mysql_fetch_array($result);
foreach($result_rows as $row) { ?>
... // Display form
<? }
} else {
$success = array();
foreach($_POST['approv'] as $approved_id) {
// Ideally we would combine all of these updates into one statement
$stmt = mysqli_prepare($link, "UPDATE announce SET approv = 'approved' WHERE id = ?i");
mysqli_stmt_bind_params($stmt, $approved_id);
// You then can look over the $success array to see if any failed.
$success[$approved_id] = mysqli_stmt_execute($stmt);
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.