0

我有一个奇怪的问题。

我尝试通过 PHP 脚本在 MySQL 中输入以下数据: 1. 到达(日期) 2. 出发(日期) 3. 价格(int,11)

PHP 如下:

if($_POST['formSubmit'] == "Submit") 
{
    $errorMessage = "";

    if(empty($_POST['type'])) 
    {
        $errorMessage .= "<li>Gelieve een type in te voeren!</li>";
    }

    $hotel = $_POST['hotel'];
    $type = $_POST['type'];
    $prijs = $_POST['prijs'];
    $arrival = $_POST['arrival'];
    $departure = $_POST['departure'];

    if(empty($errorMessage)) 
    {

        $sql = "INSERT INTO rooms (hotel, type, prijs, arrival, departure) VALUES (".
                        PrepSQL($hotel) . ", ".
                        PrepSQL($type) . ", ".
                        PrepSQL($arrival) . ", ".
                        PrepSQL($departure) . ", ".
                        PrepSQL($prijs) . ")";
        mysql_query($sql);          
        include("room-insert.php");
        exit();
    }
}


function PrepSQL($value)
{
    // Stripslashes
    if(get_magic_quotes_gpc()) 
    {
        $value = stripslashes($value);
    }

    // Quote
    $value = "'" . mysql_real_escape_string($value) . "'";

    return($value);
}

HTML是这样的:

<form action="<?php echo htmlentities($_SERVER['PHP_SELF']); ?>" method="post">

        <tr>
        <p>
            <label for='hotel'>Hotel</label><br/>
            <input name="hotel" maxlength="50" value="<?=$naam;?>" />
        </p>
        </tr>
        <tr>
        <td>
        <p>
            <label for='type'>Type</label><br/>
            <input type="text" name="type" maxlength="50" value="<?=$type;?>" />
        </p>
        </td>
        </tr>


        <tr>
        <td>
        <p>
            <label for='prijs'>Prijs</label><br/>
            <input type="text" name="prijs" maxlength="50" value="<?=$prijs;?>" />
        </p>
        </td>
        </tr>

        <tr>
        <td>
        <p>
            <label for='arrival'>Datum check-in</label><br/>
            <input type="date" name="arrival" maxlength="50" value="<?=$arrival;?>" />
        </p>
        </td>
        </tr>

        <tr>
        <td>
        <p>
            <label for='departure'>Datum check-out</label><br/>
            <input type="date" name="departure" maxlength="50" value="<?=$departure;?>" />
        </p>
        </td>
        </tr>

        <tr>
        <td>
                    <input type="submit" name="formSubmit" value="Submit" />
        </td>
        </tr>


    </form>

我以以下形式输入:价格(=prijs):50 到达:2012-09-12 出发:2012-09-30

这在 MySQL 中是这样的:

价格:2012 到达:2012-09-30 出发:50

于是,彻底乱了...

我在 MySQL、PHP 和 HTML 中尝试了几件事,所有结果都相同或更糟,我现在处于一个阶段,我不知道如何解决这个问题......

谢谢你的帮助!

大安

4

3 回答 3

0

有你的错误代码:

 $sql = "INSERT INTO rooms (hotel, type, prijs, arrival, departure) VALUES (".
                        PrepSQL($hotel) . ", ".
                        PrepSQL($type) . ", ".
                        PrepSQL($arrival) . ", ".
                        PrepSQL($departure) . ", ".
                        PrepSQL($prijs) . ")";

您以错误的顺序连接变量,应该是这样的:

 $sql = "INSERT INTO rooms (hotel, type, prijs, arrival, departure) VALUES (".
                        PrepSQL($hotel) . ", ".
                        PrepSQL($type) . ", ".
                        PrepSQL($prijs) . ")";
                        PrepSQL($arrival) . ", ".
                        PrepSQL($departure) . ", ".
于 2012-08-27T12:33:41.247 回答
0

您的问题似乎在 INSERT SCRIPT 部分。

尝试改变

$sql = "INSERT INTO rooms (hotel, type, prijs, arrival, departure) VALUES (". 
    PrepSQL($hotel) . ", ". 
    PrepSQL($type) . ", ". 
    PrepSQL($arrival) . ", ". 
    PrepSQL($departure) . ", ". 
    PrepSQL($prijs) . ")"; 

$sql = "INSERT INTO rooms (hotel, type, prijs, arrival, departure) VALUES (". 
    PrepSQL($hotel) . ", ". 
    PrepSQL($type) . ", ". 
    PrepSQL($prijs) . ", ". 
    PrepSQL($arrival) . ", ". 
    PrepSQL($departure) . ")"; 
于 2012-08-27T12:35:25.720 回答
0

你搞乱了值的顺序。

您的插入:

$sql = "INSERT INTO rooms (hotel, type, prijs, arrival, departure) VALUES (".
                        PrepSQL($hotel) . ", ".
                        PrepSQL($type) . ", ".
                        PrepSQL($arrival) . ", ".
                        PrepSQL($departure) . ", ".
                        PrepSQL($prijs) . ")";
  1. 酒店
  2. 类型
  3. 价格
  4. 到达
  5. 离开

您按以下顺序插入数据:

  1. 酒店
  2. 类型
  3. 到达
  4. 离开
  5. 价格
于 2012-08-27T12:35:49.470 回答