将相同的值插入两个不同的表中

时间:2014-07-08 01:23:17

标签: php pdo

我试图在pdo中插入两个表中相同的值,既是actual_quote,又是如何进行此操作? 它只是插入选民,而不是数据

<?php
$db_name = 'submissions';
$db_user = 'root';
$db_pass = '';
$db_host = 'localhost';
$db = new PDO('mysql:host = localhost;dbname=submissions', $db_user, $db_pass);
$formtype = (empty($_POST['formtype'])) ? : $_POST['formtype'] ;
$poster = (empty($_POST['poster'])) ? : $_POST['poster'] ;
$actual_quote = (empty($_POST['actual_quote'])) ? : $_POST['actual_quote'] ;
$query = $db->prepare("INSERT INTO `data` (actual_quote, poster, formtype) VALUES (    :actual_quote, :poster, :formtype)");
$query->bindParam(':formtype', $formtype, PDO::PARAM_STR); 
$query->bindParam(':poster', $poster, PDO::PARAM_STR); 
$query->bindParam(':actual_quote', $actual_quote, PDO::PARAM_STR); 
$query->execute();
$query1 = $db->prepare("INSERT INTO voters (actual_quote) VALUES ( :actual_quote)") or die(mysql_error());
$query1->bindParam(':actual_quote', $actual_quote, PDO::PARAM_STR); 
$query1->execute();
?>

我修好了,我不得不乱用数据库

1 个答案:

答案 0 :(得分:1)

or die(mysql_error()); ???你不应该把mysql和PDO混合在一起:

if(isset($_POST['formtype'], $_POST['poster'], $_POST['actual_quote'])){
//post data
$formtype = $_POST['formtype'];
$poster = $_POST['poster'] ;
$actual_quote = $_POST['actual_quote'] ; 

//credential
$db_name = 'submissions';
$db_user = 'root';
$db_pass = '';
$db_host = 'localhost';

//connection
$db = new PDO('mysql:host = localhost;dbname=submissions', $db_user, $db_pass);
//very very important
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 

//data query
$query = $db->prepare("INSERT INTO `data` (actual_quote, poster, formtype) VALUES (:actual_quote, :poster, :formtype)");
$query->bindValue(':formtype', $formtype, PDO::PARAM_STR); 
$query->bindValue(':poster', $poster, PDO::PARAM_STR); 
$query->bindValue(':actual_quote', $actual_quote, PDO::PARAM_STR); 
$query->execute();

//voters query
$query = $db->prepare("INSERT INTO voters (actual_quote) VALUES ( :actual_quote)");
$query->bindValue(':actual_quote', $actual_quote, PDO::PARAM_STR); 
$query->execute();
}