我的注册表格有问题。不插入第二个查询

时间:2010-12-01 20:36:59

标签: php mysql

首先抱歉我的语言。我正在为我的大学任务做一个购物车应用程序。我有注册的问题。问题是它正在插入第一个查询

$addsql = "INSERT INTO customers(forename, surname, add1, add2, add3, postcode, phone, email, registered)
                            VALUES('"
                            . strip_tags(addslashes($_POST['forenameBox'])) . "', '"
                            . strip_tags(addslashes($_POST['surnameBox'])) . "', '"
                            . strip_tags(addslashes($_POST['add1Box'])) . "', '"
                            . strip_tags(addslashes($_POST['add2Box'])) . "', '"
                            . strip_tags(addslashes($_POST['add3Box'])) . "', '"
                            . strip_tags(addslashes($_POST['postcodeBox'])) . "', '"
                            . strip_tags(addslashes($_POST['phoneBox'])) . "', '"
                            . strip_tags(addslashes($_POST['emailBox'])) . "',
                            1)";

                mysql_query($addsql);

并且它不插入第二个。

$customer_id = mysql_insert_id(); // Gets The id Of Last MySql INSERT Query 
                $insert_query = 'INSERT INTO logins (
                    username,
                    password,
                    customer_id
                    ) 
                    VALUES
                    (
                    "' . $_POST['userregBox'] . '", 
                    "' . md5($_POST['passregBox']) . '",
                    "' . $customer_id . '",
                    )';
                mysql_query($insert_query);
                header("Location: " . $basedir . "login.php?ok=1");

我尝试了不同的方法而没有结果。我正在使用Xammp。

这是完整的代码

    <?php
    session_start();
    require_once("db.php");
    /* Checking if user is logged in, if not redirecting to the main page */
    if(isset($_SESSION['SESS_LOGGEDIN']) == TRUE) {
        header("Location: " . $config_basedir);
    }
    if($_POST['login'])
    {
        $loginsql = "SELECT * FROM logins
        WHERE username = '" . $_POST['userBox'] . "' AND password = '" . $_POST['passBox'] . "'";
        $loginres = mysql_query($loginsql);
        $numrows = mysql_num_rows($loginres);

        if($numrows == 1)
        {
            $loginrow = mysql_fetch_assoc($loginres);

            session_register("SESS_LOGGEDIN");
            session_register("SESS_USERNAME");
            session_register("SESS_USERID");

            $_SESSION['SESS_LOGGEDIN'] = 1;
            $_SESSION['SESS_USERNAME'] = $loginrow['username'];
            $_SESSION['SESS_USERID'] = $loginrow['id'];

            $ordersql = "SELECT id FROM orders WHERE customer_id = " . $_SESSION['SESS_USERID'] . " AND status <2";
            $orderres = mysql_query($ordersql);
            $orderrow = mysql_fetch_assoc($orderres);

            session_register("SESS_ORDERNUM");
            $_SESSION['SESS_ORDERNUM'] = $orderrow['id'];
            header("Location: " . $config_basedir);
        }
        else
        {
            header("Location: http://" . $HTTP_HOST . $SCRIPT_NAME . "?error=1");
        }
    }
    if($_POST['register'])
    {
        $loginchecksql = "SELECT * FROM logins
        WHERE username = '" . $_POST['userBox'] . "'";
        $logincheckres = mysql_query($loginchecksql);
        $loginchecknumrows = mysql_num_rows($logincheckres);
        if($loginchecknumrows == 1)
        {
        header("Location: http://" . $HTTP_HOST . $SCRIPT_NAME . "?error=3");
        }
        else{
        if(empty($_POST['forenameBox']) ||
                    empty($_POST['surnameBox']) ||
                    empty($_POST['add1Box']) ||
                    empty($_POST['add2Box']) ||
                    empty($_POST['add3Box']) ||
                    empty($_POST['postcodeBox']) ||
                    empty($_POST['phoneBox']) ||
                    empty($_POST['userregBox']) ||
                    empty($_POST['passregBox']) ||
                    empty($_POST['emailBox']))
                {
                    header("Location: " . $basedir . "login.php?error=2");
                    exit;
                }

                $addsql = "INSERT INTO customers(forename, surname, add1, add2, add3, postcode, phone, email, registered)
                            VALUES('"
                            . strip_tags(addslashes($_POST['forenameBox'])) . "', '"
                            . strip_tags(addslashes($_POST['surnameBox'])) . "', '"
                            . strip_tags(addslashes($_POST['add1Box'])) . "', '"
                            . strip_tags(addslashes($_POST['add2Box'])) . "', '"
                            . strip_tags(addslashes($_POST['add3Box'])) . "', '"
                            . strip_tags(addslashes($_POST['postcodeBox'])) . "', '"
                            . strip_tags(addslashes($_POST['phoneBox'])) . "', '"
                            . strip_tags(addslashes($_POST['emailBox'])) . "',
                            1)";

                mysql_query($addsql);
                $customer_id = mysql_insert_id(); // Gets The id Of Last MySql INSERT Query 
                $insert_query = 'INSERT INTO logins (
                    username,
                    password,
                    customer_id
                    ) 
                    VALUES
                    (
                    "' . $_POST['userregBox'] . '", 
                    "' . md5($_POST['passregBox']) . '",
                    "' . $customer_id . '",
                    )';
                mysql_query($insert_query);
                header("Location: " . $basedir . "login.php?ok=1");
        }
    }
    else
    {
        require_once("header.php");
?>
    <?php 
    if($_GET['ok'] == 1) {
            echo "<b>Your registration was succesfull</b><p>Start shooping now</p>";

    }
    else
    {
    ?>
    <?php 
        if($_GET['error'] == 1) {
            echo "<b>Incorrect details, please try again</b>";
        }
    ?>
        <?php 
        if($_GET['error'] == 2) {
            echo "<b>Please fill all fields</b>";
        }
    ?>
    <?php 
        if($_GET['error'] == 3) {
            echo "<b>User name exist</b>";
        }
    ?>
    <div style="width:50%;float:left;">
    <fieldset style="width:90%;background:#fff; ">
    <legend>Customer Login</legend>
    <form action="<?php echo $SCRIPT_NAME; ?>" method="POST">
        <ul>
            <li>
                <fieldset>
                    <legend>Username</legend>
                    <div>
                        <input type="textbox" name="userBox" class="text" />
                    </div>
                    <p class="guidelines">Please enter your username</p>
                </fieldset>
            </li>
            <li>
                <fieldset>
                    <legend>Password</legend>
                    <div>
                    <input type="password" name="passBox" class="text" />
                    </div>
                    <p class="guidelines">Please enter your password</p>

                </fieldset>
            </li>
            <li>
                <button type="submit" name="login" value="login">Log In</button>
            </li>
        </ul>
    </form>
    </fieldset>
    </div>
    <div style="width:50%;float:right;">
    <fieldset style="width:95%;background:#fff; ">
    <legend>Register</legend>
    <form action="<?php echo $SCRIPT_NAME; ?>" method="POST">
        <ul>
            <li>
                <fieldset>
                    <legend>Username</legend>
                    <div>
                        <input type="textbox" name="userregBox" class="text" />
                    </div>
                    <p class="guidelines">Please enter your username</p>
                </fieldset>
            </li>
            <li>
                <fieldset>
                    <legend>Password</legend>
                    <div>
                    <input type="password" name="passregBox" class="text" />
                    </div>
                    <p class="guidelines">Please enter your password</p>

                </fieldset>
            </li>
            <li>
                <fieldset>
                    <legend>Delivery details</legend>
                    <table style="width:99%;">
        <tr>
            <td>Forename</td>
            <td><input type="text" name="forenameBox" class="text"></td>
        </tr>
        <tr>
            <td>Surname</td>
            <td><input type="text" name="surnameBox" class="text"></td>
        </tr>
        <tr>
            <td>House Number, Street</td>
            <td><input type="text" name="add1Box" class="text"></td>
        </tr>
        <tr>
            <td>Town/City</td>
            <td><input type="text" name="add2Box" class="text"></td>
        </tr>
        <tr>
            <td>County</td>
            <td><input type="text" name="add3Box" class="text"></td>
        </tr>
        <tr>
            <td>Postcode</td>
            <td><input type="text" name="postcodeBox" class="text"></td>
        </tr>
        <tr>
            <td>Phone</td>
            <td><input type="text" name="phoneBox" class="text"></td>
        </tr>
        <tr>
            <td>Email</td>
            <td><input type="text" name="emailBox"class="text"></td>
        </tr>

        </table>
                </fieldset>
            </li>
            <li>
                <button type="submit" name="register" value="Register">Register</button>
            </li>
        </ul>
    </form>
    </fieldset>
    </div>



<?php
    }
    }
    require_once("footer.php");
?>

1 个答案:

答案 0 :(得分:4)

你有一个额外的逗号。

更改

"' . $customer_id . '", 

"' . $customer_id . '"
在您的INSERT INTO LOGINS查询中