更新php&mysql:未定义索引

aiqt4smr  于 2021-06-21  发布在  Mysql
关注(0)|答案(2)|浏览(293)

我正在预订系统,更新数据有点问题。我做了一张table updateMy_ReservationView.php 这样地。这是selectmy\u 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\u 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'

我应该修改什么?

2nc8po8w

2nc8po8w1#

将car\u type的名称更改为car\u type\u id。错误是因为您正在updatemy\u reservationdo.php中发送car\u type并访问car\u type\u 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>
6jygbczu

6jygbczu2#

使用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\u type,但您在sql查询中使用了$car\u type\u id use:

$car_type

并确保表中存在car\u type字段

相关问题