将用户数据发布到数据库

时间:2013-04-19 03:05:46

标签: php

我在将表单发布到数据库时遇到问题。第一部分是从一个表中检查CustomerID的查询,这部分工作正常。下一部分是将值发布到不同的表。表格上的一切正常,但它没有发布到“订单”表,也没有返回任何错误。我错过了什么?

<?php
    //Insert Into database
    $mysqli = @ new mysqli('127.0.0.1','root',null,'storefront');

    if (mysqli_connect_error())
    {
    echo 'Error: Could not connect to database. Please try again later.';
    exit;
    }

    $mysqli->select_db("storefront")
        or die("<p>Unable to select the database.</p>"
        ."<p>Error code ". mysqli_errno($mysqli)
        .": " . mysqli_error($mysqli)) . "</p>";

    $customerid = isset($_POST['customerid']);
    $custid = $_POST['customerid'];

    if($customerid)
    {
        $result = $mysqli->query("SELECT * FROM customer WHERE customerID = '$custid'");
        $row = $result->fetch_row();

        if (!$row)
        {
            echo "Customer Number not found.".mysql_error();
            exit;
        }
    }
    else
    {

        $tableName = "orders";
        $nullstr = "NULL";

        $SQLstring = "INSERT INTO $tableName VALUES
                ('".$nullstr."','".$customerid."', '".$blue."','".$green."','".$red."')";
        $result = $mysqli->query($SQLstring);   

        if ($result)
        {
            echo $mysqli->affected_rows ."  $tableName database.<br />";

            $mysqli->close();
        }
    }
?>

2 个答案:

答案 0 :(得分:0)

这一行:

$SQLstring = "INSERT INTO $tableName VALUES
            ('".$nullstr."','".$customerid."', '".$blue."','".$green."','".$red."')";

应该是

$SQLstring = "INSERT INTO $tableName VALUES
            ('".$nullstr."','".$custid."', '".$blue."','".$green."','".$red."')";

您正在if($customerid)的ELSE中调用您的插入内容 - 看起来您打算将其作为第一个查询的其他内容:

    if($customerid)
    {
        $result = $mysqli->query("SELECT * FROM customer WHERE customerID = '$custid'");
        $row = $result->fetch_row();

        if (!$row)
        {
            echo "Customer Number not found.".mysql_error();
            exit;
        }      
        else
        {
            $tableName = "orders";
            $nullstr = "NULL";

            $SQLstring = "INSERT INTO $tableName VALUES
                ('".$nullstr."','".$custid."', '".$blue."','".$green."','".$red."')";
            $result = $mysqli->query($SQLstring);   

            if ($result)
            {
                echo $mysqli->affected_rows ."  $tableName database.<br />";

                $mysqli->close();
            }
        }
    }

$ blue,$ green和$ red在任何地方都没有定义,但是没有相关。

也许我在这里错过了你的意图。如果是,请告诉我,我会再次审核并更新。

答案 1 :(得分:0)

尝试替换

$customerid$custid

('".$nullstr."','".$customerid."', '".$blue."','".$green."','".$red."')";

('".$nullstr."','".$custid."', '".$blue."','".$green."','".$red."')";