【问题标题】:How to insert data in multiple table with one query PDO mysql?如何使用一个查询 PDO mysql 在多个表中插入数据?
【发布时间】:2017-04-04 09:54:43
【问题描述】:

我需要在 PDO mysql 中使用一个查询在 mysql 数据库中插入数据。

我需要和 mysqli 多查询一样的东西。

这很好用

$insert =" 

insert into comments (user_id,comment) values('$user_id','$comment');

insert into comments2 (user_id,comment) values('$user_id','$comment');

$run = mysqli_multi_query($con,$insert);

但是如何在 PDO 中做到这一点

connection.php:

<?php
    class db {
    	private $conn;
    	private $host;
    	private $user;
    	private $password;
    	private $baseName;
    	private $port;
    	private $Debug;
     
        function __construct($params=array()) {
    		$this->conn = false;
    		$this->host = 'localhost'; //hostname
    		$this->user = 'root'; //username
    		$this->password = ''; //password
    		$this->baseName = 'hotwall'; //name of your database
    		$this->port = '3306';
    		$this->debug = true;
    		$this->connect();
    	}
     
    	function __destruct() {
    		$this->disconnect();
    	}
    	
    	function connect() {
    		if (!$this->conn) {
    			try {
    				$this->conn = new PDO('mysql:host='.$this->host.';dbname='.$this->baseName.'', $this->user, $this->password, array(PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8'));  
    			}
    			catch (Exception $e) {
    				die('Erreur : ' . $e->getMessage());
    			}
     
    			if (!$this->conn) {
    				$this->status_fatal = true;
    				echo 'Connection BDD failed';
    				die();
    			} 
    			else {
    				$this->status_fatal = false;
    			}
    		}
     
    		return $this->conn;
    	}
     
    	function disconnect() {
    		if ($this->conn) {
    			$this->conn = null;
    		}
    	}
    	
    	function getOne($query) {
    		$result = $this->conn->prepare($query);
    		$ret = $result->execute();
    		if (!$ret) {
    		   echo 'PDO::errorInfo():';
    		   echo '<br />';
    		   echo 'error SQL: '.$query;
    		   die();
    		}
    		$result->setFetchMode(PDO::FETCH_ASSOC);
    		$reponse = $result->fetch();
    		
    		return $reponse;
    	}
    	
    	function getAll($query) {
    		$result = $this->conn->prepare($query);
    		$ret = $result->execute();
    		if (!$ret) {
    		   echo 'PDO::errorInfo():';
    		   echo '<br />';
    		   echo 'error SQL: '.$query;
    		   die();
    		}
    		$result->setFetchMode(PDO::FETCH_ASSOC);
    		$reponse = $result->fetchAll();
    		
    		return $reponse;
    	}
    	
    	function execute($query) {
    		if (!$response = $this->conn->exec($query)) {
    			echo 'PDO::errorInfo():';
    		   echo '<br />';
    		   echo 'error SQL: '.$query;
    		   die();
    		}
    		return $response;
    	}
    }

我应该怎么做才能插入到另一个表中

$query = $bdd->execute('insert into comments (user_id,comment) 
values('$user_id','$comment')');

【问题讨论】:

标签: php mysql pdo


【解决方案1】:

使用 2 个查询,而不是一个。当然必须是参数化查询。

$stmt = $pdo->prepare("insert into comments (user_id,comment) values(?,?)");
$stmt->execute([$user_id,$comment]);

$stmt = $pdo->prepare("insert into comments2 (user_id,comment) values(?,?)");
$stmt->execute([$user_id,$comment]);

就是你需要的所有代码。

【讨论】:

  • $query = $bdd->execute('insert into cmets (user_id,comment) values('$user_id','$comment')');我添加了 $query = $bdd->execute('insert into cmets2 (user_id,comment) values('$user_id','$comment')'); ......它应该工作吗??
  • 您应该添加我在上面发布的确切代码。你永远不应该使用你的那个 db 类。
猜你喜欢
  • 2012-04-21
  • 1970-01-01
  • 2015-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多