INSERT INTO查询不起作用?

时间:2016-05-20 09:28:48

标签: php mysql

HTML code:

<form action="create.php" method="post">
Username: <input type="text" name="username"><br>
Password: <input type="text" name="password"><br>
<input type="submit" value="CREATE ACCOUNT">
</form>

PHP代码(create.php):

<?php


require_once 'studentdb.php';
$db_server = mysql_connect($host,$username,$password);
if (!$db_server) die("Unable to connect to MySQL: " . mysql_error());
mysql_select_db($dbname)
or die("Unable to select database: " . mysql_error());
$query = "INSERT INTO credentials (Username,Password) VALUES(".$_POST['username'].",".$_POST['password'].")"
$make = mysql_query($db_server,$query);
?>

在这里的PHP中,studentdb.php具有登录数据库所需的所有信息。

我没有收到任何错误,但表格根本没有更新。

3 个答案:

答案 0 :(得分:1)

$_POST['username']$_POST['username']应为字符串,因此您需要在插入查询中添加引号'

<?php


require_once 'studentdb.php';
$db_server = mysql_connect($host,$username,$password);
if (!$db_server) die("Unable to connect to MySQL: " . mysql_error());
mysql_select_db($dbname)
or die("Unable to select database: " . mysql_error());
$query = "INSERT INTO credentials (Username,Password) VALUES('".$_POST['username']."','".$_POST['password']."')"
$make = mysql_query($db_server,$query);
?>

答案 1 :(得分:0)

$query = "INSERT INTO credentials (`Username`,`Password`) VALUES('".$_POST['username']."', '".$_POST['password']."' )"; 

请使用quotes作为字符串。

答案 2 :(得分:0)

<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 

$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john@example.com')";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>