2017-08-01 57 views
-1

我有一個簡單的表單,它需要用戶的輸入並將其添加到數據庫中,但是當我瀏覽數據庫表時,我看不到數據。它總是空的。我不知道發生了什麼,代碼昨天正常工作,但在格式化我的電腦並重新安裝了wamp服務器後停止工作。用PHP編寫語句填充表格

<?php 
$db = new mysqli("localhost","testuser","password","testdb"); 
$title = "My users"; 
if (isset($_POST['submit'])) { 
    $user = $_POST['username']; 



    $result = $db->prepare("INSERT INTO links VALUES('',?,?)"); 
    $result->bind_param("ss",$user,$title); 
    $result->execute(); 
} 

?>

<!DOCTYPE html> 
<html> 
<head> 
    <meta charset="utf-8"> 
    <meta http-equiv="X-UA-Compatible" content="IE=edge"> 
    <title>Get Users Name</title> 
    <link rel="stylesheet" type="text/css" href="css/bootstrap.min.css"> 
</head> 
<body> 
    <div class="container" align="center" style="margin-top:20%;"> 
    <h1>Who are You?</h1> 
      <form action="proc.php" method="POST" > 
      <input type="text" name="username" value="" placeholder="Your username here"> 
      <input type="submit" name="submit" value="Enter"> 


     </form> 
    </div> 
</body> 
</html> 
+0

檢查[mysqli_errors](http://php.net/manual /en/mysqli.error.php),它會告訴你插入查詢是否以任何方式失敗。如果第一個值是一個自動增量id,你可能想嘗試傳遞'NULL'而不是空字符串''''。 – aynber

+0

我似乎無法理解這段代碼可能會出現什麼問題,一切看起來都很好,但無法正常工作。在mysql錯誤日誌中沒有錯誤 –

+0

嘗試在$ db-> prepare()和$ $ result-> execute()之後添加'或die($ db-> error)'來查看錯誤。 – Barmar

回答

1

你能否改變這種

$result = $db->prepare("INSERT INTO links VALUES('',?,?)"); 

這個

$result = $db->prepare("INSERT INTO yourtable (col, col2) VALUES(?,?)");