我正在研究我的学校项目,并且现在仍然坚持这个问题,我希望有人在这里指出我正确的方向。
我正在设计一个使用网络前端和MySQL数据库的预订系统。我有几张桌子:顾客,座位,价格,预订和放映。我正在尝试使用主键从其他表中将数据插入到预订表中。但是我一直收到以下错误消息:不正确的整数值:''对于第1行的列'customerid' 我搜索过每一个地方,但似乎没有得到任何解决方案。我在下面复制了我的代码。
<?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>
答案 0 :(得分:0)
错误消息表示您获得了''
的“空字符串”(customerid
),这意味着当您的第一个代码块没有为该字段获取任何值时表格已提交。
问题在于:
echo "<option value>$customerid $fname $lname $telephone</option>";
<option>
和</option>
之间的值将显示给最终用户,但它们不会提交与您的表单,这意味着他们赢了'第一块代码可以使用。
要提交customerid,您必须将其放入value
部分:
echo "<option value=$customerid>$customerid $fname $lname $telephone</option>";
答案 1 :(得分:-1)
使用intval()
表示整数值
对字符串使用mysql_real_escape_string
,对整数使用Never
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];
CONSTRAINT ERROR表示您没有添加预订表的客户记录。或者外键错误。不需要外键。
您可以同时组合客户添加,查找和预订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;
}
&#13;
<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>
&#13;
答案 2 :(得分:-2)
当数据库中实际上是整数时,您在$customerid
(以及其他非字符串值)周围放置了撇号。删除所有在数据库中为整数的值的对象(&#39;)(我感觉很多变量就是这种情况)。另外请整理你的代码,因为在不哭的情况下查看代码非常困难:)
<?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命令。我在之前的代码中留下了它,但要注意你应该修复那个错误。
如果这对您有用,请告诉我。