使用2个查询登记表

时间:2014-01-02 06:54:51

标签: php mysql sql

这是我的人员表

ID FirstName LastName Email
1    aaa       aaa     aaa
2    bbb       bbb     bbb

这是我的会员表

ID username password
1    aaa      aaa
2    bbb      bbb

这是我的registration.php

<html>
    <head>
        <title>Registration</title>
    <script type="text/javascript">
    function validateForm()
    {
        var a=document.forms["reg"]["id"].value;
        var b=document.forms["reg"]["fname"].value;
        var c=document.forms["reg"]["lname"].value;
        var d=document.forms["reg"]["username"].value;
        var e=document.forms["reg"]["password"].value;
        var f=document.forms["reg"]["email"].value;

        if ((a===null || a==="") && (b===null || b==="") && (c===null || c==="") && (d===null || d==="") && (e===null || e==="") && (f===null || f==="")) {
            alert("All Field must be filled out");
            return false;
        }
        if  (a===null || a==="") {
            alert("ID must be filled out");
            return false;
        }
        if (b===null || b==="") {
            alert("First name must be filled out");
            return false;
        }
        if (c===null || c==="") {
            alert("Last name must be filled out");
            return false;
        }
        if (d===null || d==="") {
            alert("Username must be filled out");
            return false;
        }
        if (e===null || e==="") {
            alert("Password must be filled out");
            return false;
        }
        if (f===null || f==="") {
            alert("Email must be filled out");
            return false;
        }
    }
    </script>
    </head>

    <body>

<form name="reg" action="register_exec.php" onsubmit="return validateForm()" method="post" autocomplete="off">
    <table width="274" border="0" align="center" cellpadding="2" cellspacing="0">
    <tr>
    <td colspan="2">
    <div align="center">
    <?php
    if (!isset($_GET['remarks'])) {$remarks=""; }
        else {$remarks=$_GET['remarks']; }

    if ($remarks==null and $remarks=="") {
        echo 'Register Here';
    }
    if ($remarks=='success') {
        echo 'Registration Success';
    }
    ?>  
    </div></td>
    </tr>
    <tr>
    <td width="95"><div align="right">ID:</div></td>
    <td width="171"><input type="text" name="id" /></td>
    </tr>
    <tr>
    <td width="95"><div align="right">First Name:</div></td>
    <td width="171"><input type="text" name="fname" /></td>
    </tr>
    <tr>
    <td><div align="right">Last Name:</div></td>
    <td><input type="text" name="lname" /></td>
    </tr>
    <tr>
    <td><div align="right">Username:</div></td>
    <td><input type="text" name="username" /></td>
    </tr>
    <tr>
    <td><div align="right">Password:</div></td>
    <td><input type="text" name="password" /></td>
    </tr>
    <tr>
    <td><div align="right">Email:</div></td>
    <td><input type="text" name="email" /></td>
    </tr>

    <tr>
    <td><div align="right"></div></td>
    <td><input name="submit" type="submit" value="Submit" /></td>
    </tr>
    </table>
    </form>
    </body>
</html>

这是我的register_exec.php

 <?php
    $mysql_hostname = "localhost";
    $mysql_user = "root";
    $mysql_password = "password";
    $mysql_database = "lecturer";

    $conn = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) or die("Could not connect database");
    mysql_select_db($mysql_database, $conn) or die("Could not select database");
    ?>


    <?php
    session_start();

    $ID=$_POST['id'];
    $fname=$_POST['fname'];
    $lname=$_POST['lname'];
    $username=$_POST['username'];
    $password=$_POST['password'];
    $email=$_POST['email'];


    $query = ("INSERT INTO persons(ID, FirstName , LastName , Email)VALUES ('$ID' ,'$fname', '$lname', '$email')");
    $query1 = "INSERT INTO members (ID , username , password)VALUES ('$ID' , '$username' , '$password'";

    $result = mysql_query($query) or die(mysql_error());
    $result1 = mysql_query($query1) or die(mysql_error());


    if ($result && $result1) {
        header("location: Registration.php?remarks=success");
    }
    mysql_close($conn);
    ?>

我正在做的是使用一个表单并将数据插入到2个表中。我试过运行它我一直得到“你的SQL语法有错误;检查与你的MySQL服务器版本相对应的手册,以便在第1行附近使用正确的语法”

我问是否可以在一个php文件中执行2个查询?

4 个答案:

答案 0 :(得分:1)

你错过了大括号()。试试这个:

$query = "INSERT INTO persons(ID, FirstName , LastName , Email)VALUES ('$ID' ,'$fname', '$lname', '$email')";
$query1 = "INSERT INTO members (ID , username , password)VALUES ('$ID' , '$username' , '$password')";

答案 1 :(得分:0)

  1. 是的,您可以根据需要运行任意数量的查询。

  2. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1

  3. 这是查询错误,意味着您尝试运行的查询存在一些问题,而不是运行两个ueries。

    3错误即将发生,因为插入查询的正确语法如下:

        INSERT INTO table_name (column1, column2, column3,...) VALUES (value1, value2, value3,...)
    

    现在,将您的查询与此语法匹配,并尝试直接在您的xampp sql中运行它,这将解决问题。

    第一个查询中的额外括号(外括号)正在产生问题。删除它们,你很高兴

答案 2 :(得分:0)

您的第一个插入语句有问题

$query  = "INSERT INTO persons(ID, FirstName , LastName , Email)VALUES ('$ID' ,'$fname', '$lname', '$email')";
$query1 = "INSERT INTO members (ID , username , password)VALUES ('$ID' , '$username' , '$password')";

答案 3 :(得分:0)

没有规则在单个php文件中写入sql查询没有意味着你可以编写任意数量的