Inserting data from front end to mysql db in angularjs

后端 未结 1 1405
伪装坚强ぢ
伪装坚强ぢ 2020-12-23 17:44

I am trying to insert data from front end to mysql db using angularjs. But it is not geting inserted to the db even though there are no error messages. Foll

相关标签:
1条回答
  • 2020-12-23 18:49

    Your script for inserting the data is wrong. Replace it with the following

    $http.post("server/insert.php",{'fstname': $scope.newFriend.fname, 'lstname': $scope.newFriend.lname})
            .success(function(data, status, headers, config){
                console.log("inserted Successfully");
            });
    

    and also change the php as follows.

    $data = json_decode(file_get_contents("php://input"));
    $fstname = mysql_real_escape_string($data->fstname);
    $lstname = mysql_real_escape_string($data->lstname);
    mysql_connect("localhost", "root", "") or die(mysql_error()); 
    mysql_select_db("angularjs") or die(mysql_error()); 
    mysql_query("INSERT INTO friends (fname,lname) VALUES ('$fstname', '$lstname')"); 
    Print "Your information has been successfully added to the database."; 
    

    This worked for me when I tried with your code.

    0 讨论(0)
提交回复
热议问题