更新php& mysql:undefined index

时间:2018-06-09 01:11:08

标签: php html mysql database

我现在正在预订系统,更新数据有问题。我像这样制作了表格 updateMy_ReservationView.php this 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'

我应该修改什么?

2 个答案:

答案 0 :(得分:0)

使用 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 字段

答案 1 :(得分:0)

将car_type的名称更改为car_type_id。错误是由于您发送 car_type 并访问updateMy_ReservationDo.php中的 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>
相关问题