mysqli_query()没有给出任何响应,甚至mysqli_error()也没有给出任何错误消息

时间:2015-12-10 06:08:28

标签: php mysqli xampp ubuntu-14.04

我正在尝试将数据插入mysqli数据库。已成功建立数据库连接。但是没有插入数据。即使mysqli_eror()没有显示任何错误。

我的代码:

<?php

  $name=$_POST['name'];
  $email=$_POST['email'];
  $password=$_POST['password'];

  $con=mysqli_connect("localhost","root","password","vote") ||
  die("couldn't connect to database");

  if(!$con){
    die("Could't connect to database. try again");
  }
  else{
    echo "connected to db"; /* connection established successfully */
    $query="INSERT INTO candidate(ID,name,email,password)
    VALUES(0,'$name','$email','$password' ) ";
    $result=mysqli_query($con,$query);
    echo ($result)? '1':mysqli_error($con);
  }

?>

转到phpmyadmin我运行相同的插入查询并插入了数据。

但是这段代码没有插入数据。即使 mysqli_error($ con)也会返回空字符串

我在XAMPP

上使用Ubuntu 14.04

[请询问您是否需要更多信息]

1 个答案:

答案 0 :(得分:2)

我认为这是在制造问题,

 $con=mysqli_connect("localhost","root","password","vote") ||
      die("couldn't connect to database");

将其更改为

 $con=mysqli_connect("localhost","root","password","vote") or
                                                           ^
      die("couldn't connect to database");

或者

$con = mysqli_connect("localhost", "root", "password", "vote");

if (mysqli_connect_errno()) {
     echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

如果您没有看到错误,请尝试在文件中添加error_reporting(E_ALL);