我需要使用复选框将HTML表的特定行插入到PHP数组中。由于某种原因,只有第一列进入数组。我写的代码是:
if(isset($_GET["key"])) {
$user_key = $_GET["key"];
if ($stmt = $mysqli->prepare("SELECT sandwich.sname, description, size, price FROM sandwich JOIN menu on sandwich.sname = menu.sname WHERE description like ?")) {
$stmt->bind_param("s", $search_string);
$search_string= "%".$user_key."%";
$stmt->execute();
$stmt->bind_result($sname, $description, $size, $price);
// Printing results in HTML
echo "The menu for the given key word is.<br>";
echo "<table border = '1'>\n";
while ($stmt->fetch()) {
echo "<tr>";
echo "<td>$sname</td><td>$description</td><td>$size</td><td>$price</td><td><input type='checkbox' name='order[]' value='$sname'|'$size'|'$price'></td>";
echo "</tr>\n";
}
echo "</table>\n";
$stmt->close();
$mysqli->close();
}
我在数组顺序[]中列出了所有的sname,size和price值,但只有sname才会进入它。有人请说明代码有什么问题。我对PHP完全不熟悉。
答案 0 :(得分:0)
看起来你有几行错误的方法:
$stmt->bind_param("s", $search_string);
$search_string= "%".$user_key."%";
应该是:
$search_string= "%".$user_key."%";
$stmt->bind_param("s", $search_string);
此外,您的sandwich
和menu
表之间的任何字段名称是否相同?我建议修改您的查询以说明您所指的表:
if ($stmt = $mysqli->prepare("SELECT sandwich.sname, <tablename>.description, <tablename>.size, <tablename>.price FROM sandwich JOIN menu on sandwich.sname = menu.sname WHERE <tablename>.description like ?")) {