1

我正在尝试将数据插入到我的 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['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')");
}

连接很好,没有返回错误。

4

4 回答 4

6

您的表名应该用反引号括起来,因为它包含非字母数字字符。

INSERT INTO `listings-rent` (...) VALUES (...)

并且请参数化这些值。

于 2013-02-09T15:56:43.473 回答
4

如本文档中所述,您的表名具有-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;提到的。

于 2013-02-09T16:03:03.500 回答
1

可能您的表名中缺少 ``

我会建议:

    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 手动插入并检查它为什么不工作

于 2013-02-09T16:05:42.200 回答
0

试试这个:

$mysqli = mysqli_connect("localhost", "your_db_username" , "your_db_password" , "database_name")

对于本地服务器,通常用户名为 root,密码为空。所以在这种情况下复制粘贴这个:

$mysqli = mysqli_connect("localhost", "root" , "" , "login info")
于 2017-06-24T06:14:21.160 回答