2014-07-26 51 views
0

创建新注册时,我无法写入数据库....!我有既有的登录和注册部分,如下所示一个javascript ...这是更新的脚本和PHP脚本两者的代码版本的登录ANC使用jquery php mysql提交数据?

<!DOCTYPE html> 
<html> 
<head> 
<title>Load </title> 
<meta name="viewport" content="width=device-width, height=device-height, initial-scale=1.0"/> 
<link rel="stylesheet" href="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.css" /> 
<script src="http://code.jquery.com/jquery-1.9.1.min.js"></script> 
<script src="http://code.jquery.com/mobile/1.3.2/jquery.mobile-1.3.2.min.js"></script> 
<script src="js/index.js"></script> 
</head> 
<body> 
<div data-role="page" id="login" data-theme="b"> 
    <div data-role="header" data-theme="a"> 
     <h3>Login Page</h3> 
    </div> 

    <div data-role="content"> 
     <form id="check-user" class="ui-body ui-body-a ui-corner-all" data-ajax="false"> 
      <fieldset> 
       <div data-role="fieldcontain"> 
        <label for="username">Enter your username:</label> 
        <input type="text" value="" name="username" id="username"/> 
       </div>         
       <div data-role="fieldcontain">          
        <label for="password">Enter your password:</label> 
        <input type="password" value="" name="password" id="password"/> 
       </div> 
       <input type="button" data-theme="b" name="submit" id="submit" value="Submit"> 
      </fieldset> 
      <a href="#registerp" data-role="button">Register</a> 
     </form>        
    </div> 

    <div data-theme="a" data-role="footer" data-position="fixed"> 

    </div> 
</div> 
<div data-role="page" id="registerp"> 
    <div data-theme="a" data-role="header"> 
     <h3>Register</h3> 
    </div> 
    <div data-role="content"> 
     <form id="registerform" class="ui-body ui-body-a ui-corner-all" data-ajax="false"> 
      <fieldset> 
       <div data-role="fieldcontain"> 
        <label for="fname">First Name:</label> 
        <input type="text" value="" name="fname" id="fname"/> 
       </div>   
       <div data-role="fieldcontain"> 
        <label for="lname">Last Name:</label> 
        <input type="text" value="" name="lname" id="lname"/> 
       </div> 
       <div data-role="fieldcontain"> 
        <label for="uname">User Name:</label> 
        <input type="text" value="" name="uname" id="uname"/> 
       </div>     
       <div data-role="fieldcontain">          
        <label for="pwd">Enter your password:</label> 
        <input type="password" value="" name="pwd" id="pwd"/> 
       </div> 

       <div data-role="fieldcontain"> 
        <label for="email">Email:</label> 
        <input type="text" value="" name="email" id="email"/> 
       </div> 
       <input type="button" data-theme="b" name="submit" id="register" value="Register"> 
      </fieldset> 
     </form>  
    </div> 

    <div data-theme="a" data-role="footer" data-position="fixed"> 
     <h3>Page footer</h3> 
    </div> 
</div> 
<div data-role="page" id="second"> 
    <div data-theme="a" data-role="header"> 
     <h3>Welcome Page</h3> 
    </div> 

    <div data-role="content"> 
     Welcome 
    </div> 

    <div data-theme="a" data-role="footer" data-position="fixed"> 
     <h3>Page footer</h3> 
    </div> 
</div> 
<script type="text/javascript"> 
    $(document).on('pageinit', '#login', function(){ 
    $(document).on('click', '#submit', function() { // catch the form's submit event 
     if($('#username').val().length > 0 && $('#password').val().length > 0){ 
      // Send data to server through the ajax call 
      // action is functionality we want to call and outputJSON is our data 
       $.ajax({url: 'check.php', 
        data: "action=login&" + $('#check-user').serialize(), 
        type: 'post',     
        async: 'true', 
        dataType: 'json', 
        beforeSend: function() { 
         // This callback function will trigger before data is sent 
         $.mobile.showPageLoadingMsg(true); // This will show ajax spinner 
        }, 
        complete: function() { 
         // This callback function will trigger on data sent/received complete 
         $.mobile.hidePageLoadingMsg(); // This will hide ajax spinner 
        }, 
        success: function (result) { 
         if(result.status) { 

          $.mobile.changePage("#second"); 

         } else { 
          alert('Log on unsuccessful!'); 
         } 
        }, 
        error: function (request,error) { 
         // This callback function will trigger on unsuccessful action    
         alert('Network error has occurred please try again!'); 
        } 
       });     
     } else { 
      alert('Please fill all necessary fields'); 
     }   
     return false; // cancel original event to prevent form submitting 
    }); 
    }); 

</script> 
<script type="text/javascript"> 
    $(document).on('pageinit', '#registerp', function(){ 
    $(document).on('click', '#register', function() { 
    if($('#uname').val().length > 0 && $('#pwd').val().length > 0){ 
      // Send data to server through the ajax call 
      // action is functionality we want to call and outputJSON is our data 
       $.ajax({url: 'insert.php', 
        data: "action=register&" + $('#registerform').serialize(), 
        type: 'post',     
        async: 'true', 
        dataType: 'json', 
        beforeSend: function() { 
         // This callback function will trigger before data is sent 
         $.mobile.showPageLoadingMsg(true); // This will show ajax spinner 

        }, 
        complete: function() { 
         // This callback function will trigger on data sent/received complete 
         $.mobile.hidePageLoadingMsg(); // This will hide ajax spinner 
        }, 
        success: function (result) { 
         if(result.status) { 

          $.mobile.changePage("#second"); 

         } else { 
          alert(' Try again later ! Server is busy !'); 
         } 
        }, 
        error: function (request,error) { 
         // This callback function will trigger on unsuccessful action    
         alert('Network error has occurred please try again!'); 
        } 
       });     
     } else { 
      alert('Please fill all necessary fields'); 
     }   
     return false; // cancel original event to prevent form submitting 
    }); 
    }); 
    </script> 
</body> 
</html> 

While my PHP Script is simple as shown below... please help 


    <?php 

    $con=mysqli_connect("...............", "...........", ".........","........"); 
    // Check connection 
    if (mysqli_connect_errno()) { 
    echo "Failed to connect to MySQL: " . mysqli_connect_error(); 
    } 

    // escape variables for security 
    $fname = mysqli_real_escape_string($con, $_POST['fname']); 
    $lname = mysqli_real_escape_string($con, $_POST['lname']); 
    $uname = mysqli_real_escape_string($con, $_POST['uname']); 
    $email = mysqli_real_escape_string($con, $_POST['email']); 
    $password = mysqli_real_escape_string($con, $_POST['pwd']); 

    $action = $_POST['action']; 
    // Decode JSON object into readable PHP object 
    //$formData = json_decode($_POST['formData']); 

    $sql="INSERT INTO userdb (username, fname, lname, password, email) VALUES ('$uname', '$fname', '$lname', '$password','$email')"; 

    if (!mysqli_query($con,$sql)) { 
    die('Error: ' . mysqli_error($con)); 
    } 
    echo "1 record added"; 

    mysqli_close($con); 

    if($action == 'register'){ 
    $output = array('status' => true, 'message' => 'Registered'); 
    } 

    echo json_encode($output); 
    ?> 

Insert php script doesnt work while the below register php script works fine. 

<?php 
// We don't need action for this tutorial, but in a complex code you need a way to determine Ajax action nature 
$action = $_POST['action']; 
// Decode JSON object into readable PHP object 
//$formData = json_decode($_POST['formData']); 

// Get username 
$username = $_POST['username']; 
// Get password 
$password = $_POST['password']; 

$db = @mysql_connect('..........', '........', '..........') or die("Could not connect database"); 
@mysql_select_db('users', $db) or die("Could not select database"); 


$result = mysql_query("SELECT `password` FROM `userdb` WHERE `username`= '$username'"); 
$r = mysql_fetch_assoc($result); 
$pass_ret = $r['password']; 

// Lets say everything is in order 
if($action == 'login' && $password == $pass_ret){ 
$output = array('status' => true, 'message' => 'Login'); 
} 
else 
{ 
$output = array('status' => false, 'message' => 'No Login'); 

} 
echo json_encode($output); 

?>

+1

*“它总是返回错误.....在我的注册脚本”* - 是? –

+1

请让我们知道错误 –

+0

相同在我的代码的警报消息警报('网络错误发生,请再试!'); – user2939685

回答

0

你使用插入语句的方式是错误的......它被错误地封装为@rbcummings说。

您必须更改

$sql="INSERT INTO userdb (username, fname, lname, password, email) VALUES ('$uname', '$fname', '$lname', '$password','$email')"; 

$sql="INSERT INTO userdb (username, fname, lname, password, email) VALUES (".$uname.", ".$fname.", ".$lname.", ".$password.",".$email.")"; 

没有适当intentation你可以得到errors..so打算我们的代码就可以解决问题乌尔。

+0

它仍然无法正常工作...... :(它仍然会出现关于错误的警告消息... – user2939685

+0

我已经更新了代码,请您再看一遍吗? – user2939685

0

尝试改变你的变量封装的方式。例如:

$sql="INSERT INTO userdb (username, fname, lname, password, email) VALUES (".$uname.", ".$fname.", ".$lname.", ".$password.",".$email.")"; 
+0

它仍然没有工作...... :(它仍然得到我关于错误的警报消息... – user2939685

+0

我也试过..我附上我的整个JavaScript的登录和注册都实现了登录工作正常,但登记不还附带了insert.php – user2939685