ERROR插入ON duplicatekey UPDATE函数

时间:2016-09-23 08:42:01

标签: php mysql debugging

我有一张桌子' fuser'在数据库中提到的各个字段..我有.php形式与字段输入区域。我试过INPUT INTO& DUPLICATEKEY UPDATE函数,但它似乎得到了一些错误,如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();
?>

任何建议都得到了赞赏..

1 个答案:

答案 0 :(得分:0)

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

<select id="price-from" onchange="setPrice()">
    <option selected value="500">£500</option>
    <option value="1000">£1,000</option>
    <option value="2000">£2,000</option>
    <option value="3000">£3,000</option>
    <option value="4000">£4,000</option>
    <option value="5000">£5,000</option>
    <option value="6000">£6,000</option>
    <option value="7000">£7,000</option>
    <option value="8000">£8,000</option>
    <option value="9000">£9,000</option>
    <option value="10000">£10,000</option>
    <option value="20000">£20,000</option>
</select>
<p id="to">To</p>
<select id="price-to" onchange="setPrice()">
    <option value="500">£500</option>
    <option value="1000">£1,000</option>
    <option value="2000">£2,000</option>
    <option value="3000">£3,000</option>
    <option value="4000">£4,000</option>
    <option value="5000">£5,000</option>
    <option value="6000">£6,000</option>
    <option value="7000">£7,000</option>
    <option value="8000">£8,000</option>
    <option value="9000">£9,000</option>
    <option value="10000">£10,000</option>
    <option selected value="20000">£20,000</option>
</select>

现在它像宝石一样......

没关系..我实际上需要使用UPDATE-SET条件而不是这个..无论如何,多亏了你所有的时间..通过这个我刚学到的东西,我们不能使用INSERT INTO的WHERE条件.. 。