SQL Update或插入查询不起作用

时间:2016-05-23 22:01:58

标签: php mysql sql

我花了大约2个小时现在改变脚本并找到其他方法,但没有运气。如果有人可以请指出我的脚本中的任何问题,这将是伟大的。非常感谢。

我已经更新了脚本,希望这可以提供更好的理解和更多细节。

<?php

    session_start();

if(!isset($_SESSION["user"]) or !is_array($_SESSION["user"]) or empty($_SESSION["user"])) {
      // redirect to login page
}
$dbhost   = "localhost";
$dbname   = "**";
$dbuser   = "root";
$dbpass   = "**";

// database connection
$conn = new PDO("mysql:host=$dbhost;dbname=$dbname",$dbuser,$dbpass);
// new data



error_reporting(E_ALL);

                    $work = $_POST['work'];
                    $education = $_POST['education'];
                    $location = $_POST['location'];
                    $relationship = $_POST['relationship'];
                    $phone = $_POST['phone'];
                    $email = $_POST['email'];
                    $website = $_POST['website'];
                    $language = $_POST['language'];
                    $skill = $_POST['skill'];
                    $tumblr = $_POST['tumblr'];
                    $instagram = $_POST['instagram'];
                    $skype = $_POST['skype'];
                    $facebook = $_POST['facebook'];
                    $youtube = $_POST['youtube'];
                    $twitter = $_POST['twitter'];
                    $about = $_POST['about'];
                    $user_id = $_SESSION['user']['id'];

                    echo $work."</br>";
                    echo $education."</br>";
                    echo $location."</br>";
                    echo $relationship."</br>";
                    echo $phone."</br>";
                    echo $email."</br>";
                    echo $website."</br>";
                    echo $language."</br>";
                    echo $skill."</br>";
                    echo $tumblr."</br>";
                    echo $instagram."</br>";
                    echo $skype."</br>";
                    echo $facebook."</br>";
                    echo $youtube."</br>";
                    echo $twitter."</br>";
                    echo $about."</br>";
                    echo $user_id;

/*
        "IF EXISTS (SELECT * FROM Table1 WHERE Column1=’SomeValue’)
    UPDATE Table1 SET (…) WHERE Column1=’SomeValue’
ELSE
    INSERT INTO Table1 VALUES (…)"
*/

$sql = "IF EXISTS (SELECT * FROM user_details WHERE user_id=".$_SESSION['user']['id'].")
    UPDATE user_details SET 
    (work='".$work."',education='".$education."',location='".$location."',relationship='".$relationship."',phone='".$phone."',email='".$email."',website='".$website."',language='".$language."',skill='".$skill."',tumblr='".$tumblr."',instagram='".$instagram."',skype='".$skype."',facebook='".$facebook."',youtube='".$youtube."',twitter='".$twitter."',about='".$about."') 
WHERE user_id='".$user_id."'
ELSE INSERT INTO user_details (work,education,location,relationship,phone,email,website,language,skill,tumblr,instagram,skype,facebook,youtube,twitter,about,user_id)
VALUES
('".$work."','".$education."','".$location."','".$relationship."','".$phone."','".$email."','".$website."','".$language."','".$skill."','".$tumblr."','".$instagram."','".$skype."','".$facebook."','".$youtube."','".$twitter."','".$about."','".$user_id."')";
                    $q = $conn->prepare($sql);
                    $q->execute();
        var_dump(mysql_error())



// header('Location: ../usr_settings.php');


?>

1 个答案:

答案 0 :(得分:2)

更多详情请参阅。例如,错误消息应该是有用的。在更新你有$$电子邮件(doulbe $$标志)可能会尝试输入空电子邮件。如果它不能为空 - 查询可能会崩溃。