jquery - insert to mysql from ajax and php

238

Hi i am trying to insert data to mysql from ajax with php here is my code can anyone help. The only error i get is that the data is not insert, in my mysql the values is not pass in. Where is the problem in my ajax? or in my php when i pass values.

index.php

Now my file of php where the connection is done.

<?php
/**
 * Created by PhpStorm.
 * User: erevos13
 * Date: 26/6/2017
 * Time: 11:13 μμ
 */
//is the databases connection
include "bd.php";

if (!$connection) {
    die("Connection failed: " . mysqli_connect_error());
}

if (isset($_POST['name'])) {
    $name = $_POST['name'];
    $last_name = $_POST['last_name'];
    $email = $_POST['email'];
    $phone = $_POST['phone'];
    $bridge = $_POST['bridge'];
    $comments = $_POST['comments'];


    $sql = "INSERT INTO info (`id` , `name`, `last_name`, `email `, `phone`, `bridge` , `comments` ) VALUES ( '' , '" . $name . "', '" . $last_name . "','" . $email . "', '" . $phone . "' , '" . $bridge . "', '" . $comments . "')";

    $query = mysqli_query($connection, $sql);
    if ($query) {
        echo "data insert successfully";
    } else {
        echo "data is not insert";
    }

}

The only error i get is the "data is not insert".

683

Answer

Solution:

You should useconsole.log() function to check the data in the javascript code. And read a little bit more aboutserialize function at here. Your javascript code should be something like:

$.ajax({
   type    : "POST",
   url     : "ajax.php",
   data    : $("form").serialize();
189

Answer

Solution:

Tryval() in place ofserialize().Check code for jquery:

<script type="text/javascript">
    $(document).ready(function(){

        $("#submit").click(function(){
            var name = $("#name").val();
            var last_name = $("#last_name").val();
            var email = $("#email").val();
            var phone = $("#phone").val();
            var bridge = $("#bridge").val();
            var comments = $("#comments").val();

            $.ajax({
                type    : "POST",
                url     : "ajax.php",
                data    : {'name': name,'email': email ,'last_name':last_name,'phone':phone,'bridge': bridge, 'comments': comments},

                success : function(result) {
                    alert(result);

                }
            });
        });

    });
</script>
987

Answer

Solution:

Use.val() function instead.serialize(), if you have value of name isniklesh it will givename=niklesh and you expect onlyniklesh here.

$(document).ready(function(){
        $("#submit").click(function(){
            var name = $("#name").val();
            var last_name = $("#last_name").val();
            var email = $("#email").val();
            var phone = $("#phone").val();
            var bridge = $("#bridge").val();
            var comments = $("#comments").val();
            $.ajax({
                type    : "POST",
                url     : "ajax.php",
                data    : {"name": name,"email": email ,"last_name":last_name,"phone":phone,"bridge": bridge, "comments": comments},
                success : function(result) {
                    alert(result);
                }
            });
        });
    });

Also use parameterised mysqli :

http://php.net/manual/en/mysqli.prepare.php#refsect1-mysqli.prepare-examples

380

Answer

Solution:

Thanks to every one i found all the problem it was in :

 $sql = "INSERT INTO info ( name, last_name, email , phone, bridge, comments ) VALUES (  '{$name}' , ' {$last_name} ','{$email }', '{$phone}' , '{$bridge}','{$comments}')"; 

I manage to find put in this in my code.:

mysqli_error($connection)

and i find that the mysql it was not insert one row. I fix that and all is good.

People are also looking for solutions to the problem: javascript - BadMethodCallException Method [logout] does not exist

Source

Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.

Ask a Question

Write quick answer

Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.

Similar questions

Find the answer in similar questions on our website.