2017-02-19 45 views
1

我在我的Mysqli插入這裏有一點問題。我在插入的周圍放置了一個try塊來捕捉可能的錯誤,我收到了它的工作通知,但沒有插入任何數據。Mysqli插入不起作用。沒有錯誤彈出

下面的代碼:

$mysqli = new mysqli("localhost", "yourusername", "somepasswdhere", "users"); 
if ($mysqli->connect_errno) { 
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; 
} 



         //formular set ? 
     if(isset($_POST['submit'])) { 
      echo $_POST['username']; 

      //bind post variables on local vars. 
      $username = $_POST['username']; 
      $fname = $_POST['fname']; 
      $lname = $_POST['lname']; 
      $email = $_POST['email']; 
      $password = $_POST['password']; 
      $cpassword = $_POST['cpassword']; 

      // Heres some crap but thats not that problem so im getting 
      // right into the next 
      // maybe problem part 

      try{ 
      $mysqli->query("INSERT INTO `users` ($username) VALUES ('$username')"); 
      $mysqli->commit(); 
      echo "works"; 
      } catch(Exception $e) { 
       echo $e->getMessage(), "\n"; 
      } 

要確保我的代碼將被提交,我把$mysqli->commit();

+0

類似於INSERT INTO用戶(bob)VALUES('bob')'聽起來不正確。 – apokryfos

回答

0

你可以嘗試錯誤檢查以下內容:

$success = $mysqli->query(<YOUR SQL QUERY>); 
if (!$success) { 
    print_r($mysqli->error); 
} 

此外,在您的查詢,爲什麼你有第一$用戶名?我相信它應該是您的列名,格式如下:

INSERT INTO `users` (<col name>) VALUES (<value>); 
+0

謝謝,您的「錯誤cheking」與Gmnayem的答案一起修復了劇本。和耶應該是我的大名,我搞砸了$感謝您的快速支持 – Tolkosino

+0

@Tolkosino:真棒。另外,請標記您接受的正確答案。 – uautkarsh

1

您的列名不應該包含「$」 sign.Use這個代替。

$mysqli->query("INSERT INTO `users` (username) VALUES ('$username')"); 

希望它可以幫助你。

相關問題