MYSQL插入多个表不起作用

时间:2018-05-06 16:55:21

标签: php mysql

我有三个文件reg_form.php,dbconnection.php和insert.php。 提交表单时,数据不会插入数据库。我无法弄清楚为什么。最初我不知道如何在多个表中使用插入,但从这里接受了许多帖子的建议。不幸的是,我仍然没能使它工作,它让我疯了。这是到目前为止插入的sql代码。

<?php
include ("dbconnection.php");

if(file_exists("dbconnection.php")) {
echo"Connected to database successfully";
} else if(!file_exists("dbconnection.php")){
echo "Connection failed";  
}
    $forename = "forename";
    $surname = "surname";
    $address_line1 = "address_line1";
    $address_line2 = "address_line2";
    $address_line3 = "address_line3";
    $city = "city";
    $postcode = "postcode";
    $phone = "phone";
    $email = "email"; 
    $username = "username";
    $password = "password";
    $cpassword = "cpassword ";


    $query = "INSERT INTO users (username, 
    password)VALUES('$username','$password');";
    $query2 = "INSERT INTO users_details (forename, surname,address_line1, 
            address_line2, address_line3, city, postcode, phone, email)
            VALUES('$forename','$surname','$address_line1','$address_line2',
            '$address_line3','$city','$postcode','$phone','$email')";
    query ($dbconnection,$sql);
    ?>

1 个答案:

答案 0 :(得分:0)

好的问题解决了。我创建了一个存储过程,因为我正在执行INSERT INTO多个表,然后像这样调用它。        $ sql =&#34; CALL add_user(&#39;&#34;。$ username。&#34;&#39;,&#39;&#34;。$ password。&#34;&#39 ;,&#39;用户&#39;,        &#39;&#34;。$ forename。&#34;&#39;,&#39;&#34;。$ surname。&#34;&#39;,&#39;&#34; 。$ ADDRESS_LINE1&#34;&#39; ,        &#39;&#34;。$ address_line2。&#34;&#39;,&#39;&#34;。$ address_line3。&#34;&#39;,&#39;&#34; 。$ city。&#34;&#39;,&#39;&#34;。$ postcode。&#34;&#39;,        &#39;&#34;。$ phone。&#34;&#39;,&#39;&#34;。$ email。&#34;&#39;,&#39;&#34; .is_bool($ email_contact)&#34;&#39 ;,        #&39;&#34; .is_bool($ PHONE_CONTACT)&#34;&#39;)&#34 ;;         $ query = $ con&gt; prepare($ sql);

    $query->execute();