简体   繁体   English

第1行的'customerid'列的整数值不正确:“”

[英]Incorrect integer value: '' for column 'customerid' at row 1

I am working on my project for school and is now stuck on this problem that I hope someone here my point me in the right direction. 我正在为我的学校项目进行工作,但现在仍停留在这个问题上,希望这里有人向我指出正确的方向。

I am designing a booking system which uses a web front and MySQL database. 我正在设计一个使用Web Front和MySQL数据库的预订系统。 I have a few tables: Customers, Seats, Price, Booking and Screening. 我有几张桌子:客户,席位,价格,预订和筛选。 I am trying to insert data into the booking table from the other tables using there primary keys. 我正在尝试使用那里的主键将数据从其他表插入到预订表中。 however I keep getting the following error message: Incorrect integer value: '' for column 'customerid' at row 1 I have search every where but doesn't seem to get any solution. 但是,我不断收到以下错误消息:不正确的整数值:第1行的“ customerid”列的“”,我到处搜索但似乎没有任何解决方案。 I have copied my code below. 我已经在下面复制了我的代码。

<?php                           
$customerid=$_POST['customerid'];
$screeningid=$_POST['screeningid'];
$seatid=$_POST['seatid'];
$priceid=$_POST['priceid'];
$status=$_POST['status'];

$query = "INSERT INTO `booking`(bookingid, customerid, screeningid,        seatid,     priceid, bookingdate, status) 
VALUES(NULL, '". mysql_real_escape_string($customerid)."', '".   mysql_real_escape_string($screeningid)."', '". mysql_real_escape_string($seatid)."','". mysql_real_escape_string($priceid)."', 'DateTime()', '". mysql_real_escape_string($status)."')";

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

// if successfully insert data into database, displays   message "Successful". 
if($result)
{
echo "<p>success</p>"; 
echo "<BR>"; 
} 
else 
    { 
echo mysql_error();
}
?> 

This is my Form: 这是我的表格:

<div id="content"> 
        <h2>Enter Booking Details Below</h2>   

        <form name="reg_form" action="bookingecho.php?action=add type=booking" onsubmit="return validate_reg()" method="POST" >
        <table>
            <tr>
            <td>Customer</td>
            <td> <select name="customerid">             
        <?php
            //Perform database query
            $query = ("SELECT * FROM customers
                ORDER BY customerid DESC");

            $result = mysql_query($query, $connection) or die (mysql_error());  
            // populate the select options with the results

            while ($row = mysql_fetch_assoc($result)) 
                {
                //extract column
                $customerid = $row['customerid'];
                $fname = $row['fname'];
                $lname = $row['lname'];
                $telephone = $row['telephone'];
                //use
                echo "<option value>$customerid $fname $lname $telephone</option>";
                }                   
        ?>
            </select></td></tr>

        <tr>
            <td>Screening</td>
            <td> <select name="screeningid">                
        <?php
            //Perform database query
            $query = ("SELECT * FROM screening");

            $result = mysql_query($query, $connection) or die (mysql_error());  
            // populate the select options with the results

            while ($row = mysql_fetch_assoc($result)) 
                {
                //extract column
                $screeningid = $row['screeningid'];
                $day = $row['day'];
                $screeningdate = $row['screeningdate'];
                $filmtitle = $row['filmtitle'];


                //use
                echo "<option value>$screeningid $day $screeningdate $filmtitle</option>";
                }                   
        ?>
        </select></td></tr>             

        <tr>
            <td>Seat</td>
            <td> <select name="seatid">             
        <?php

            //Perform database query
            $query = ("SELECT seats.seatid, seats.seatnumber, seats.seatclass
                    FROM seats
                    WHERE seatid 
                    NOT IN (SELECT seatid FROM booking 
                    WHERE screeningid = '$screeningid')
                    ORDER BY `Seats`.`seatid` ASC");

            $result = mysql_query($query, $connection) or die (mysql_error());  
            // populate the select options with the results

            while ($row = mysql_fetch_assoc($result)) 
                {
                //extract column
                $seatid = $row['seatid'];
                $seatnumber = $row['seatnumber'];
                $seatclass = $row['seatclass'];


                //use
                echo "<option value>$seatid $seatnumber $seatclass</option>";
                }                   
        ?>
        </select></td></tr> 

        <tr>
            <td>Concession</td>
            <td> <select name="priceid">                
        <?php
            //Perform database query
            $query = ("SELECT * FROM price");

            $result = mysql_query($query, $connection) or die (mysql_error());  
            // populate the select options with the results

            while ($row = mysql_fetch_assoc($result)) 
                {
                //extract column
                $priceid = $row['priceid'];
                $concession = $row['concession'];
                $cost = $row['cost'];


                //use
                echo "<option value>$priceid $concession $cost</option>";
                }                   
        ?>
        </select></td></tr>

        <tr>
        <td>Status</td>
        <td>
            <input type= radio name="status" value="Booked"> Booked
            <input type= radio name="status" value="Reserved"> Reserved

        </td>
        </tr>
        </select></td></tr>
        </table>            

              <p align="center">                     
                 <td><input type="submit" name="submit" id= "submit" value="Add"/></td>
                 <input type="reset" value="Reset Form">
              </p>


        </form>     
    </div>

The error message means you are getting an "empty string" ( '' ) for customerid , which simply means that your first chunk of code is not getting any value at all for that field when the form is submitted. 该错误消息意味着你得到一个“空字符串”( ''的) customerid ,这仅仅意味着表单提交,当你的代码的第一块没有得到所有该字段的任何值。

Here is the problem: 这是问题所在:

echo "<option value>$customerid $fname $lname $telephone</option>";

The values between <option> and </option> are what will be displayed to the end-user, but they will not be submitted with your form, which means they won't be available to that first chunk of code. <option></option>之间的值将显示给最终用户,但不会与您的表单一起提交 ,这意味着它们对于第一段代码将不可用。

To submit the customerid, you have to put it into the value part: 要提交customerid,您必须将其放入value部分:

echo "<option value=$customerid>$customerid $fname $lname $telephone</option>";

Use intval() for integer values. 使用intval()作为整数值。
Use mysql_real_escape_string on strings, Never for integers. 在字符串上使用mysql_real_escape_string ,从不用于整数。
Single quotes around integer values in a mySQL query is optional. mySQL查询中的整数值周围的单引号是可选的。
Because intval() guarantees $customerid is an integer value, the quotes are not necessary and will never generate an error. 因为intval()保证$customerid是整数值,所以引号不是必需的,并且永远不会产生错误。

I have only included the two lines of code directly related to your customer id. 我只包括了与您的客户ID直接相关的两行代码。 The same likely applies to the other values as well. 同样的可能也适用于其他值。

$customerid=intval($_POST['customerid']);

$query = "INSERT INTO `booking`
(`bookingid`, `customerid`, `screeningid`,`seatid`,`priceid`, `bookingdate`, `status`)  
VALUES(NULL,$customerid,'$screeningid','$seatid','$priceid',CURDATE(),'$status')";  

Change: 更改:

<input name="status" value="Booked" type="radio"> Booked
<input name="status" value="Reserved" type="radio"> Reserved

To: 至:

<input name="status" value="1" type="radio"> Booked
<input name="status" value="2" type="radio"> Reserved

Always submit integer values greater then zero whenever possible. 只要有可能,始终提交大于零的整数值。 They are easy to validate. 它们很容易验证。 To get the text back: 取回文字:

$statuses = array('unknown','booked','reserved')
$strStatus = $statuses[$status];

UPDATE FOREIGN KEY CONSTRAINT ERROR 更新外键约束错误

A CONSTRAINT ERROR says you do not have a customer record for the added booking table. 约束错误表明您没有所添加预订表的客户记录。 Or the foreign key is wrong. 或外键错误。 The foreign key is not needed. 不需要外键。

You could combine the customer add, lookup and booking INSERT at the same time. 您可以同时结合客户添加,查找和预订INSERT。

Rather than some sort of login to create or retrieve the customer record get the booking then during the last step in the booking process just ask for the phone number. 无需某种创建或检索客户记录的登录名即可进行预订,然后在预订过程的最后一步中索取电话号码即可。 Personally I do not care if someone were to query the system with my phone number and found out which seat I have. 就我个人而言,我不在乎是否有人用我的电话号码查询系统并找出我的座位。 Some might. 有些可能。 But asking for aa login in before getting the booking is a nuisance and a road block to finalizing the booking. 但是在获得预订之前要求先登录是个麻烦事,也是完成预订的障碍。

What I did was add the security ID 我所做的就是添加安全ID

If they want to secure their seats with a password let it be their choice. 如果他们想用密码固定座位,那就选择他们。

If the record does not exist, then ask for their name AFTER the seats are booked. 如果记录不存在,请在预订座位后询问他们的名字。

//$customerid = intval($_POST['customerid']);
$screeningid = intval($_POST['screeningid']);
$seatid = intval($_POST['seatid']);
$priceid = intval($_POST['priceid']);
$status = intval($_POST['status']);
$fname = mysql_real_escape_string($_POST['status']);
$lname = mysql_real_escape_string($_POST['lname']);
$telephone = intval(preg_replace('/[^D]/','',$_POST['telephone']));

//must have UNIQUE Index on `telephone`

$sql = "INSERT INTO `customer` (`customerid`,`fname`, `lname`, `telephone`)
VALUES(NULL,'', '', $telephone)";
$result = mysql_query($sql);
if(mysql_insert_id()){
  $customerid = mysql_insert_id();
}
else{
  list($customerid, $fname`, $lname,$id) = mysql_fetch_array(mysql_query(
  "SELECT `customerid`,`fname`, `lname`, `telephone`,`id` 
   FROM `customer` WHERE  `telephone`=$telephone"),MYSQL_NUM);
}

$query = "INSERT INTO `booking` 
(`bookingid`, `customerid`, `screeningid`,`seatid`,`priceid`, `bookingdate`, `status`)
VALUES(NULL,$customerid,$screeningid,$seatid,$priceid,CURDATE(),$status)";

echo <<<EOT
<p>Your seats are booked.</p>
<form action="update.php" method="post">
  <label>Last:</label>
  <input type="text" name="lname" value="$lname" />
  <br/>
  <label>First:</label>
  <input type="text" name="fname" value="$fname" />
  <br/>
  <label>Phone:</label>
  <input type="tel" name="telephone" value="$telephone" />
  <br/>
  <p class="footnote">Security ID is optional</p>
  <label>Security ID:</label>
  <input type="text" name="id" value="$id" />
  <br/>
  <input type="hidden" name="phone" value="$telephone" />
  <br/>
  <div class="footnote">

    If you want to keep your booking secure then enter a security id.
    <br>If blank, no security ID will be necessary to retrieve your seats in the future.
    <br/>This can be any number (e.g. PIN) word, or any characters.
    <br/>Maximum 16 characters.</p>
    <p>Do NOT use an existing high security password (e.g. your banking password)</p>

    <h4>If you would like your seats sent to you via text,<br/>Select your mobile carrier<br/>This will also verify you entered the correct phone number</h4> 
  </div>
  <label>Mobile Carrier</label>
  <select>
    <option value="">No Text / Land Line</option>
    <option value="@message.alltel.com">Alltel</option>
    <option value="@paging.acswireless.com">Ameritech</option>
    <option value="@mmode.com">ATT Wireless</option>
    <option value="@bellsouth.cl">Bellsouth</option>
    <option value="@myboostmobile.com">Boost</option>
    <option value="@mobile.celloneusa.com">CellularOne</option>
    <option value="@mobile.mycingular.com">Cingular</option>
    <option value="@sms.edgewireless.com">Edge Wireless</option>
    <option value="@mymetropcs.com">Metro PCS</option>
    <option value="@messaging.nextel.com">Nextel</option>
    <option value="@mobile.celloneusa.com">O2</option>
    <option value="@mobile.celloneusa.com">Orange</option>
    <option value="@qwestmp.com">Qwest</option>
    <option value="@pcs.rogers.com">Rogers Wireless</option>
    <option value="@messaging.sprintpcs.com">Sprint PCS</option>
    <option value="@teleflip.com">Teleflip</option>
    </optgroup>
    <option value="@msg.telus.com">Telus Mobility</option>
    <option value="@email.uscc.net">US Cellular</option>
    <option value="@vtext.com">Verizon</option>
  </select>
  <br/>
  <input type="submit" value="Save Changes" />
</form>
EOT;

FORM SNIPPET 表格片段

 label { width: 5em; display: inline-block; text-align: right; } .footnote { margin: .5em 0 .5em 5em; } input[type="submit"] { margin: 1em 6em; } h4 { margin-bottom: 0; } 
 <p>Your seats are booked.</p> <form action="update.php" method="post"> <label>Last:</label> <input type="text" name="lname" value="$lname" /> <br/> <label>First:</label> <input type="text" name="fname" value="$fname" /> <br/> <label>Phone:</label> <input type="tel" name="telephone" value="$telephone" /> <br/> <p class="footnote">Security ID is optional</p> <label>Security ID:</label> <input type="text" name="id" value="$id" /> <br/> <input type="hidden" name="phone" value="$telephone" /> <br/> <div class="footnote"> If you want to keep your booking secure then enter a security id. <br>If blank, no security ID will be necessary to retrieve your seats in the future. <br/>This can be any number (eg PIN) word, or any characters. <br/>Maximum 16 characters.</p> <p>Do NOT use an existing high security password (eg your banking password)</p> <h4>If you would like your seats sent to you via text,<br/>Select your mobile carrier<br/>This will also verify you entered the correct phone number</h4> </div> <label>Mobile Carrier</label> <select> <option value="">No Text / Land Line</option> <option value="@message.alltel.com">Alltel</option> <option value="@paging.acswireless.com">Ameritech</option> <option value="@mmode.com">ATT Wireless</option> <option value="@bellsouth.cl">Bellsouth</option> <option value="@myboostmobile.com">Boost</option> <option value="@mobile.celloneusa.com">CellularOne</option> <option value="@mobile.mycingular.com">Cingular</option> <option value="@sms.edgewireless.com">Edge Wireless</option> <option value="@mymetropcs.com">Metro PCS</option> <option value="@messaging.nextel.com">Nextel</option> <option value="@mobile.celloneusa.com">O2</option> <option value="@mobile.celloneusa.com">Orange</option> <option value="@qwestmp.com">Qwest</option> <option value="@pcs.rogers.com">Rogers Wireless</option> <option value="@messaging.sprintpcs.com">Sprint PCS</option> <option value="@teleflip.com">Teleflip</option> </optgroup> <option value="@msg.telus.com">Telus Mobility</option> <option value="@email.uscc.net">US Cellular</option> <option value="@vtext.com">Verizon</option> </select> <br/> <input type="submit" value="Save Changes" /> </form> 

You placed apostrophes around $customerid (and other non-string values) when it is actually an integer inside your database. $customerid (和其他非字符串值)实际上是数据库中的整数时,请将撇号放在$customerid周围。 Delete the apostropes (') around all values that are meant to be integers in your database (I have a feeling that is the case for many of your variables). 删除数据库中所有应为整数的值的撇号(')(我感觉您的许多变量都是这种情况)。 Also please organize your code because it was extremely difficult to look at it without crying :) 另外,请组织您的代码,因为不哭就很难看:)

<?php

    $customerid=mysql_real_escape_string($_POST['customerid']);
    $screeningid=$_POST['screeningid'];
    $seatid=mysql_real_escape_string($_POST['seatid']);
    $priceid=mysql_real_escape_string($_POST['priceid']);
    $status=mysql_real_escape_string($_POST['status']);


    $query = "INSERT INTO `booking`(bookingid, customerid, 
    screeningid, seatid, priceid, bookingdate, status) 
    VALUES (NULL, ". $customerid.", ". $screeningid.", ".     
    $seatid.", ".$priceid.", 'DateTime()', '".$status."')";

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

    // if successfully insert data into database, displays message "Successful". 
    if($result)
    {
        echo "<p>success</p><br>"; 
    } 
    else 
    { 
         echo mysql_error();
    }

?> 

Also note, DateTime() is a php function, not an SQL command. 另请注意, DateTime()是php函数,而不是SQL命令。 I left it in the previous code but be aware that you should fix that error. 我将其保留在先前的代码中,但请注意,您应该修复该错误。

Let me know if this worked for you. 让我知道这是否对您有用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM