Ribi
Ribi

Reputation: 121

MYSQL Insert the same values in different table PHP

I have a problem with my script...

If I run this script, the value 'id' and 'name' will insert just in the first table "TABLE1", why this problem? the php doesn't get any error

<?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);
?>

somebody know why? i try a lot of method without success...

thank you very much!!!

Upvotes: 0

Views: 726

Answers (2)

Ulrich Horus
Ulrich Horus

Reputation: 350

Note that the number of columns must fit with the number of values. Change your code to the code bellow:

<?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);
?>

Upvotes: 0

John Conde
John Conde

Reputation: 219934

You're telling MySQL to update two columns and then giving it seven values. If you checked for errors using mysql_error() you would have caught this.

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

Upvotes: 5

Related Questions