【问题标题】:update php&mysql : undefined index更新 php&mysql : 未定义的索引
【发布时间】:2018-06-09 01:11:07
【问题描述】:

我正在制作预订系统,更新数据时出现问题。我做了这样的表updateMy_ReservationView.phpthis is an image of SelectMy_ReservationView.php 由于此编辑器的错误消息,我插入更多代码时出错。

<?php
include "connection.php";
$id=$_GET['reservation_id'];
$sql = "select reservation.*, customer.*, car_type.*, datediff(return_time, 
rent) as total_day,  (datediff(return_time, rent) * price ) AS total_price 
FROM 
reservation, customer, car_type
WHERE reservation.car_type_id=car_type.car_type_id AND 
reservation.customer_id=customer.customer_id and reservation_id='$id' order 
by reservation_id ";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);

$sql_car = "SELECT car_type.* from car_type";
$result_car = mysqli_query($conn, $sql_car);
?>  
<h3><b>Update Reservation</b></h3><br>
<form method = "post" action = "?page=updateMy_ReservationDo">
              <table class="table table-striped table-sm" 
style="width:500px; height:200px;">
                 <tr>
                    <td>Customer Name</td>
                    <td>
                       <?php echo" $row[customer_name]";?>
                       <input type = "hidden" name="reservation_id" value=" 
<?php echo"$row[reservation_id]";?>">
                    </td>
                 </tr>
                  <tr>
                    <td>Old car type</td>
                    <td>
                       <?php echo" $row[car_type]";?>

                    </td>
                 </tr>
                 <tr>
                    <td>New Car Type (Price USD)</td>
                    <td>
                    <select name = "car_type">
                    <?php
                    while($row_car = mysqli_fetch_assoc($result_car)) {
                    ?>   
                       <option value="<?php echo"$row_car[car_type_id]";?>"> 
<?php echo"$row_car[car_type] ($row_car[price])";?></option>        
                    <?php
                    }
                    ?>
                    </select>
                    </td>
                 </tr>
                 <tr>
                    <td>Old Rent</td>
                    <td><?php echo "$row[rent]"; ?></td>
                 </tr>
                 <tr>
                    <td>Rent</td>
                    <td><input type="text" name="rent" id="rent" 
maxlength="25" size="25"/>
                    <img src="images_date/cal.gif" alt="" 
onclick="javascript:NewCssCal('rent','yyyyMMdd','arrow',false,'24',false)" 
style="cursor:pointer"/></td>
                 </tr>
                 <tr>
                    <td>Old Return</td>
                    <td><?php echo "$row[return_time]"; ?></td>
                 </tr>
                 <tr>
                    <td>Return</td>
                    <td><input type="text" name="return_time" 
id="return_time" maxlength="25" size="25"/>
                    <img src="images_date/cal.gif" alt="" 
onclick="javascript:NewCssCal('return_time','yyyyMMdd'
'arrow',false,'24',false)" style="cursor:pointer"/></td>
                 </tr>
                 <tr>
                    <td>Old Pickup Station</td>
                    <td><?php echo "$row[car_station]"; ?></td>
                 </tr>
                  <tr>
                    <td>Pickup Station</td>
                    <td>
                       <select name = "car_station">
                          <option value="Yeouido">Yeouido</option>
                          <option value="Shinchon">Shinchon</option>
                          <option value="Jongro">Jongro</option>
                          <option value="Seoul Station">Seoul 
Station</option>
                          <option value="Gangnam">Gangnam</option>
                          <option value="Geondae">Geondae</option>
                      </select></td>
                 </tr>
                 <tr>
                    <td> </td>
                    <td><input type="reset" value="Reset"> <input name = "add" type = "submit" value = "Update Reservation">
                    </td>
                 </tr>
              </table>

我做了更新函数文件 updateMy_ReservationDo.php 如下所示。

include "connection.php";
$reservation_id=$_POST['reservation_id'];
$car_type=$_POST['car_type_id'];
$rent=$_POST['rent'];
$return_time=$_POST['return_time'];
$car_station=$_POST['car_station'];

$sql = "update reservation set car_type='$car_type_id',rent='$rent', 
return_time='$return_time' and car_station='$car_station' where 
reservation_id=$reservation_id ";

if (mysqli_query($conn, $sql)) {
echo "Reservation is updated successfully<br>";
 echo "<p><p><a href=?page=selectMy_reservationView><button type=button>Show 
all reservation</button></a>";

} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

mysqli_close($conn);
?>

然后出现这样的错误消息:

Notice: Undefined index: car_type_id in C:\xampp\htdocs\rentcar\updateMy_ReservationDo.php on line 5

Notice: Undefined variable: car_type_id in C:\xampp\htdocs\rentcar\updateMy_ReservationDo.php on line 10
Error: update reservation set car_type='',rent='2018-05-31', return_time='2018-06-01' and car_station='Shinchon' where reservation_id=17 
Unknown column 'car_type' in 'field list'

我应该修改什么?

【问题讨论】:

  • 根据我的经验,价格会随着季节的变化而波动,所以我无法想象这样的场景可以简单地通过将天数乘以一个数字来计算。
  • 感谢您的评论。这只是简单的本科生项目,所以我们没有考虑。价格仅由汽车计算(例如,奥迪 a5、现代索纳塔等)我看到了您关于 selectMy_reservationView 的最后一条评论,但由于代码太多,我无法上传它......通过堆栈编辑器的错误消息。你有没有错误上传的想法?如果需要,我可以把它作为图片发布吗?

标签: php html mysql database


【解决方案1】:

使用 isset 如下:

$reservation_id = isset($_POST['reservation_id']) ? $_POST['reservation_id'] : '';
$car_type = isset($_POST['car_type_id']) ? $_POST['car_type_id'] : '';
$rent = isset($_POST['rent']) ? $_POST['rent'] : '';
$return_time = isset($_POST['return_time']) ? $_POST['return_time'] : '';
$car_station = isset($_POST['car_station']) ? $_POST['car_station'] : '';

您定义的变量是 $car_type 但您在 sql 查询中使用了 $car_type_id 使用:

$car_type

并确保 car_type 字段存在于您的表中

【讨论】:

  • 谢谢!我在你写的时候改变了,并使用了这个 sql:$sql = "update reservation set car_type_id=$car_type,rent='$rent', return_time='$return_time' and car_station='$car_station' where reservation_id=$reservation_id " ;和这样的错误: 错误:更新预订集 car_type_id=,rent='2018-06-05', return_time='2018-06-06' and car_station='Shinchon' where reservation_id=17 You have a error in your SQL syntax ;检查与您的 MySQL 服务器版本相对应的手册,以在第 1 行的 'rent='2018-06-05'、return_time='2018-06-06' 和 car_station='Shinchon' where res' 附近使用正确的语法
【解决方案2】:

把car_type的名字改成car_type_id。错误是因为你在updateMy_ReservationDo.php中发送car_type并访问car_type_id

<?php
include "connection.php";
$id=$_GET['reservation_id'];
$sql = "select reservation.*, customer.*, car_type.*, datediff(return_time, 
rent) as total_day,  (datediff(return_time, rent) * price ) AS total_price 
FROM 
reservation, customer, car_type
WHERE reservation.car_type_id=car_type.car_type_id AND 
reservation.customer_id=customer.customer_id and reservation_id='$id' order 
by reservation_id ";
$result = mysqli_query($conn, $sql);
$row = mysqli_fetch_assoc($result);

$sql_car = "SELECT car_type.* from car_type";
$result_car = mysqli_query($conn, $sql_car);
?>  
<h3><b>Update Reservation</b></h3><br>
<form method = "post" action = "?page=updateMy_ReservationDo">
              <table class="table table-striped table-sm" 
style="width:500px; height:200px;">
                 <tr>
                    <td>Customer Name</td>
                    <td>
                       <?php echo" $row[customer_name]";?>
                       <input type = "hidden" name="reservation_id" value=" 
<?php echo"$row[reservation_id]";?>">
                    </td>
                 </tr>
                  <tr>
                    <td>Old car type</td>
                    <td>
                       <?php echo" $row[car_type]";?>

                    </td>
                 </tr>
                 <tr>
                    <td>New Car Type (Price USD)</td>
                    <td>
                    <select name = "car_type_id">
                    <?php
                    while($row_car = mysqli_fetch_assoc($result_car)) {
                    ?>   
                       <option value="<?php echo"$row_car[car_type_id]";?>"> 
<?php echo"$row_car[car_type] ($row_car[price])";?></option>        
                    <?php
                    }
                    ?>
                    </select>
                    </td>
                 </tr>
                 <tr>
                    <td>Old Rent</td>
                    <td><?php echo "$row[rent]"; ?></td>
                 </tr>
                 <tr>
                    <td>Rent</td>
                    <td><input type="text" name="rent" id="rent" 
maxlength="25" size="25"/>
                    <img src="images_date/cal.gif" alt="" 
onclick="javascript:NewCssCal('rent','yyyyMMdd','arrow',false,'24',false)" 
style="cursor:pointer"/></td>
                 </tr>
                 <tr>
                    <td>Old Return</td>
                    <td><?php echo "$row[return_time]"; ?></td>
                 </tr>
                 <tr>
                    <td>Return</td>
                    <td><input type="text" name="return_time" 
id="return_time" maxlength="25" size="25"/>
                    <img src="images_date/cal.gif" alt="" 
onclick="javascript:NewCssCal('return_time','yyyyMMdd'
'arrow',false,'24',false)" style="cursor:pointer"/></td>
                 </tr>
                 <tr>
                    <td>Old Pickup Station</td>
                    <td><?php echo "$row[car_station]"; ?></td>
                 </tr>
                  <tr>
                    <td>Pickup Station</td>
                    <td>
                       <select name = "car_station">
                          <option value="Yeouido">Yeouido</option>
                          <option value="Shinchon">Shinchon</option>
                          <option value="Jongro">Jongro</option>
                          <option value="Seoul Station">Seoul 
Station</option>
                          <option value="Gangnam">Gangnam</option>
                          <option value="Geondae">Geondae</option>
                      </select></td>
                 </tr>
                 <tr>
                    <td> </td>
                    <td><input type="reset" value="Reset"> <input name = "add" type = "submit" value = "Update Reservation">
                    </td>
                 </tr>
              </table>

【讨论】:

  • 感谢您的评论。错误:更新预订集 car_type=1,rent='2018-06-05', return_time='2018-06-13' and car_station='Geondae' where reservation_id=17 Unknown column 'car_type' in 'field list' -- > 当我按照你所说的修改它们时会发生此错误。
  • 因此未定义索引错误被删除。现在查看 sql 表,您为 car_type 或 car_type_id 分配了什么名称。从 sql 分配和获取数据时要小心。希望这能解决问题。
  • 谢谢,还有一个问题.. table(column, column) : 有 car_type(car_type_id, car_type , ...) 和 reservation(....car_type_id, ...).. .. car_type_id 是预订的 fk、int 和 car_type 的 pk(描述列出的汽车的序列号)。 car_type 是 varchar(描述列出的汽车的名称)..
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-01-26
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-19
相关资源
最近更新 更多