简体   繁体   English

使用PHP脚本更新MySQL表

[英]Updating MySQL table with a PHP script

I am developing an android application, where I want to insert the address of the user to the database calling a PHP script on a website. 我正在开发一个android应用程序,我想在其中将用户的地址插入调用网站上PHP脚本的数据库中。

In the first try, it has to insert the address into the database and after that, it has update the same tuple. 在第一次尝试中,它必须将地址插入数据库,然后再更新相同的元组。

This is the PHP script I have, but it gives an error in line number 36 (Call to a member function bind_param()). 这是我拥有的PHP脚本,但是在行号36(调用成员函数bind_param())中给出了错误。

Nevertheless, insertion is working perfectly fine. 但是,插入工作正常。

class DbOperations1{

    private $con;
    function __construct(){

        require_once dirname(__FILE__).'/DbConnect.php';

    $db = new DbConnect();
    $this->con = $db->connect();
    }

    public function createUser ($name, $email, $password) {
        if($this->isUserExist($name, $email))
        {
            return 0;
        }else{
            $password = md5($password);
            $stmt = $this->con->prepare("INSERT INTO `test` (`id`,`name`, `email`, `password`) VALUES (NULL, ? , ? , ? );"); 
            $stmt->bind_param ("sss", $name, $email, $password);

            if ($stmt->execute()){
                return 1;
            } else {
                return 2;
            }
        }

    }

    public function Address($id_user, $address, $road, $city, $country) {
        if($this->isAddressExist($id_user, $address))
        {   
            $stmt = $this->con->prepare("UPDATE address a, users u SET `address`=$address,`road`=$road,`city`=$city,`country`=$country WHERE a.id_user=u.id"); 
            $stmt->bind_param("ssss", $address, $road, $city, $country);
            if ($stmt->execute()){
                return 2;
            } else {
                return 3;
            }
        }else{
            $stmt = $this->con->prepare("INSERT INTO `address` (`id_address`, `id_user`,`address`, `road`, `city`, `country`) VALUES (NULL, ?, ? , ? ,? ,? );"); 
            $stmt->bind_param ("sssss", $id_user, $address, $road, $city, $country);

            if ($stmt->execute()){
                return 0;
            } else {
                return 1;
            }
        }

    }

        public function userLogin($email, $password){
        $password = md5($password);
        $stmt = $this->con->prepare("SELECT id FROM users WHERE email = ? AND password = ?");
        $stmt->bind_param("ss",$email,$password);
        $stmt->execute();
        $stmt->store_result(); 
        return $stmt->num_rows > 0; 
        }

        public function getUserByemail($email){
        $stmt = $this->con->prepare("SELECT * FROM users WHERE email = ?");
        $stmt->bind_param("s",$email);
        $stmt->execute();
        return $stmt->get_result()->fetch_assoc();
    }

        private function isUserExist($name, $email){
        $stmt = $this->con->prepare("SELECT id FROM test WHERE name = ? OR email = ?");
        $stmt->bind_param("ss", $name, $email);
        $stmt->execute(); 
        $stmt->store_result(); 
        return $stmt->num_rows > 0; 
    }

        private function isAddressExist($id_user){
        $stmt = $this->con->prepare("SELECT id_address FROM address WHERE id_user = ?");
        $stmt->bind_param("s", $id_user);
        $stmt->execute(); 
        $stmt->store_result(); 
        return $stmt->num_rows > 0; 
    }


}

According to your comments, the error is because of call to prepare failing. 根据您的评论,该错误是由于调用prepare失败而引起的。 Following code will allow to get more info: 以下代码将允许获取更多信息:

        if($this->isAddressExist($id_user, $address))
        {   
            $stmt = $this->con->prepare("UPDATE address a, users u SET `address`=$address,`road`=$road,`city`=$city,`country`=$country WHERE a.id_user=u.id");
            if($stmt != False) {
              $stmt->bind_param("ssss", $address, $road, $city, $country);
              if ($stmt->execute()){
                  return 2;
              } else {
                  return 3;
              }
            } else {
              // this line will give an insight into an error message
              echo $this->con->error;
            }
        }

After getting the error message from MySQL: 从MySQL获取错误消息后:

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'road=Sarak,city=Khost,country=Afghanistan WHERE a.id_user= 10' at line 1

we locate exact error location and there is $address variable used, while it shall be ? 我们找到确切的错误位置,并且使用了$address变量,而它应该是? ! Such statement can't be prepared. 这样的陈述无法准备。

Using following update statement should fix it: 使用以下更新语句可以解决该问题:

$stmt = $this->con->prepare("UPDATE address a, users u SET `address`=?,`road`=?,`city`=?,`country`=? WHERE a.id_user=u.id");
public function Address($id_user, $address, $road, $city, $country) {
        if($this->isAddressExist($id_user))
    {   
        $stmt = $this->con->prepare("UPDATE address SET `address`=?,`road`=?,`city`=?,`country`=? WHERE id_user= ?");
        if($stmt != False) {
          $stmt->bind_param("ssss", $address, $road, $city, $country,$id_user);
          if ($stmt->execute()){
              return 2;
          } else {
              return 3;
          }
        } else {
          // hopefully this line will give an insight into an error message
          echo $this->con->error;
        }
    } else{
            $stmt = $this->con->prepare("INSERT INTO `address` (`id_address`, `id_user`,`address`, `road`, `city`, `country`) VALUES (NULL, ?, ? , ? ,? ,? );"); 
            $stmt->bind_param ("sssss", $id_user, $address, $road, $city, $country);

            if ($stmt->execute()){
                return 0;
            } else {
                return 1;
            }
        }

    }

This should work. 这应该工作。 When it comes to parameter binding you have to use ? 当涉及参数绑定时,您必须使用? (question mark ) instead of the parameter name and bind the parameters by name in the correct order in bind_param function. (问号)而不是参数名称,并在bind_param函数中按正确的顺序按名称绑定参数。

$stmt = $this->con->prepare("UPDATE address SET `address`=$address,`road`=$road,`city`=$city,`country`=$country WHERE a.id_user=u.id");

尝试更新一张桌子

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

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