簡體   English   中英

數據驗證php / mysql / jquery

[英]Data verification php/mysql/jquery

我正在嘗試從excel文件中檢索數據並將其發送到php文件,以檢查它是否存在於數據庫中,否則通知用戶該數據是錯誤的。

HTML

<form name="trialInsert" id="trialInsert" action="trial_insert_excel.php"  method="post">
<?php foreach( $data as $row ) { ?>
<tr class="item-row">
<td><input type="text"  name="acc_code[]" id="acc_code[]" class="code" onchange="validate();" value="<?php echo ($row['acc_code']); ?>"/></td>
<td><input type="text" readonly name="int_code[]" id="int_code[]" value="<?php echo ( $row['int_code']); ?>"/></td>
<td><input type="text" readonly name="debit[]" id="debit[]" value="<?php echo ( $row['debit'] ); ?>"/></td>
<td><input type="text" readonly name="credit[]" id="credit[]" value="<?php echo( $row['credit'] ); ?>"/></td>
<td><input type="text" readonly name="debitOld[]" id="debitOld[]" value="<?php echo( $row['debitOld'] ); ?>"/></td>
<td><input type="text" readonly name="creditOld[]" id="creditOld[]" value="<?php echo($row['creditOld'] ); ?>"/></td>
</tr>
<?php } ?>

jQuery的

$(document).ready(function(){
    var code = $('.code').val()
    jQuery.ajax({
        type: 'POST',
        source:'autocomplete.php',
        data: 'acc_code='+ code,
        cache: false,
        success: function(response){
            if(response > 0){
                alert(code);
            } else {
                $('.code').addClass('errorClass');
            }
        }
    });
});

function validate(){
    var code = $('.code').val()
    jQuery.ajax({
        type: 'POST',
        url: 'autocomplete.php',
        data: 'acc_code='+ code,
        cache: false,
        success: function(response){
            if(response > 0){
                alert(code);
            } else {
                $('.code').addClass('errorClass');
            }
        }
    });
}

PHP

$q=$_GET['q'];
$my_data=mysql_real_escape_string($q);
$mysqli=mysqli_connect('localhost','root','03300130','mydb') or die("Database Error");
$sql="SELECT acc_code,acc_desc,acc_type FROM charts WHERE acc_code LIKE '%$my_data%' ORDER BY acc_code";
$result = mysqli_query($mysqli,$sql) or die(mysqli_error());
if($result) {
    echo 1;
} else {
    echo 0;
}

看起來返回值是錯誤的或類似的東西,文本框總是變成紅色,就好像輸入的數據是錯誤的,即使不是。

您正在混合mysqli_*mysql_*命令。

引用mysql_real_escape_string()的文檔:

Returns the escaped string, or FALSE on error.

由於您之前沒有mysql_connect()調用,因此很可能會返回false。 您可以使用var_dump($my_data);進行檢查var_dump($my_data);

您可以改用mysqli_real_escape_string() ,也可以查看准備好的語句。 注: mysqli_real_escape_string()必須也被稱為后mysqli_connect()調用。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM