我试图使用json将数据插入mysql这里是我的php代码没有问题,但它插入查询无法正常工作.JSON输出始终显示:{"code":0}
它必须是{"code":1}
帮助
<?php
header('Content-type=application/json; charset=utf-8');
$con = mysql_connect("xxxx","xxx","xxx");
$db = mysql_select_db("android_data");
$us = $_POST["uname"];
$pa = $_POST["upass"];
$pe = $_POST["uper"];
$flag["code"] = 0;
$sql = "insert into administrator (username,password,permission) values('".$us."','".$pa."','".$pe."')";
$query = mysql_query($sql);
if ($query)
{
$flag["code"] = 1;
echo json_encode($flag);
//echo $flag['StatusID'];
}
mysql_close($con);
echo json_encode($flag);
?>
答案 0 :(得分:0)
试试这个:
$query = mysql_query("INSERT INTO administrator (username, password, permission) VALUES( '$us', '$pa', '$pe' )");
还有一点需要注意,看起来您为了成功的查询而回显两次输出。
你可能想做这样的事情:
if ($query)
{
$flag["code"] = 1;
$flag["message"] = "success";
echo json_encode($flag);
} else {
// failed to insert row
$flag["code"] = 0;
$flag["message"] = "Oops! An error occurred.";
echo json_encode($flag);
}
mysql_close($con);