mysqli_query()不会将数据插入我的数据库

时间:2012-03-11 20:11:54

标签: php

我遇到mysqli_query的问题。我在单例数据库类调用connect()querydb()中设置了方法,querydb采用$query参数我确定没问题,因为我使用了查询来添加数据到phpMyAdmin中的表。这是我的PHP方法,当我尝试将数据添加到register.php类中的表时。

public function register() {

   $databaseinst = database:: getinstance();
   $conn =   $databaseinst->connect();

   $query = "INSERT INTO  Customer (email_address,firstname,lastname,address_line_1,address_line_2,city,postcode,country,password) VALUES ('$this->email','$this->firstname','$this->secondname','$this->address1','$this->address2','$this->city','$this->postcode','$this->country','$this->password1')";

   $numrows = $databaseinst->querydb($query);

       if ( false===$numrows ) {
    $this->errors[]= mysqli_error($conn);
    }

       if ($numrows < 1) {
            $this->errors[] = "could not process query";
        }
        if($conn == 0)
        {
        $this->errors[] = "couldnt connect";
        }
        if($conn == 1)
        {
        $this->errors[] = " connected";
        }
    }

    public function showerrors() {
        echo "<h3> ERRORS!!</h3>";
        echo "<p>" . var_dump($this->errors) . "</p>";
        foreach ($this->errors as $key => $value)
        {
            echo $value . "</br>";
        }
    }

第二种方法就是如何在错误发生时打印出来。 接下来是我的数据库单例类中的方法,它返回一个实例很好我认为问题是我的mysqli_query()方法中的querydb()方法调用只是可以弄清楚它是什么我一直得到一个“不能进程查询“回复。无论如何这里是我使用的单身人士课程,它几乎是我在网上找到的一个编辑版本。

<?php

class database {

private static $instance;
private $dbcon;
private $result;
private $numrows;


private function __construct()
{

}

  static function getinstance()
{
    if(!self::$instance)
    {
     self::$instance = new self();
    }

    return self::$instance;

 }

public function connect()
{
    $this->dbcon = mysqli_connect('localhost','********','****');
  if (!$this->dbcon)
  {
  return 0;
  }
  else
  {
  return 1;
  }

}

 public function querydb($query)
{

mysqli_select_db($this->dbcon,'theislan_testdatabase');

$this->result = mysqli_query($this->dbcon,$query);
$this->numrows = $this->result->num_rows;

   if ($this->numrows < 1)
   {
      return 0;

   }
  else 
    {
        return 1;
    }
 }
}

?>

1 个答案:

答案 0 :(得分:0)

您用来连接MySQL的用户是否允许INSERT?换句话说,是MySQL权限导致问题,而不是PHP?尝试使用root用户进行测试以确定是否是这种情况(或检查PHPMyAdmin中的权限)。

相关问题