2017-08-01 118 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(?,?)");