数据未通过php插入mysql数据库

时间:2016-08-12 08:13:51

标签: php mysql mysqli

正确显示数据,因此从表单传递数据没有问题,因此我无法找到错误。

<?php

$connect = mysql_connect("localhost", "root", "") or die(mysql_error());
mysql_select_db("property", $connect);

/* if(isset($_POST['name'],$_POST['email'],$_POST['comments'])){ */
$rs = $_POST['rs'];
$cr = $_POST['cr'];
$locality = $_POST['locality'];
$br = $_POST['br'];
$bth = $_POST['bth'];
$fr = $_POST['fr'];
$flr = $_POST['flr'];
$tflr = $_POST['tflr'];
$bu = $_POST['bu'];
$park = $_POST['park'];
$af = $_POST['af'];
$comment = $_POST['comment'];

$query = "INSERT INTO property(id,sale/rent,com/res,locality,bedroom,bathroom,furnished_status,
            floor,total floors,builtup/superbuiltup,includes_park,available_from,details) 
          VALUES('','$rs','$cr','$locality','$br','$bth','$fr','$flr','$tflr','$bu','$park','$af','$comment')";

mysql_query($query, $connect);

echo $rs;
echo "<br>";
echo $cr;
echo "<br>";
echo $locality;
echo "<br>";
echo $br;
echo "<br>";
echo $bth;
echo "<br>";
echo $fr;
echo "<br>";
echo $flr;
echo "<br>";
echo $tflr;
echo "<br>";
echo $bu;
echo "<br>";
echo $af;
echo "<br>";
echo $comment;
echo "<br>";


/*
  header('location:submitted.html');
  } */
?>

3 个答案:

答案 0 :(得分:1)

你的sql语法有错误。

$query="INSERT INTO property(id,sale/rent,
                                        com/res,
                                        locality,
                                        bedroom,
                                        bathroom,
                                        furnished_status,
                                        floor,
                                        total floors,
                                        builtup/superbuiltup,
                                        includes_park,
                                        available_from,
                                        details) VALUES('',                                                                          '$rs',
                                                                        '$cr',
                                                                        '$locality',
                                                                        '$br',
                                                                        '$bth',
                                                                        '$fr',
                                                                        '$flr',
                                                                        '$tflr',
                                                                        '$bu',
                                                                        '$park',
                                                                        '$af',
                                                                        '$comment')";

试试这个..

$query="INSERT INTO property(id,sale/rent,
                                        com/res,
                                        locality,
                                        bedroom,
                                        bathroom,
                                        furnished_status,
                                        floor,
                                        total floors,
                                        builtup/superbuiltup,
                                        includes_park,
                                        available_from,
                                        details) VALUES('',
                                                                        '".$rs."',
                                                                        '".$cr."',
                                                                        '".$locality."',
                                                                        '".$br."',
                                                                        '".$bth."',
                                                                        '".$fr."',
                                                                        '".$flr."',
                                                                        '".$tflr."',
                                                                        '".$bu."',
                                                                        '".$park."',
                                                                        '".$af."',
                                                                        '".$comment."')";

答案 1 :(得分:0)

查看错误使用mysql_error():

 mysql_query($query) or die(mysql_error();

你的代码需要被保护它对SQL注入是开放的,而且mysql函数已经过时你应该转移到mysqli或pdo然后使用预处理语句它会更安全。

如果您将ID设置为自动增量,则无需将其添加到插入查询中。

答案 2 :(得分:0)

列名为total floors

INSERT QUERY

创建表时,可能是列名允许有空格。但是,插入它时会显示错误。

  

1064 - 您的SQL语法出错;检查手册   对应于您的MySQL服务器版本,以便使用正确的语法   靠近&#39;总楼层&#39;在第1行

因此,插入时请用反引号括起来。

INSERT INTO property(... `total floors` ... ) VALUES ('',''....);

<强> BACKTICK

enter image description here