【问题标题】:Incorrect integer value: '' for column 'customerid' at row 1不正确的整数值:第 1 行的列 'customerid' 的 ''
【发布时间】:2016-05-07 00:03:09
【问题描述】:

我正在为学校做项目,现在被困在这个问题上,我希望有人在这里为我指出正确的方向。

我正在设计一个使用 Web Front 和 MySQL 数据库的预订系统。我有几张桌子:顾客、座位、价格、预订和筛选。我正在尝试使用那里的主键将数据从其他表插入到预订表中。但是我不断收到以下错误消息: Incorrect integer value: '' for column 'customerid' at row 1 我到处搜索,但似乎没有得到任何解决方案。我在下面复制了我的代码。

<?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();
}
?> 

这是我的表格:

<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>

【问题讨论】:

  • 那么customerid是什么数据类型? $_POST['customerid'] 的值是多少?
  • customerid 是整数
  • $_POST['customerid']的值的值?

标签: php mysql


【解决方案1】:

intval() 用于整数值。
对字符串使用mysql_real_escape_string,从不用于整数。
在 mySQL 查询中,整数值周围的单引号是可选的。
因为intval() 保证$customerid 是一个整数值,所以引号不是必需的并且永远不会产生错误。

我只包含了与您的客户 ID 直接相关的两行代码。这可能也适用于其他值。

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

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

变化:

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

收件人:

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

尽可能提交大于零的整数值。它们很容易验证。要取回文本:

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

更新外键约束错误

约束错误表示您没有添加的预订表的客户记录。或者外键错误。不需要外键。

您可以同时结合客户添加、查找和预订 INSERT。

而不是某种登录来创建或检索客户记录获取预订,然后在预订过程的最后一步中询问电话号码。就个人而言,我不在乎是否有人用我的电话号码查询系统并找出我的座位。有些可能。但是在预订之前要求登录是一件麻烦事,也是完成预订的障碍。

我所做的是添加安全 ID

如果他们想用密码保护自己的座位,让他们自己选择吧。

如果记录不存在,则在预订座位后询问他们的姓名。

//$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;

表单片段

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 (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>

【讨论】:

  • 似乎对它进行排序,但现在我得到:无法添加或更新子行:外键约束失败(cinema_booking.booking,CONSTRAINT booking_ibfk_1 FOREIGN KEY(customerid ) 参考customers (customerid))
  • 我从不使用外键,所以我帮不上什么忙。从表的索引中删除键或约束,或者修复查询或表结构,以便创建/更新子记录以满足约束条件。如果您不熟悉外键,请尝试导出数据库(仅限结构)并查看导出文档。
  • 我到处搜索,没有找到如何解决外键约束问题的解决方案。我还重新检查了我的桌子,一切似乎都很好。真的不知道我错过了
  • 为什么会有外键?
  • 如果没有外键,我将无法插入或更新预订。当我删除外键时,我在预订表中看到的都是零。
【解决方案2】:

错误消息意味着您得到了customerid 的“空字符串”(''),这仅仅意味着在提交表单时,您的第一段代码根本没有为该字段获取任何值。

问题来了:

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

&lt;option&gt;&lt;/option&gt; 之间的值将显示给最终用户,但它们不会提交您的表单,这意味着它们将不可用于第一段代码。

要提交 customerid,您必须将其放入 value 部分:

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

【讨论】:

  • 这是正确的,但它没有解决当客户表对于成功的企业来说往往变得很大时进行客户 ID 查找的方法是多么可怕。 .不仅如此,有多少客户希望在售票网站上看到他们的名字和电话号码?我们不会故意做那些只对不成功的企业有用的事情,让它们不成功。我建议他用要求电话号码进行查找来代替它。但你可能在你走到那一步之前就对我投了反对票,谢谢。强调 intval 的原因是因为先前的答案给出了糟糕的建议。
  • 嗯,这是给学校的。我觉得进入所有这些可能超出了课程的范围,并且绝对超出了问题的范围,问题很简单,“为什么我会收到这个错误消息?”
【解决方案3】:

$customerid(和其他非字符串值)实际上是数据库中的整数时,您在其周围放置了撇号。删除数据库中所有作为整数的值周围的撇号 (')(我感觉您的许多变量都是这种情况)。也请整理好你的代码,因为看着它不哭是非常困难的:)

<?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();
    }

?> 

另外请注意,DateTime() 是一个 php 函数,而不是 SQL 命令。我把它留在了前面的代码中,但请注意您应该修复该错误。

如果这对你有用,请告诉我。

【讨论】:

  • 如果在没有验证值是整数的情况下删除单引号(在这种情况下),它可能会在查询失败而不是引号仅显示警告和其他字段数据时产生错误内容不会丢失。 mysql_real_escape_string 永远不应该用于整数值。
猜你喜欢
  • 2014-04-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-12-11
  • 2017-05-24
相关资源
最近更新 更多