如何从数组值搜索到mysql

变量包含来自搜索框的 (14409,14408,14405)。如果找到其中一个获取数据,我想在 mysql 中分别搜索它们中的每一个,如果没有忽略它并显示不存在的按摩


<form method="POST" class="form" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']); ?>">

    <input type="text" name="vehiclenum" placeholder="Search Multiple Separate by a Comma (,)">

    <input type="submit" name="submit" Value="Search">

</form>

<?php

$vehiclenumerr = array();

if ($_SERVER['REQUEST_METHOD']=="POST"){

  $vehiclenum = $_POST["vehiclenum"];


$exploded=explode(",",$vehiclenum);

$explodedd=implode("','",$exploded) ;

$sql = "SELECT * FROM vehicles WHERE num_vehicles IN('$explodedd')";

$result = mysqli_query($conn, $sql);

$num_query = mysqli_num_rows($result);

if ($num_query > 0) { ?>

<table> 

<tr>

<th>Vehicle num</th>

<th>Description</th>

</tr>


<?php while($row = mysqli_fetch_assoc($result)) { ?>

<tr>

<td><?php echo $row['num_vehicles']; ?></td>

<td><?php echo $row['desc_vehicles']; ?></td>

</tr>

<?php   }   ?>

</table>


<?php 

} else { $vehiclenumerr[] ="Vehicle Number: ".$vehiclenum. " are NOT Exist";}


}


if(!empty($vehiclenumerr)){

foreach ($vehiclenumerr as $error) {

            echo $error; 

            echo "<br>";

        }

}


?>

我希望发现表格有 2 个结果(14409 和 14405)以及该注释的下方(车辆编号:14408 不存在)


我得到了表成功,但没有不存在的注释。


绝地无双
浏览 166回答 2
2回答

慕神8447489

希望它会有所帮助:&nbsp; <?php while($row = mysqli_fetch_assoc($result)) { ?>&nbsp; &nbsp; <?php if($row['num_vehicles'] && $row['desc_vehicles']): ?>&nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; <td><?php echo $row['num_vehicles']; ?></td>&nbsp; &nbsp; &nbsp; <td><?php echo $row['desc_vehicles']; ?></td>&nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; <?php else: ?>&nbsp; &nbsp; &nbsp; <tr>&nbsp; &nbsp; &nbsp; <td><?php echo "Not exist" ?></td>&nbsp; &nbsp; &nbsp; <td><?php echo "Not exist" ?></td>&nbsp; &nbsp; &nbsp; </tr>&nbsp; &nbsp; <php endif; ?>&nbsp; <?php&nbsp; &nbsp;}&nbsp; &nbsp;?>或者您可以删除“不存在”字段。

浮云间

感谢您的帮助,我找到了解决方案,所以我与您分享。$vehiclenum = check_input($_POST["vehiclenum"]);$exploded = explode(",",$vehiclenum);&nbsp;$countexploded = count($exploded);for ($i = 0; $i < $countexploded; $i++) {&nbsp; if (!empty($exploded[$i])) {&nbsp; &nbsp; $sql = "SELECT * FROM vehicles WHERE num_vehicles='$exploded[$i]'";&nbsp; &nbsp; $result = mysqli_query($connect_db, $sql);&nbsp; &nbsp; $num_query = mysqli_num_rows($result);&nbsp; &nbsp; if ($num_query > 0) {&nbsp; &nbsp; &nbsp; &nbsp;while($row = mysqli_fetch_assoc($result)) {&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // sort results here&nbsp; &nbsp; &nbsp; &nbsp;}&nbsp;&nbsp; &nbsp; } else { $vehiclenumerr[] ="Vehicle Number: ".$exploded[$i]. " are NOT Exist";}&nbsp; }}编辑:我还添加了防止 sql 注入的功能::function check_input($data){&nbsp; &nbsp; $data=trim($data);&nbsp; &nbsp; $data=stripcslashes($data);&nbsp; &nbsp; $data=htmlspecialchars($data);&nbsp; &nbsp; return $data;}
打开App,查看更多内容
随时随地看视频慕课网APP