Php 即使数据是错误的,SQL仍然会将数据插入数据库

Php 即使数据是错误的,SQL仍然会将数据插入数据库,php,android,mysql,Php,Android,Mysql,我想做的是:我正在使用Android将数据插入数据库。但它必须在第一个表中检索/搜索此车牌号,但在将数据插入另一个表之前,它应首先检查此表中是否有此车牌号,如果为真,则将数据插入表中,如果为错,则不将数据插入表中 我遇到的问题是:我在EditText中键入的数据仍然会插入到数据库/表中,即使我键入的车牌号是错误的 我认为我的SQL语句有问题吗 我的PHP代码: <?php $host='localhost'; $user='root'; $password=''; $db='employe

我想做的是:我正在使用Android将数据插入数据库。但它必须在第一个表中检索/搜索此车牌号,但在将数据插入另一个表之前,它应首先检查此表中是否有此车牌号,如果为真,则将数据插入表中,如果为错,则不将数据插入表中

我遇到的问题是:我在EditText中键入的数据仍然会插入到数据库/表中,即使我键入的车牌号是错误的

我认为我的SQL语句有问题吗

我的PHP代码:

<?php
$host='localhost';
$user='root';
$password='';
$db='employee101';

$PLATE_NUM = $_POST["PLATE_NUM"];
$PUV_TYPE = $_POST["PUV_TYPE"];
$content = $_POST["content"];

$sql = "select * from employee_data where PLATE_NUM like '$PLATE_NUM';";//first table where you retrieve the plate number first
$sql_insert = "insert into employee_content (PLATE_NUM, PUV_TYPE, content) values('$PLATE_NUM', '$PUV_TYPE', '$content')";//insert the table

$con = mysqli_connect($host,$user,$password,$db);

$result = mysqli_query($con, $sql);
$result2 = mysqli_query($con, $sql_insert);
$response = array();

if (mysqli_num_rows($result)> 0 && ($result2)=== TRUE ){
        echo"Log-In Success!!!!!!!!!!!!!!!!!!!!!!!!!!!!!";
}else{
    echo "Log-In not success";
}

mysqli_close($con);

?>
背景工人阶级:

if(type.equals("report")){
                try {
                    String id_ret = params[1];
                    String puv_type = params[2];
                    String report = params[3];

                    URL url = new URL(retrieve_id);
                    HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
                    httpURLConnection.setRequestMethod("POST");
                    httpURLConnection.setDoOutput(true);
                    httpURLConnection.setDoInput(true);
                    OutputStream outputStream = httpURLConnection.getOutputStream();
                    BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
                    String post_data = URLEncoder.encode("PLATE_NUM","UTF-8")+"="+URLEncoder.encode(id_ret,"UTF-8") + "&"
                            + URLEncoder.encode("PUV_TYPE", "UTF-8") + "=" + URLEncoder.encode(puv_type, "UTF-8") + "&"
                            + URLEncoder.encode("content", "UTF-8") + "=" + URLEncoder.encode(report, "UTF-8");;
                    bufferedWriter.write(post_data);
                    bufferedWriter.flush();
                    bufferedWriter.close();
                    outputStream.close();
                    InputStream inputStream = httpURLConnection.getInputStream();
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream, "iso-8859-1"));
                    String result = "";
                    String line = "";
                    while((line = bufferedReader.readLine())!= null){
                        result += line;
                    }
                    bufferedReader.close();
                    inputStream.close();
                    httpURLConnection.disconnect();
                    return result;
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                } catch (IOException e) {
                    e.printStackTrace();
                }

你可以用两种方法

1使用一些php脚本手动检查数据库。然后您可以插入数据。 其逻辑是

编写一个SQL查询以检查板的计数\u NUM='value'

然后从结果中获取count值。如果该值大于0。表示该值已存在于数据库中

if($count>0){
    echo "PLATE_NUM already exist";
    // if you want to update. Here you can update.
}else{
    // perform your actions like insert.
}
2将PLATE_NUM字段作为数据库中的唯一键。。这将自动检查是否存在重复

使用mysqli_bind参数概念获取计数

$stmt = $mysqli->prepare($query) or trigger_error($mysqli->error."[$sql]");
$stmt->bind_param('d', $plate_no_value);// here d for number and s for string 
$stmt->bind_result($count);
$stmt->execute();

php中的这条语句看起来很粗略:

$sql=从员工_数据中选择*,其中PLATE_NUM类似于“$PLATE_NUM”

我认为like关键字可以找到一种模式[1]。你为什么不去那里?因为只有当两个条件都为真时,if语句才为真,并且如果在PLATE_NUM列中找到一个模式,它将始终为真。您希望特定的车牌号作为条件检查

$sql=从员工_数据中选择*,其中PLATE_NUM='$PLATE_NUM'

[1]

编辑,问题是在这里,您总是在if语句检查之前运行result2查询。您只需要在result1返回成功时运行result2

换成

$result = mysqli_query($con, $sql);
//注释掉$result2=mysqli\u查询$con,$sql\u插入; $response=数组

if (mysqli_num_rows($result)> 0 ){
        $result2 = mysqli_query($con, $sql_insert);

实际上,您编写了查询,但是在未检查插入数据的情况下,您没有检查第一个查询是否可用

相反,您只需使用一个更新查询即可实现这一点

$sql_insert = "UPDATE employee_content SET PLATE_NUM = '$PLATE_NUM', PUV_TYPE = '$PUV_TYPE', content = '$content' WHERE PLATE_NUM='$PLATE_NUM' )";

我将其更改为“=”,但它仍然插入到数据库中。啊,我现在看到了您的问题,您将始终运行result2。我正在更新我的答案,现在更新了检查我的更新答案,非常确定这是真正的问题我做得对吗$count=mysqli_query$con$query;?是的,为了更好地练习,我用MySQLi_绑定参数概念编辑了答案。请看一下
if (mysqli_num_rows($result)> 0 ){
        $result2 = mysqli_query($con, $sql_insert);
$sql_insert = "UPDATE employee_content SET PLATE_NUM = '$PLATE_NUM', PUV_TYPE = '$PUV_TYPE', content = '$content' WHERE PLATE_NUM='$PLATE_NUM' )";