ERROR insert into ON duplicatekey UPDATE function

北慕城南 提交于 2020-01-05 04:43:19

问题


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

My code as follows:

<?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();
?>

Any suggestions are Appreciated..


回答1:


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...

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...



来源:https://stackoverflow.com/questions/39656419/error-insert-into-on-duplicatekey-update-function

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!