2016-05-23 95 views
-2

我花了大約2個小時來改變腳本並找到其他方法,但沒有運氣。如果有人能夠指出我的腳本中的任何問題,那就太棒了。非常感謝。SQL更新或插入查詢不起作用

我已經更新了腳本,希望這可以提供更好的理解和更多的細節。

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


?> 

回答

2

更多詳細資訊請參考。例如錯誤消息應該很有用。在更新你有$$電子郵件(doulbe $ $$ sign),可能試圖輸入空電子郵件。如果它不能爲空 - 查詢可能會崩潰。

+0

那麼這裏沒有錯誤信息。所有的領域正在填寫和張貼形式,我通過回聲方法檢查。沒有SQL錯誤或任何東西。 我想更新一個數據庫表,如果它們不是帶有用戶ID的查詢,我需要插入一個新的。 –

+1

而且,當然,您正在使用mysql_query或類似的東西運行該查詢? –

+0

是的,MySQL不是mysqli –