douzhu6149 2014-10-27 20:51
浏览 38

将数据库数据检索到JSON - 移动登录表单

Im creating Mobile App using Phonegap that first need to login. if login success It will retrieve the data to JSON.

This is the PHP page that connects to mobile site and retrieve the data.

<?php
$con = mysqli_connect($mysql_hostname, $mysql_user, $mysql_password,$mysql_database);
if (mysqli_connect_errno()) {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}else{
    //echo 'ok';
}

if(isset($_POST['username']) && isset($_POST['password'])){
    $username = $_POST['username'];
    $password = $_POST['password'];
}

$sql = "SELECT * FROM member WHERE username = '$username' AND password = '$password'";
$result = mysqli_query($con,$sql);

if (!$result) {
  //die('Error: ' . mysqli_error($con));
  $data = array('status'=>'failure','Error: ' . mysqli_error($con));
}else {
    //$result = mysqli_query($con,$sql);
    $row = mysqli_fetch_array($result);
    $details = array(
                        'id'=>$row['id'],
                        'username'=>$row['username'],
                        'email'=>$row['email'],
                        'password'=>$row['password'],
                        'repassword'=>$row['repassword'],
                    );
    $data = array('status'=>'success','data'=>$details);
}
echo json_encode($data);
mysqli_close($con);
?>

but it doesnt work properly. I'm following my lecturer Code. but i think he has missed something

here my HTML content

<script>
    function login(){

var uName=$("#loginID").val();
var pwd=$("#pwdID").val();

$.post("http://boost.meximas.com/mobile/verifying.php", { username: uName, password: pwd },
   function(json){
console.log(json);
var obj = jQuery.parseJSON(json );
console.log(obj.status);
if(obj.login == "success"){
alert("success");
}
else
alert("Incorrect user name or password");
});
}
    </script>


<div data-role="content">
                <Table>
    <tr><td>Login</td>
    <td><input class="fld" id="loginID" name="txtNum1" type="text" value="" /></td></tr>
    <tr><td>Password</td>
    <td><input class="fld" id="pwdID" name="txtNum1" type="text" value="" /></td></tr>

</Table>
    <a class="ui-button" data-role="button" onClick="login();" data-transition="slide" id="flipid" >Login</a>
        </div>
  • 写回答

1条回答 默认 最新

  • doumor942473 2014-10-27 22:07
    关注

    There are a few issues I can see off the bat:

    1. You are testing if isset($_POST['username']) && isset($_POST['password']) but, you are not actually catching the case where they are not set, and you are continuing to execute the code with null/empty values.
    2. the SQL injection issue I mentioned in my comment to your question, you should never let this code be visible on a public facing website.

    Without more detail, I am unable to say if the issue lies with the HTML, the PHP or the JavaScript.

    I would change change the if/else when testing the username/password to return something like: $data = array('status'=>'failure','Error' => 'Invalid username/password'); when the username/password is not set.

    I hope this helps.

    评论

报告相同问题?

悬赏问题

  • ¥30 VMware 云桌面水印如何添加
  • ¥15 用ns3仿真出5G核心网网元
  • ¥15 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”