使用jQuery mysql_affected_rows()正确地响应消息

Using mysql_affected_rows() with jQuery dosen't response message corectly

本文关键字:正确地 响应 消息 rows jQuery mysql affected 使用      更新时间:2023-09-26

我正在使用jQuery将行插入数据库。所有的数据被插入,最后我使用mysql_affected_rows()像这样:

if(mysql_affected_rows()>0){  
    echo "1";  
}else{  
    echo "2";  
}  

对于javascript代码使用:

function(data, textStatus) {  
    $('#response').html(textStatus);  
    if(data == 1){  
        $('#response').html("Registration successfull!!");  
        $('#response').css('color','green');  
    }else{  
        $('#response').html("Some Error Occurred");  
        $('#response').css('color','red');  
    }  

将行插入到数据库中,但显示:

Some Error Occurred

更新:

必须有2个文件:article_add.php和article_add_process.php

article_add.php

<script type="text/javascript">
$('document').ready(function(){
$('#button').click(function(){
if($('#article').val()==""){ alert("Веведете име"); return false; } else { var article = $('#article').val(); } 
if($('#category').val()==""){ alert("Изберете категория"); return false; } else { var category = $('#category').val(); } 
if($('#qty_unit').val()==""){ alert("Изберете мерна единица"); return false; } else { var qty_unit = $('#qty_unit').val(); } 
if($('#code').val()==""){ alert("Въведете уникален код"); return false; } else { var code = $('#code').val(); } 
if($('#barcode').val()==""){ alert("Въведете Баркод"); return false; } else { var barcode = $('#barcode').val(); } 
if($('#textarea').val()==""){ alert("Въведете информация"); return false; } else { var textarea = $('#textarea').val(); } 
jQuery.post("articles_add_process.php", { 
article: article, 
category: category, 
qty_unit: qty_unit, 
code: code, 
barcode: barcode, 
textarea: textarea 
}, 
function(data, textStatus) {
$('#response').html(data);
if(data == 1){
$('#response').html("Registration successfull!!");
$('#response').css('color','green');
}else{
$('#response').html("Some Error Occurred");
$('#response').css('color','red');
}
});
});
});
</script>
<div id="form">
<form>
<div id="imagePreview">
<input id="uploadFile" type="file" name="image" class="img" /><br />
</div>
<br />
<input id="article" type="text" placeholder="Име" /><br />
<select id="category" name="category">
<option value="*">Категория</option>
<?php
$extract_category = "SELECT * FROM categories";
$select_category = mysql_query($extract_category);
while($row_query = mysql_fetch_array($select_category)) 
{        
echo "<option value='".$row_query['id']."'>".$row_query['category']."</option>"; 
}
?>    
</select><br />
<select id="qty_unit">
<option value="*">Мярка</option>
<?php
while($row_query = mysql_fetch_array($query)) 
{        
echo "<option value='".$row_query['id']."'>".$row_query['category']."</option>"; 
}
?>    
</select><br />
<input id="generate_code" type="checkbox" value="" checked="checked" />Автоматично генериране на уникален код<br />
<input id="code" type="text" id="" placeholder="Уникален код" /><br />
<input id="barcode" type="text" id="" placeholder="Баркод" /><br />
<textarea id="textarea" id="textarea" cols="45" rows="5" placeholder="Информация за артикула"></textarea><br />
<input type="button" id="button" value="Добави" /><br />
<label id="response"></label>
</form>
</div>

article_add_process.php

<?php
require_once("models/config.php");
if (!securePage($_SERVER['PHP_SELF'])){die();}
require_once("models/header.php");
require_once("config.php");
$article = $_POST['article'];
$category = $_POST['category'];
$qty_unit = $_POST['qty_unit'];
$code = $_POST['code'];
$barcode = $_POST['barcode'];
$textarea = $_POST['textarea'];
$query=mysql_query("INSERT INTO articles(`article`,`id_cat`,`id_unit`,`code`,`barcode`,`info`) VALUES ('$article', '1', '1', '$code', '$barcode', '$textarea')");
if(mysql_affected_rows()>0){
    echo "1";
    }else{
    echo "2";
}
?>

我得到这个代码:在数据库中插入记录使用jQuery和PHP

首先你似乎混淆了jQuery和php。你的服务器端代码是php的。现在,您应该检查mysql_affected_rows实际返回的内容。

<?php
var_dump(mysql_affected_rows());
?>

并检查您的页面接收到什么:

console.log(data);

如果php脚本抛出NOTICE或在php标签之外有一个字符,javascript代码将接收到比'1'或'2'更多的东西

Chrome有一个不错的控制台。

顺便说一句,mysql函数已弃用。使用mysql代替。你有程序版本,所以这没什么大不了的。http://php.net/manual/en/function.mysql-affected-rows.php