mySQL插入失败

时间:2014-06-08 14:33:34

标签: php mysql sql

请帮我这个。我认为我的查询是正确的,但它不会插入到我的表中。

$dbhost = "localhost";
$dbuser = "root";
$dbpass = "root";
$dbname = "test";

$connection = mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);

if (!empty($_POST['name']) AND !empty($_POST['info'])) {
    $name = mysql_real_escape_string($_POST['name']);
    $info = mysql_real_escape_string($_POST['info']);

    $query = "INSERT INTO comment (name, info) VALUES('$name', '$info') ";
    if(mysqli_query($connection,$query)){
        $print = "success";
    }
    else{
        $print = "not success";
    }
}

4 个答案:

答案 0 :(得分:0)

弗雷德试图说的是你应该调整你的代码

$dbhost = "localhost";
$dbuser = "root";
$dbpass = "root";
$dbname = "test";

$con= mysqli_connect($dbhost, $dbuser, $dbpass, $dbname);

if (!empty($_POST['name']) AND !empty($_POST['info'])) {
$name = $con->real_escape_string($_POST['name']);
$info = $con->real_escape_string($_POST['info']);

$query = "INSERT INTO comment (name, info) VALUES('$name', '$info') ";
if(mysqli_query($con,$query)){
    $print = "success";
}
else{
    $print = "not success";
}
}

应该有效

参考文献:php MYSQLI

答案 1 :(得分:0)

一旦您尝试仅指定值,即

$query = "INSERT INTO comment VALUES('$name', '$info') ";

某些时候它可以运作

答案 2 :(得分:0)

试试这个:

$query = "INSERT INTO comment (name, info) VALUES('".$name."', '".$info."') ";

答案 3 :(得分:-1)

我的兄弟......

你用过这个:$ query =&#34; INSERT INTO评论(姓名,信息)价值观(&#39; $ name&#39;,&#39; $ info&#39;)&#34 ;; < / p>

但是你忘记了SET:使用这个:

$query = "INSERT INTO comment SET (name, info) VALUES('$name', '$info') ";