MYSQL在不同的表PHP中插入相同的值

时间:2014-01-17 16:57:27

标签: php mysql sql database

我的剧本有问题......

如果我运行这个脚本,值'id'和'name'将只插入第一个表“TABLE1”,为什么会出现这个问题? php没有任何错误

<?php    
$id= $_GET['id'];
$name= $_GET['name'];

if(empty($id) || empty($name)){
        echo "errore";
}else{
        $sql="INSERT INTO `$db`.`TABLE1` (id, name)
        VALUES
        ('','$id','$name','','','','')";
        mysql_query($sql);       

        $sql1="INSERT INTO `$db`.`TABLE2` (id, name)
        VALUES
        ('','$id','$name','','','','')";
        mysql_query($sql1);

        $sql2="INSERT INTO `$db`.`TABLE3` (id, name)
        VALUES
        ('','$id','$name','','','','')";
        mysql_query($sql2);                               
}
mysql_close($con);
?>

SCRIPT ONLINE:

<?php
require 'client/database.php';

$uid= mysql_escape_string($_GET['uid']);
$username= mysql_escape_string($_GET['username']);
$email= mysql_escape_string($_GET['email']);

$con = mysql_connect($host, $dbu, $dbp);
if (!$con){
        die('Could not connect: ' . mysql_error());
        }

mysql_select_db($db, $con);
//end opening connection

if(empty($uid) || empty($username)){
        echo "errore";
}else{
//artist info
        $sql="INSERT INTO TABLE1 (UIDfacebook, username, email)
        VALUES
        ('$uid','$username','$email')";
        mysql_query($sql);       

        $sql1="INSERT INTO TABLE2 (UIDfacebook, username)
        VALUES
        ('$uid','$username')";
        mysql_query($sql1);


        $sql2="INSERT INTO TABLE3 (UIDfacebook)
        VALUES
        ('$uid')";
        mysql_query($sql2);


        $sql3="INSERT INTO TABLE4 (UIDfacebook)
        VALUES
        ('$uid')";
        mysql_query($sql3);

        $sql4="INSERT INTO TABLE5 (UIDfacebook)
        VALUES
        ('$uid')";
        mysql_query($sql4);

        echo "GOOD...";

}
//close connection
mysql_close($con);
?>

有人知道为什么吗?我尝试了很多没有成功的方法......

非常感谢!!!

2 个答案:

答案 0 :(得分:5)

你告诉MySQL更新两列,然后给它七个值。如果您使用mysql_error()检查了错误,那么您会发现错误。

    $sql="INSERT INTO `$db`.`TABLE1` (id, name)
    VALUES
    ('$id','$name')";
    mysql_query($sql);     
    if (mysql_error()) {
         echo mysql_error();
    }   

答案 1 :(得分:0)

请注意,列数必须符合值的数量。将代码更改为以下代码:

<?php    
$id= mysql_real_escape_string($_GET['id']);
$name= mysql_real_escape_string($_GET['name']);

if(empty($id) || empty($name)){
        echo "errore";
}else{
        $sql="INSERT INTO `$db`.`TABLE1` (id, name)
        VALUES
        ('$id','$name')";
        mysql_query($sql);       

        $sql1="INSERT INTO `$db`.`TABLE2` (id, name)
        VALUES
        ('$id','$name')";
        mysql_query($sql1);

        $sql2="INSERT INTO `$db`.`TABLE3` (id, name)
        VALUES
        ('$id','$name')";
        mysql_query($sql2);                               
}
mysql_close($con);
?>