我有一个奇怪的问题,我希望它是我忽略的愚蠢问题。
我收到一个错误,通常在 mysql 表中缺少列或拼写错误的 php 变量时发生:“‘字段列表’中存在未知列‘Bob’”...
除了“Bob”不是列的名称,它是我试图分配给该行的值。为什么 php 会混淆两者?
这是我的 php 函数,我认为这是错误的位置:
function recordGuest($id,$fname,$lname,$dinner){
$conn = connect("wedding");
$guest_query = "INSERT INTO guest (fname,lname,person_id)
VALUES (".$fname.",".$lname.",".$id.");";
mysql_query($guest_query,$conn) or die(mysql_error());
$guest_id_query = "SELECT id FROM guest WHERE person_id = ".$id.";";
$guest_id_result = mysql_query($guest_id_query,$conn) or die(mysql_error());
$guest_id = "";
while($row = mysql_fetch_array($guest_id_result)){
$guest_id = $row["id"];
}
$guest_dinner_query = " INSERT INTO guest_dinner (dinner_id,guest_id)
VALUES (".$dinner.",".$guest_id.");";
mysql_query($guest_dinner_query,$conn) or die(mysql_error());
}
下面是处理表单并执行上述函数的 php 代码:
<?php
include("functions.php");
$code = $_POST["code"];
$type = $_POST["type"];
$people = getPeople($code);
$ids = $people["ids"];
$email= "";
for($i = 0; $i < count($ids); $i++){
$response = $_POST["response_".$i];
$dinner = $_POST["dinner_".$i];
recordResponse($ids[$i],$response);
if($dinner != "null"){
recordDinner($ids[$i],$dinner);
}
if($type == 3){
$guest_responses = $_POST["guest_response_".$i];
$guest_fname = $_POST["guest_fname_".$i];
$guest_lname = $_POST["guest_lname_".$i];
$guest_dinner_response = $_POST["guest_dinner_response_".$i];
if($guest_dinner_response != "null"){
recordGuest($ids[$i],$guest_fname,$guest_lname,$guest_dinner_response);
}
}
}
?>
这是我的“ guest ”mysql 表的样子:
guest
id int auto inc (primary key)
fname varchar
lname varchar
person_id int
如有任何帮助,我们将不胜感激。
最佳答案
在尝试插入字符串值之前,将字符串括在单引号中,即“Bob”
关于php - 'Bob' 中的未知列 'field list' ... php 或 mysql 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3852134/