如果已存在,如何验证代码

时间:2014-03-09 15:46:56

标签: php mysqli

大家好,我是网络开发的新手 我有个问题 如果已存在echo错误,我想验证位置代码 怎么做?

这是我的代码.. 这是正确的吗?..

 $lcode = $_POST['locationcode'];
 $ladd  = $_POST['locationadd'];

 $chqry = $mysqli->query("SELECT locationcode FROM table_station");

 if($chqry == $lcode){

  echo "error";
 }

 else{

 $stationadd =  $mysqli->prepare("INSERT INTO table_station
                          (locationcode,locationaddress)
                  VALUES 
                  (?,?)");

 $stationadd->bind_param('is',$lcode,$ladd);
 $stationadd->execute();
 $stationadd->close();
 $mysqli->close();

echo "<script>alert('station information added');</script>";
}

感谢提前......

1 个答案:

答案 0 :(得分:1)

如果您正在试图检查该值是否存在,您可以尝试;

$lcode = $_POST['locationcode'];
$chqry = $mysqli->prepare("SELECT * FROM table_station WHERE lcode=?");
    $chqry->bind_param("s", $lcode); // you can change the 's' according to type of your data
    $chqry->execute();
    $chqry->store_result(); 
    $row_chqry = $chqry->num_rows;

if($row_chqry  > 0 ) {

$chqry->close(); 
$mysqli->close();
//your error
}

这样您就可以获得包含已发布值的行数。如果行数大于0,您可以回显一些错误..