Php 为什么mysqli insert不起作用?
我正在尝试将数据插入MySQL表,但没有成功。我的代码如下所示:Php 为什么mysqli insert不起作用?,php,mysql,sql,sql-insert,Php,Mysql,Sql,Sql Insert,我正在尝试将数据插入MySQL表,但没有成功。我的代码如下所示: $mysqli = mysqli_connect("localhost","login info"); if ($mysqli->connect_errno) { echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error; } if (isset($_POST['submi
$mysqli = mysqli_connect("localhost","login info");
if ($mysqli->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
}
if (isset($_POST['submit'])) {
$number = $_POST['number'];
$road = $_POST['road'];
$postcode=$_POST['postcode'];
$price=$_POST['price'];
$type=$_POST['type'];
$bedrooms=$_POST['bedrooms'];
$agent=$_POST['agent'];
$featured=$_POST['featured'];
$keywords=$_POST['keywords'];
$mysqli->query("
INSERT INTO listings-rent
(number, road, postcode, price, type, bedrooms, agent, featured, keywords)
VALUES
('$number','$road','$postcode','$price','$type','$bedrooms','$agent','$featured','$keywords')");
}
连接正常,未返回任何错误。您的表名应包含反勾号,因为它包含非字母数字字符
INSERT INTO `listings-rent` (...) VALUES (...)
另外,请对这些值进行参数化。您的表名应该用反勾号括起来,因为它包含一个非字母数字字符
INSERT INTO `listings-rent` (...) VALUES (...)
另外,请对值进行参数化。您的表名有一个
-
,它不是MySQL中不带引号的表名所支持的字符,如中所述。试试这个:
$mysqli->query("INSERT INTO `listings-rent` (number, road, postcode, price, type, bedrooms, agent, featured, keywords) VALUES ('$number','$road','$postcode','$price','$type','$bedrooms','$agent','$featured','$keywords')");
要查看错误,可以使用
echo$mysqli->error提到了code>。您的表名有一个-
,它不是MySQL中不带引号的表名所支持的字符,如中所述。试试这个:
$mysqli->query("INSERT INTO `listings-rent` (number, road, postcode, price, type, bedrooms, agent, featured, keywords) VALUES ('$number','$road','$postcode','$price','$type','$bedrooms','$agent','$featured','$keywords')");
要查看错误,可以使用echo$mysqli->error代码>已提到。您的表名中可能缺少“`”
我建议:
if (TRUE == $mysqli->query("INSERT INTO `listings-rent` (number, road, postcode, price, type, bedrooms, agent, featured, keywords) VALUES ('$number','$road','$postcode','$price','$type','$bedrooms','$agent','$featured','$keywords')"))
echo "its working!"
else
echo $mysqli->error;
这样你就会发现问题所在
其他选项我建议它打印失败的查询,并使用phpmyadmin手动插入它,并检查它为什么不工作可能表名中缺少``
我建议:
if (TRUE == $mysqli->query("INSERT INTO `listings-rent` (number, road, postcode, price, type, bedrooms, agent, featured, keywords) VALUES ('$number','$road','$postcode','$price','$type','$bedrooms','$agent','$featured','$keywords')"))
echo "its working!"
else
echo $mysqli->error;
这样你就会发现问题所在
其他选项我建议它打印失败的查询,并使用phpmyadmin手动插入它,并检查它为什么不工作尝试以下方法:
$mysqli = mysqli_connect("localhost", "your_db_username" , "your_db_password" , "database_name")
对于本地服务器,用户名通常为root,密码为null。因此,在这种情况下,复制粘贴以下内容:
$mysqli = mysqli_connect("localhost", "root" , "" , "login info")
请尝试以下方法:
$mysqli = mysqli_connect("localhost", "your_db_username" , "your_db_password" , "database_name")
对于本地服务器,用户名通常为root,密码为null。因此,在这种情况下,复制粘贴以下内容:
$mysqli = mysqli_connect("localhost", "root" , "" , "login info")
您需要了解该漏洞。为什么似乎很少有PHP/MySQL开发人员听说过参数化查询?使用反勾号作为表名@JW。建议:插入'listings rent`
@juergend您当然明白了。我不知道为什么其他人不知道。您需要了解漏洞。为什么PHP/MySQL开发人员很少听说过参数化查询?请使用反勾号作为表名@JW。建议:插入'listings rent`
@juergend您当然明白了。我不知道为什么其他人不这么做。我不知道为什么这个答案会遭到大量的反对票:D
会接受第一个回答+更正。将研究参数化的值-有人建议一个好的、易于理解的资源吗?本文讨论但清楚地显示了一些基本示例和扩展上的链接。我不知道为什么这个答案会获得大量的反对票:D
将接受为第一个回答+更正。将研究参数化的值-有人建议一个好的、易于理解的资源吗?本文讨论了一些基本的例子,但清楚地显示了一些扩展的链接。