使用一种形式。尝试让用户对数据库进行INSERT或UPDAT。
尝试var_dump();尝试过error_reporting(E_ALL);
$stmt = $connection->prepare("SELECT * FROM profiles WHERE user=?");
$stmt->bind_param("s", $user);
/* execute prepared statement */
$stmt->execute();
$result = $stmt->get_result();
if (isset($_POST['text']))
{
if ($result->num_rows)
$text = ($_POST['text']);
$birthday = ($_POST['birthday']);
$gender= ($_GET['gender']);
$stmt = $connection->prepare("UPDATE profiles SET
user=?, text=?, birthday=?, gender=?
WHERE user=?");
$stmt->bind_param("ssss", $user, $text, $birthday, $gender);
/* execute prepared statement */
$stmt->execute();
}
//using bound paramaters for profile
else
{
$stmt = $connection->prepare("INSERT INTO profiles
(user, text, birthday, gender)
VALUES (?,?,?,?)");
$stmt->bind_param("ssss", $user, $text, $birthday, $gender);
/* execute prepared statement */
$stmt->execute();
/* close statement and connection */
$stmt->close();
}
没有收到有关性别的未定义变量(单选按钮),但是该变量消失了。数据不插入数据库。有时我在列中看到null。
答案 0 :(得分:0)
UPDATE
查询有5个占位符,但是您仅在bind_param
中绑定了4个参数。最后需要另外一个$user
。
$stmt = $connection->prepare("UPDATE profiles SET
user=?, text=?, birthday=?, gender=?
WHERE user=?");
$stmt->bind_param("sssss", $user, $text, $birthday, $gender, $user);
但是无需设置user
,因为您只是将其设置为相同的值,因此将其更改为:
$stmt = $connection->prepare("UPDATE profiles SET
text=?, birthday=?, gender=?
WHERE user=?");
$stmt->bind_param("ssss", $text, $birthday, $gender, $user);
如果user
列是唯一键,则可以使用INSERT ... ON DUPLICATE KEY UPDATE
将所有代码组合成一个查询。
$stmt = $connection->prepare("
INSERT INTO profiles (user, text, birthday, gender)
VALUES (?,?,?,?)
ON DUPLICATE KEY UPDATE text = VALUES(text), birthday = VALUES(birthday), gender = VALUES(gender)");
$stmt->bind_param("ssss", $user, $texxt, $birthday, $gender);
答案 1 :(得分:0)
您的第二个if语句缺少括号,以及@Barmar提到的其他错误。建议您在弄清楚代码为何无法正常工作之后,再查看INSERT ... ON DUPLICATE KEY UPDATE
语句。
为什么要向表中插入null
值是一个更明显的原因,是因为首先要设置变量$text
,$birthday
,$gender
if
语句的一半,它将不会执行,并且将是未初始化的变量,并且在null
之后都等于else
。
如果代码未输出任何错误,请使用echo或print函数让您自己了解代码的到达位置,这将使您知道代码在哪里停止工作。您还可以在选择的IDE中使用php调试插件,这将帮助您更快地解决问题。
这是您的代码版本,其中进行了一些修复和调整。花点时间看清楚@Barmar提到的内容,并确保在所有if语句中添加括号,甚至是一行语句,这样您就不会在学习时感到困惑。
/** Did the user give us a name to use? If not, resubmit the form with a user name */
if (isset($_POST['user']))
{
$user = $_POST['user']; /** Remember to Sanitize & Validate Structure of User Inputs */
$text = $_POST['text']; /** Remember to Sanitize & Validate Structure of User Inputs */
$birthday = $_POST['birthday']; /** Remember to Sanitize & Validate Structure of User Inputs */
$gender = $_POST['gender']; /** Remember to Sanitize & Validate Structure of User Inputs */
$stmt = $connection->prepare("SELECT * FROM profiles WHERE user=?");
$stmt->bind_param("s", $user);
/* execute prepared statement */
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows)
{
$stmt = $connection->prepare("UPDATE profiles SET
text=?, birthday=?, gender=?
WHERE user=?");
$stmt->bind_param("ssss", $text, $birthday, $gender, $user);
/* execute prepared statement */
if ($stmt->execute()) {
echo "Record updated successfully";
} else {
echo "Error updating record: " . $stmt->error;
}
}
else
{
//using bound paramaters for profile
$stmt = $connection->prepare("INSERT INTO profiles
(user, text, birthday, gender)
VALUES (?,?,?,?)");
$stmt->bind_param("ssss", $user, $text, $birthday, $gender);
/* execute prepared statement */
$stmt->execute();
}
/* close statement and connection */
$stmt->close();
}
else
{
echo "No User Inputs - Go ahead and submit the form";
}