【问题标题】:PHP: A HTML hidden input value generates an error upon querying mysqlPHP:一个 HTML 隐藏的输入值在查询 mysql 时会产生一个错误
【发布时间】:2021-03-05 05:24:27
【问题描述】:

我正在开发一个允许用户编辑汽车信息的网页。在主线中,有一个编辑按钮(输入 - 输入带有隐藏键值的文本),它将用户带到这个“编辑汽车信息”页面。最初,一旦第一次打开页面,这个隐藏值用于查询数据库、检索原始信息并将它们设置为字段的占位符。用户可以在输入字段中写入信息,然后按“提交编辑”按钮,然后更新数据库表中的行。但是,我收到一个错误,即隐藏值的名称未定义。我不明白当它对选择查询工作得很好时,它是如何为更新查询未定义的。任何人都可以对此有所了解吗?我该怎么办?这是错误的图片:

这是大陆代码:(这里设置了隐藏值)

<?php
$mysqli= new mysqli("localhost", "root","","Car_registration"); 
if(empty($_SESSION)) // if the session not yet started
session_start();
if(isset($_SESSION['username'])) { // if user already logged in
header("location: mainlanding_user.php"); //send to homepage
exit;
}
?> 

<!DOCTYPE html>

<html>

<head>
  <title> Car Registration: User's Mainlanding </title>
  <link href="css/style3.css" rel="stylesheet">
</head>

<body>

<header> 

<h1>Account Information</h1>
<img id="img1" src= "image/car.jpg" alt ="car image">

</header> 

<nav id='nav'>
<form action="logout.php">
<input type="submit" value="  Logout " id="button">
</form>
</nav>


<h2>Profile </h2>

<div class='container1'>

<?php 

$username="root";
$password="";
$database="Car_registration";

$mysqli= new mysqli("localhost",$username,$password,$database); 


$query= "select * from driver where username='".$_SESSION['logged_username']."'";
$result = $mysqli->query($query);


while( $row = $result->fetch_assoc() ){

echo "<div id='container'>" ;
echo "<dl> <dt>First Name</dt> <dd>".$row['Fname'];
echo "</dd> <br> <dt>Last name</dt><dd>".$row['Lname'];
echo "</dd> <br> <dt>License Number</dt><dd>".$row['license_no'];
echo "</dd> <br> <dt>Age</dt><dd>".$row['Age'];
echo "</dd> <br> <dt>Birthday</dt><dd>".$row['bdate'];
echo "</dd> <br> <dt>City</dt><dd>".$row['City'];
echo "</dd></dl>";
echo "</div>";

$license_no = $row['license_no']; //used for finding cars

}

?>


<div class="align-me">
  <div class="form-wrapper" action="search_plate_no.php">
    <form class="center">
      <input class="input-fields" name="search" type="text" placeholder="Search a plate number">
      <input class="input-fields submit" name="find" type="submit" value="Search">
    </form>
  </div>
</div>






<h3> Registered Cars </h3>

<div class='container2'>

<?php 

$username="root";
$password="";
$database="Car_registration";

$mysqli= new mysqli("localhost",$username,$password,$database); 


$query= "select * from cars where license_no='".$license_no."'";
$result = $mysqli->query($query);


echo "<table border=1>
<tr>
<th>Plate No.</th>
<th>License No.</th>
<th>Car Type</th>
<th>Fines</th>
<th>City</th>
<th>Edit</th>
<th>Delete</th>
</tr>";


while ($temp = $result->fetch_assoc()){
?> 

<tr>
    <td><?php echo $temp['Plate_no']; ?></td>
    <td><?php echo $temp['license_no']; ?></td>
    <td><?php echo $temp['Car_type']; ?></td>   
    <td><?php echo $temp['Fines']; ?></td>
    <td><?php echo $temp['city']; ?></td>         
   <td>
   <form action = "edit_car.php" method="post">
      <input type="hidden" name="id" value="<?php echo $temp['Plate_no']; ?>">
      <input type="submit" name="edit" value="Edit">
   </form>
</td>

<td>
   <form action = "delete_car.php" method="post">
      <input type="hidden" name="id" value="<?php echo $temp['Plate_no']; ?>">
      <input type="submit" name="delete" value="Delete">
   </form>
</td>
    
  </tr> 
<?php
}
?>
</table>

</div>

 
<form action="register_car.php">
<input type="submit" value="  Register Car " id="button2">
</form>

<footer> 
<h4> All rights belong to Car Registration Inc. </h4>
<img id="img3" src= "image/license.png" alt ="license plates image">

</footer>


</body>

</html>

编辑汽车页面:(此处产生错误)

 <!DOCTYPE html>
<html>

<head>
  <title> Edit Car Information Page </title>
  <link href="css/style2.css" rel="stylesheet">
   
</head>

<body>

<div class="container">

<header> 
<h1>Edit Car Information </h1>
<img id="img1" src= "image/register.png" alt ="Registration image">
</header>  

<?php 

$username="root";
$password="";
$database="Car_registration";

$mysqli= new mysqli("localhost",$username,$password,$database); 

$plate_no= $_POST["id"]; //This line causes an error 
$_SESSION['plateNo'] = $plate_no; 

$query= "select * from cars where Plate_no='".$plate_no."'";
$result = $mysqli->query($query);  

while( $row = $result->fetch_assoc()){

$plate_no = $row['Plate_no']; 
$car_type = $row['Car_type']; 
}

?>

<main>
<h2> You can only edit the following information: </h2>

<form action="" method="post">

<label for="car_type_input">Car Type:</label>
<input type="text" placeholder="<?php echo $car_type?>" id="car_type_input" name="car_type_input"><br><br>


<div class="vertical-center">
<input type="submit" value="  Submit Edit "  name="button1" id="button1">
</div>

</form>

<?php 

$username="root";
$password="";
$database="Car_registration";

$mysqli= new mysqli("localhost",$username,$password,$database); 

if( isset($_POST['button1']) ){  //If user changed field, take value. If not, keep old value. 

if( !empty($_POST['car_type_input']) ){ //If there is user input 
$car_type_2 = $_POST['car_type_input']; 
$query= "update cars set Car_type='".$car_type_2."' WHERE Plate_no='".$_SESSION['plateNo']."'";
}


if ($mysqli->query($query))
echo "Fields updated successfuly!";
else 
echo "Update Fields Failed!";

}

?>


</main>
<footer> 
<h3> All rights belong to Car Registration Inc. </h3>
<img id="img3" src= "image/license.png" alt ="license plates image">

</footer>

</div>
</body>

</html>

【问题讨论】:

  • 不要发文字图片
  • $plate_no= $_POST["id"]; //This line causes an error 是什么错误?
  • 错误在我链接的图片中。它是第 27 行的“未定义索引:id”

标签: php html mysql html-input


【解决方案1】:
Use $plate_no= $_POST['id']; instead of $plate_no= $_POST["id"];

【讨论】:

    【解决方案2】:

    为什么要关闭while循环??

    while ($temp = $result->fetch_assoc()){
    ?> 
    

    这里也是

    <?php
    }
    

    试试这个:

    print"<h3> Registered Cars </h3>
    
    <div class='container2'>";
    
    
    $username="root";
    $password="";
    $database="Car_registration";
    
    $mysqli= new mysqli("localhost",$username,$password,$database); 
    
    
    $query= "select * from cars where license_no='".$license_no."'";
    $result = $mysqli->query($query);
    
    
    echo "<table border=1>
    <tr>
    <th>Plate No.</th>
    <th>License No.</th>
    <th>Car Type</th>
    <th>Fines</th>
    <th>City</th>
    <th>Edit</th>
    <th>Delete</th>
    </tr>";
    
    
    while ($temp = $result->fetch_assoc())
    {
    
    print"
    <tr>
        <td><?php echo $temp['Plate_no']; ?></td>
        <td><?php echo $temp['license_no']; ?></td>
        <td><?php echo $temp['Car_type']; ?></td>   
        <td><?php echo $temp['Fines']; ?></td>
        <td><?php echo $temp['city']; ?></td>         
       <td>
       <form action = "edit_car.php" method="post">
          <input type="hidden" name="id" value="<?php echo $temp['Plate_no']; ?>">
          <input type="submit" name="edit" value="Edit">
       </form>
    </td>
    
    <td>
       <form action = "delete_car.php" method="post">
          <input type="hidden" name="id" value="<?php echo $temp['Plate_no']; ?>">
          <input type="submit" name="delete" value="Delete">
       </form>
    </td>
        
      </tr> ";
    
    }
    print"</table>
    
    </div>";
    
    

    【讨论】:

    • 这样我就可以在 html 中打印表格,而不是在 php 代码中回显 html。我这样做的方式很好,但你的建议!
    【解决方案3】:

    您没有发送id 那是因为出现错误使用此代码首先检查id 是否存在:

    $plate_no='';
    $car_type = '';
    if(isset($_POST["id"])){
        $plate_no= $_POST["id"]; //This line causes an error 
        $_SESSION['plateNo'] = $plate_no; 
    
        $query= "select * from cars where Plate_no='".$plate_no."'";
        $result = $mysqli->query($query);  
    
        while( $row = $result->fetch_assoc()){
            $plate_no = $row['Plate_no']; 
            $car_type = $row['Car_type']; 
        }
    }
    

    【讨论】:

    • 我会试试这个,让你知道它是怎么回事。如果 id 未设置,我应该采取什么措施?我试图将 plate_no 作为会话变量发送
    • 这是您的决定,您可以验证它并使其成为必需或做其他事情
    猜你喜欢
    • 1970-01-01
    • 2013-08-18
    • 2019-11-18
    • 2018-07-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多