PHP不会将数据插入MySQL DB

时间:2014-01-20 18:01:59

标签: php mysql forms

在我开始之前,我已经看过其他类似的问题并完成了所有内容,但它仍然无效。 我正在创建一个注册脚本,没有错误发生,但数据库中没有提交任何数据。 数据库名为“test”,表名为“users”。 这是表格;

<body><link rel="stylesheet" type="text/css" href="style.css">
<form action="script3.php" method="get">
<p>First Name:<input type="text" name="first_name" /></p>
<p>Last Name:<input type="text" name="last_name" /></p>
<p>Email: <input type="text" name="email" /></p>
<p>Password: <input type="text" name="pass" /></p>
<input type="submit" name="submit" value="Submit!" />
</form>
</body>
</html>

这是script3;

<?php 
$first_name = $_REQUEST['first_name'];
$last_name = $_REQUEST['last_name'];
$email = $_REQUEST['email'];
$pass = $_REQUEST['pass'];
DEFINE ('DB_USER', 'root');
DEFINE ('DB_PASSWORD', '');  
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'test');
$dbc = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) 
OR die("could not connect");
$query = ("INSERT INTO users (first_name, last_name, email, pass, reg_date) 
VALUES ('$first_name', '$last_name', '$email', SHA1('$pass'), NOW() );")
?>

7 个答案:

答案 0 :(得分:2)

您需要实际运行SQL查询,而不仅仅是定义查询SQL字符串。

答案 1 :(得分:2)

试试这个,你需要执行查询:

$query = ("INSERT INTO users (first_name, last_name, email, pass, reg_date) 
VALUES ('$first_name', '$last_name', '$email', SHA1('$pass'), NOW() );");
mysqli_query($dbc, $query);

参考:http://in3.php.net/mysqli_query

答案 2 :(得分:1)

 <?php 
$first_name = $_REQUEST['first_name'];
$last_name = $_REQUEST['last_name'];
$email = $_REQUEST['email'];
$pass = $_REQUEST['pass'];
DEFINE ('DB_USER', 'root');
DEFINE ('DB_PASSWORD', '');  
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'test');
$dbc = @mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) 
OR die("could not connect");
$query = ("INSERT INTO users (first_name, last_name, email, pass, reg_date) 
VALUES ('$first_name', '$last_name', '$email', SHA1('$pass'), NOW() );")
    // insert this line, it will work
    mysqli_query($query);

答案 3 :(得分:0)

只需将查询字符串传递给mysql_query()函数。

mysqli_query($query);

答案 4 :(得分:0)

老兄,你只需插入一行。

mysqli_query($query,$dbc);

现在你的代码准备好了。

答案 5 :(得分:0)

<?php 
$first_name = $_REQUEST['first_name'];
$last_name = $_REQUEST['last_name'];
$email = $_REQUEST['email'];
$pass = $_REQUEST['pass'];
DEFINE ('DB_USER', 'root');
DEFINE ('DB_PASSWORD', '');  
DEFINE ('DB_HOST', 'localhost');
DEFINE ('DB_NAME', 'test');
$dbc = mysqli_connect (DB_HOST, DB_USER, DB_PASSWORD, DB_NAME) //@ symbol removed
OR die("could not connect");
$today=Date('Y-m-d');
$query = "INSERT INTO users (first_name, last_name, email, pass, reg_date) 
VALUES ('$first_name', '$last_name', '$email', SHA1('$pass'), '$today' )";   //() removed , '' added for date
$res=mysqli_query($abc,$query);  //added
?>

这将有效。

如果不起作用,请评论您的错误消息..

答案 6 :(得分:0)

$query = ("INSERT INTO users (first_name, last_name, email, pass, reg_date) 
VALUES ('".$first_name."', '".$last_name."', '".$email."', SHA1('".$pass."'), NOW() );")

    mysqli_query($query);