2016-12-07 30 views
0

我有一个输入类型name =“name”,当我提交它时,我想检查它是否与数据库中名为“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

所有MySQL的第一已经在最近版本的PHP被删除,所以改用库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 } 
} ?> 

另请注意我是如何将脚本标记和javascript从php中分离出来的。这将会如何工作,但它更清洁并被认为是最佳实践。

+0

先生我不能使用mysqli做到这一点,因为我的PHP没有更新,是否有任何替代的代码在MySQL?这将是一个很大的帮助 – Janaaan

相关问题