将数据插入多个表

时间:2012-04-15 14:42:00

标签: php mysql

我正在努力掌握PHP中多查询的概念,并且必须做错事但我不确定是什么。我将数据存储在来自其他表单的会话中。

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

还有更多的表要插入,但不想用不必要的重复来“敲打”问题。如果有人能够解释我做错了什么,那么非常感谢,提前谢谢。

3 个答案:

答案 0 :(得分:2)

要使多个查询起作用,所有查询必须用分号;分隔,因此在您的情况下:

$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')";

答案 1 :(得分:0)

试试这个,删除括号并在查询之间添加分号

$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');";

答案 2 :(得分:0)

没有mysql_multiquery()这样的东西,这就是它返回的原因:

  

致命错误:在...上调用未定义的函数mysql_multiquery()

函数mysql_multiquery()未定义,这意味着它不存在。

相反,使用mysqli_multi_query,但是你必须使用更安全的mysql i 方法。还要尝试使用分号(;)来结束查询。

// 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";