简体   繁体   English

将变量作为行插入到mysql数据库中的代码不起作用(PHP)

[英]This code for inserting variables into a mysql db as rows doesn't work (PHP)

I'm trying to get variables to input as rows to columns in a mysql database (I'm new to both mysql and php) via php. 我试图通过php将变量作为行输入到mysql数据库中的列(我是mysql和php的新手)。 However, it doesn't seem to work and this test code I put in fails every time. 但是,它似乎不起作用,我输入的测试代码每次都失败。 Again I'm new to this, so if I'm making a stupid mistake please point it out nicely. 我再次对此不熟悉,所以如果我犯了一个愚蠢的错误,请指出它。

$conn = new mysqli ("localhost", "root", "", "testdb");
echo "Database connection successful";
echo '<br />';
$test1 = "a";
$test2 = "a";
$test3 = "a";
$test4 = "a";
$test5 = "a";
$query = "INSERT INTO testtable (testcolumn1, testcolumn2, testcolumn3, testcolumn4, testcolumn5) VALUES ('$test1', '$test2', '$test3', '$test4', '$test5')";
if ($conn->query($query) === TRUE) {
    echo "passed";
} else {
    echo "failed";
}

The database and table and columns are all valid and present, it's something to do with how I'm using the query. 数据库,表和列都是有效的和存在的,这与我如何使用查询有关。 Can anyone help? 有人可以帮忙吗?

Sample 样品

MariaDB [bb]> insert into table (val) VALUES(99);
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'table (val) VALUES(99)' at line 1

MariaDB [bb]> insert into `table` (val) VALUES(99);
Query OK, 1 row affected (0.00 sec)

MariaDB [bb]>

and your query: 和您的查询:

you can copy it 你可以复制它

INSERT INTO `table` (testcolumn1, testcolumn2, testcolumn3, testcolumn4, testcolumn5) VALUES ('$test1', '$test2', '$test3', '$test4', '$test5')

The script seems to be working fine. 该脚本似乎工作正常。 I was able to execute the following script without a problem. 我能够毫无问题地执行以下脚本。

<?php

$conn = new mysqli ("localhost", "newuser", "password", "testdb");
echo "Database connection successful";
echo '<br />';
$test1 = "a";
$test2 = "a";
$test3 = "a";
$test4 = "a";
$test5 = "a";
$query = "INSERT INTO testtable (testcolumn1, testcolumn2, testcolumn3, testcolumn4, testcolumn5) VALUES ('$test1', '$test2', '$test3', '$test4', '$test5');";
if ($conn->query($query) === TRUE) {
    echo "passed";
} else {
    echo "failed";
}

Double check your Database connection. 仔细检查您的数据库连接。

can you try the below code, and another thing just asking did you checked the auto increment field of primary key. 你可以尝试下面的代码,另一件事只是问你检查了主键的自动增量字段。

    $conn = new mysqli ("localhost", "root", "", "testdb");
    if(!$conn){
       echo "Database connection failed";
    } else {
       echo "Database connection successful";
        echo '<br />';
        $test1 = "a";
        $test2 = "a";
        $test3 = "a";
        $test4 = "a";
        $test5 = "a";


       $query = "INSERT INTO testtable (testcolumn1, testcolumn2,    testcolumn3, testcolumn4, testcolumn5)
        VALUES ('$test1', '$test2', '$test3', '$test4', '$test5');";
      if ($conn->query($query) === TRUE) {
        echo "passed";
      } else {
      echo "failed";
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM