无法正确执行mysql查询

时间:2016-05-24 19:41:27

标签: php mysql

编码新手。试图创建一个存储广告细节的数据库。 php工作正常,直到最后。但输出始终显示未插入。有人可以帮帮我吗?表格和PHP在这里给出。

<html>
<body>

<form action="basic.php" method="post">

Name: <input type="text" name="adtitle"></input><br>
Name: <input type="text" name="sellersname"></input><br>
Name: <input type="text" name="email"></input><br>
Name: <input type="text" name="description"></input><br>
Name: <input type="text" name="noo"></input><br>
Name: <input type="text" name="base"></input><br>
Name: <input type="text" name="cutoff"></input><br>

<input type="submit"></input>
</form>

</body>
</html>
<?php
$serverName = "127.0.0.1";
$userName = "root";
$password = "";
$database = "demo";

$con = mysqli_connect($serverName, $userName, $password, $database);
if(!$con)
{echo 'not connected';}
if(!mysqli_select_db($con,'demo'))
{echo'db not selected';
}


$Title = $_POST['adtitle'];
$Sname= $_POST['sellersname'];
$email = $_POST['email'];
$Description = $_POST['description'];
$Mobileno = $_POST['noo'];
$Base = $_POST['base'];
$Cutoff = $_POST['cutoff'];

$sql = "INSERT INTO advert (Title,Seller name,email,Description,Number,Base,Cutoff)VALUES('$Title','$Sname','$email','$Description','$Mobileno','$Base','$Cutoff')";
if(!mysqli_query($con,$sql))
{echo'not inserted';}
else {echo'innserted';}

?>

1 个答案:

答案 0 :(得分:2)

您有一个字段seller name,其中有一个空格。

你必须用反引号引用它:

从以下位置更改:

"INSERT INTO advert (Title,Seller name,email,Description,Number,Base,Cutoff) VALUES ('$Title','$Sname','$email','$Description','$Mobileno','$Base','$Cutoff')"

"INSERT INTO advert (Title,`Seller name`,email,Description,Number,Base,Cutoff)VALUES('$Title','$Sname','$email','$Description','$Mobileno','$Base','$Cutoff')"