如何将输入类型与数据库中的特定列进行比较

时间:2016-12-07 01:55:12

标签: php

我有一个输入类型名称=“名称”,当我提交它时,我想检查它是否匹配数据库中名为“name”的特定列中的任何数据(如图所示),如果匹配,我将执行某个命令。我的问题是,如果我的输入与该特定列中的任何数据匹配,我不知道正确的查询或逻辑。我试过这段代码,但它没有给我正确的输出。提前致谢!对我的项目有很大的帮助!

$name=$_GET['name']; //input type

$result = mysql_query("SELECT name FROM map");
while ($search = mysql_fetch_assoc($result)) {
$storeArray =  $search['name'];  

if($name!=$storeArray){
echo "<script>alert('Nothing match your voice search! ')</script>";
echo "<script>window.location.href=\"map_index.php?  name='$storeArray'\"   </script>";


}else{


$find=mysql_query("SELECT * FROM map WHERE name='$name'");
while($found=mysql_fetch_assoc($find)){

Click this for the image sample

1 个答案:

答案 0 :(得分:1)

首先在最新版本的PHP中删除了mysql,所以改为使用mysqli或PDO:http://php.net/manual/en/intro.mysql.php

<?php
$name = $_GET['name']; //input type

//Create Prepared Statement

if ($stmt = $mysqli->prepare("SELECT name FROM map WHERE name=?")) {
    // bind parameters for markers the first parameter is the type, in this case it is a string, which is why we use 's'. If the parameter was an integer you would use 'i'.
    //if this passes then the name that you are searching the database for exists
    if ($stmt->bind_param("s", $name)) {
      //success
    }
    else { ?>
     <script>
         alert('Nothing match your voice search!');
         window.location.href="map_index.php?name=<?= $storeArray; ?>";
     </script>
   <?php }
} ?>

还要注意我是如何从php中分离脚本标记和javascript的。这将完全按照您的方式工作,但它更清洁,被认为是最佳实践。