[英]Update or Insert data with an if else statement
使用一种形式。 试图让用户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。
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);
你的第二个if语句缺少括号,除了@Barmar提到的其他一些错误。 我建议您在弄清楚代码无法正常工作的原因之后,查看INSERT ... ON DUPLICATE KEY UPDATE
语句。
你在表中插入null
值的一个更明显的原因是因为你在if
语句的前半部分设置了变量$text
, $birthday
, $gender
,这些变量将不会被执行并且将是未初始化的变量并且在else
之后都等于null
。
如果您的代码没有输出任何错误,请使用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";
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.