问题描述
我在数据库中有一个表"fuser",其中提到了各个字段.我尝试了INPUT INTO&DUPLICATEKEY UPDATE函数,但似乎出现了一些错误,例如 ERRORINSERTINTO.检查与您的MariaDB服务器版本相对应的手册,以在
i have a table 'fuser' in database with respective fields mentioned.. i have .php form with the fields input area. I tried INPUT INTO & DUPLICATEKEY UPDATE function, but it seems to be getting some error like ERRORINSERTINTO You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near
我的代码如下:
<?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是正确的.代码应该是
My bad, @furrie is right.. the code should be
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)";
现在它像宝石一样工作...
now its working like gem...
没关系..实际上我确实需要使用UPDATE-SET条件,..无论如何,感谢您的时间..通过这一点,我刚刚学到了一点,我们不能在INSERT INTO中使用WHERE条件...
Never mind.. i actually needed to use UPDATE-SET condition instead this.. anyway thanks to all for your time .. By this i just learned something, we can't use WHERE condition with INSERT INTO...
这篇关于错误插入到ONplicatekey UPDATE函数中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!