gpt4 book ai didi

php - 错误插入 ON duplicatekey UPDATE 函数

转载 作者:行者123 更新时间:2023-11-30 22:12:26 24 4
gpt4 key购买 nike

我在数据库中有一个表“fuser”,其中提到了各自的字段。我有带有字段输入区域的 .php 表单。我尝试了 INPUT INTO & DUPLICATEKEY UPDATE 函数,但它似乎出现了一些错误,例如 ERRORINSERTINTO 你的 SQL 语法有错误;查看与您的 MariaDB 服务器版本对应的手册,了解在附近使用的正确语法

我的代码如下:

<?php include('profile.php'); ?>

<?php

$servername = "localhost";
$dbusername = "root";
$dbpassword = "*******";
$dbname = "the_database";

$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$aboutme = $_POST['aboutme'];
$subject1 = $_POST['subject1'];
$subject2 = $_POST['subject2'];
$subject3 = $_POST['subject3'];
$country = $_POST['country'];
$birthday = $_POST['birthday'];
$occupation = $_POST['occupation'];
$mobile = $_POST['mobile'];
$websiteurl = $_POST['websiteurl'];



$conn = new mysqli ($servername, $dbusername, $dbpassword, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}


$sql = "INSERT INTO fuser (firstname, lastname, aboutme, subject1, subject2, subject3, country, birthday, occupation, mobile, websiteurl)
VALUES ('$firstname', '$lastname', '$aboutme', '$subject1', '$subject2', '$subject3', '$country', '$birthday', '$occupation', '$mobile', '$websiteurl')
ON DUPLICATE KEY UPDATE
firstname = VALUES('$firstname'),
lastname = VALUES('$lastname'),
aboutme = VALUES('$aboutme'),
subject1 = VALUES('$subject1'),
subject2 = VALUES('$subject2'),
subject3 = VALUES('$subject3'),
country = VALUES('$country'),
birthday = VALUES('$birthday'),
occupation = VALUES('$occupation'),
mobile = VALUES('$mobile'),
websiteurl = VALUES('$websiteurl')";

if ($conn->query($sql) === TRUE) {
echo '<script language="javascript">';
echo 'alert("Your details have been updated succesfully..")';
echo '</script>';
echo '<a href="profile.php"></a>';
}
else {
echo "ERROR" . $sql . "<br>" . $conn->error;
}

$conn->close();
?>

感谢任何建议..

最佳答案

我的错,@furrie 是对的..代码应该是

ON DUPLICATE KEY UPDATE
firstname = VALUES(firstname),
lastname = VALUES(lastname),
aboutme = VALUES(aboutme),
subject1 = VALUES(subject1),
subject2 = VALUES(subject2),
subject3 = VALUES(subject3),
country = VALUES(country),
birthday = VALUES(birthday),
occupation = VALUES(occupation),
mobile = VALUES(mobile),
websiteurl = VALUES(websiteurl)";

现在它像 gem 一样工作......

没关系.. 我实际上需要使用 UPDATE-SET 条件而不是这个.. 无论如何感谢大家的时间.. 通过这个我刚刚学到了一些东西,我们不能将 WHERE 条件与 INSERT INTO 一起使用...

关于php - 错误插入 ON duplicatekey UPDATE 函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39656419/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com