简体   繁体   中英

Data verification php/mysql/jquery

I am trying to retrieve data from excel file and sending it to a php file to check if it exists in the database otherwise notify the user that the data is wrong.

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;
}

It looks like the return value is wrong or something,the textbox always goes red as if data entered is wrong even if it's not.

you are mixing mysqli_* and mysql_* commands.

quote from the documentation of mysql_real_escape_string() :

Returns the escaped string, or FALSE on error.

Since you don't have a mysql_connect() call before it will most probably return false. You can check that with var_dump($my_data); .

You can use mysqli_real_escape_string() instead, alternatively you should look at prepared statements. Note: mysqli_real_escape_string() must also be called AFTER the mysqli_connect() call.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM