PurpleSmurph
PurpleSmurph

Reputation: 2107

Inserting Data into multiple tables

I'm struggling to grasp the concept of multi query in PHP and must be doing something wrong but I'm not sure what. I have data stored in sessions coming from other forms.

<?php 
    $conn=mysql_connect("database","username","password");
    mysql_select_db("host",$conn);

    session_start();

    $insert_query=("INSERT INTO testone_tbl 
                      (age,hours,flexibility,fastpaced,retailexp,workedus,conviction,permit,education)
                    VALUES 
                      ('$age','$hours','$flexibility','$fastpaced','$retailexp','$workedus','$conviction,'$permit','$education') 
                    INSERT INTO equality_tbl 
                      (age,ethnic,disability)
                    VALUES ('$age','$ethnic'.'$disability')");

    mysql_multiquery($insert_query);
?>

There are many more tables to be inserted but didn't want to 'bung' up the question with needless repeats. If anyone can explain what I am doing wrong it would be greatly appreciated, thanks in advance.

Upvotes: 0

Views: 593

Answers (3)

Richard de Wit
Richard de Wit

Reputation: 7452

There is no such thing as mysql_multiquery(), that's why it returns this:

Fatal error: Call to undefined function mysql_multiquery() on...

The function mysql_multiquery() is undefined, which means it doesn't exist.

Instead, use mysqli_multi_query, but therefore you have to use the safer mysqli methods. Also try to make it a habit to end queries with a semicolon (;).

// Create mysqli connection
$conn = mysqli_connect("server", "user", "password", "database");

$query  = "INSERT INTO testone_tbl 
             (age,hours,flexibility,fastpaced,retailexp,workedus,conviction,permit,education)
           VALUES 
             ('$age','$hours','$flexibility','$fastpaced','$retailexp','$workedus','$conviction,'$permit','$education');";
$query .= "INSERT INTO equality_tbl 
             (age,ethnic,disability)
           VALUES 
             ('$age','$ethnic'.'$disability');";

// Execute queries
if(mysqli_multi_query($conn, $query)) 
    echo "Success!";
else
    echo "Error";

Upvotes: 0

Andreas Linden
Andreas Linden

Reputation: 12721

try this, remove the brackets and add a semicolon between the queries

$insert_query="
    INSERT INTO testone_tbl (age,hours,flexibility,fastpaced,retailexp,
    workedus,conviction,permit,education)
    VALUES ('$age','$hours','$flexibility','$fastpaced','$retailexp','$workedus',
    '$conviction,'$permit','$education');
    INSERT INTO equality_tbl 
    (age,ethnic,disability)
    VALUES ('$age','$ethnic'.'$disability');";

Upvotes: 0

Randy the Dev
Randy the Dev

Reputation: 26690

For a multiquery to work, all queries must be separated by a semicolon ;, so in your case:

$insert_query = "
    INSERT INTO testone_tbl (age,hours,flexibility,fastpaced,retailexp,
    workedus,conviction,permit,education)
    VALUES ('$age','$hours','$flexibility','$fastpaced','$retailexp','$workedus',
    '$conviction,'$permit','$education'); <--Semicolon
    INSERT INTO equality_tbl 
    (age,ethnic,disability)
    VALUES ('$age','$ethnic'.'$disability')";

Upvotes: 2

Related Questions