2014-02-26 110 views
0

我修改了我的mysqli數據庫,使用預準備語句將值從我的表單插入到數據庫中。 感謝stackoverflow - 我知道這是通過閱讀本網站的方式。無法使用準備好的語句插入mySQLi數據庫

更改我的語句如下所示,但由於某些原因,他們沒有插入數據庫。我可能做錯了什麼?我在我的字符串周圍添加了「',但這不起作用......不知道我可能會丟失什麼。我從一篇教程開始,一直致力於這組代碼。

任何幫助讓我的插入工作將不勝感激,我是新的mysqli準備的陳述。這裏是我的代碼:

<?php 
    $con= new mysqli("localhost","admin1","default","sourcesDB"); 
    // Check connection 

    if (mysqli_connect_erno()) { 
     printf("Connect failed: %s\n", mysqli_connect_error()); 
    exit(); 
    } 

    $biotext = $_POST[$bio]; 

     $keywords= json_encode($_POST['Keyword_ID']); 
     $assocs= json_encode($_POST['Associations']); 
     $fname = $_Post['FName']; 
     $mname = $_POST['MName']; 
     $lname = $_POST['LName']; 
     $suffix = $_POST['Suffix']; 
     $dept = $_POST['Dept']; 
     $title = $_POST['Title']; 
     $title2 = $_POST['Title2']; 
     $title3 = $_POST['Title3']; 
     $edu = $_POST['Education']; 
     $edu2 = $_POST['Education2']; 
     $edu3 = $_POST['Education3']; 
     $ph1 = $_POST['PH1']; 
     $ph2 = $_POST['PH2']; 
     $email = $_POST['Email']; 
     $pic = $_POST['Pic']; 
     $linkname = $_POST['LinkName']; 
     $website = $_POST['Website']; 
     $tags = $_POST['Tags']; 
     $bio = $_POST['bioLinks']; 


    $stmt = $con->prepare('INSERT IGNORE INTO profileTable 
    (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?) '); 

    $stmt->bind_param('sssssssssssssssssssss', $fname, $mname, $lname, $suffix, $dept, 
    $title, $title2, $titl3, $edu, $edu2, $edu3, $ph1, $ph2, $email, $pic, $linkname, 
    $bio, $website, $keywords, $tags, $assocs); 

    if($stmt->execute()) { 
echo '<div style="color: green;">Profile Added!</div>'; 
echo "<meta http-equiv='Refresh' content='4; URL=addProfile.php'>"; 
    } 
    else { 
echo "Failed to insert"; 
    } 

    $stmt->close(); 
+0

'$ title3'和'$ titl3'不匹配。如果/當一個失敗時,他們都會失敗。 –

+0

好抓@ Fred-ii-我確實改變了這個,但仍然沒有插入。我有一個問題...如果我的數據庫中的第一列是「id」會如何設置插入的順序? – MizAkita

+0

http://stackoverflow.com/q/18457821/285587 –

回答

0

我能回答這個問題,我自己...我添加的列的名稱插入到,然後結合我的PARAMS我的價值。最後,這是什麼結束了工作完美...

if ($stmt = $con->prepare("INSERT INTO profileTable (FirName, MName, LaName, 
     Suffix, Dept, Title, Title2, Title3, Education, Education2, Education3, 
     PH1, PH2, Email, Photo, BioLK, Website, Keyword_ID, Tags, Associations) 
      VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)")); 

     { 
     $stmt->bind_param("ssssssssssssssssssss", $fname, $mname, $lname, $suffix, 
     $dept, $title, $title2, $title3, $edu, $edu2, $edu3, $ph1, $ph2, $email, 
     $linkname, $bio, $website, $keywords, $tags, $assocs); 

     $stmt->execute(); 
     echo '<div style="color: green;">Profile $fname $lname Added!</div>'; 
     echo "<meta http-equiv='Refresh' content='4; URL=addProfile.php'>"; 

     $stmt->close(); 

     mysqli_close($con); 
     }