简体   繁体   中英

php mysql jquery: data not inserting

I'm getting an insert error upon submitting form data to a local MySQL database. I've been working on this for a while and can't quite understand what the problem is. I would appreciate someone who can look over this and tell me what I've done wrong.

Thanks

Here is the relevant code:

jquery.main.js:

//jquery.main.js

$('#submit_second').click(function(){
    //send information to server
    var data = $('#kisForm :input').serializeArray();
    $.post( $('#kisForm').attr('action'), data, function(info) {
        $('#result').html(info);
    });
    alert('Data sent')
    return;
});


$('kisForm').submit( function() {
    return false;
});

userInfo.php

    <?php

include_once('db.php');

    $firstname = $_POST['firstname'];
    $middlename = $_POST['middlename'];
    $lastname = $_POST['lastname'];

 $query = ("INSERT INTO users VALUES ('$firstname', '$middlename', '$lastname')");

if(!$conn){
   die("Can not connect: " .mysql_error());
  }

if(mysql_query($query)){
    echo "Record added successfully";[];
  } else {
    echo "Error: Record insert failed";
  }

     mysql_close($conn);

index.html

<!DOCTYPE HTML>
<head>
    <meta http-equiv="Content-Type" content="text/html;charset=UTF-8"/>
    <title></title>

    <link rel="stylesheet" type="text/css" href="css/style.css" media="all"/>
    <link rel="stylesheet" href="http://code.jquery.com/ui/1.10.3/themes/smoothness/jquery-ui.css">

    <script type="text/javascript" src="js/jquery-1.10.2.min.js"></script>
    <script type="text/javascript" src="js/jquery-ui.min.js"></script>
    <script type="text/javascript" src="js/jquery.main.js"></script>
    <script type="text/javascript" src="js/cities.js"></script>
    <script type="text/javascript" src="js/masked_input_1.3.1.js"></script>


</head>
<body>

<div id="container">
    <form id=kisForm action="userInfo.php" method="post">

        <!-- #first_step -->
        <div id="first_step">
            <h1>Volunteer Signup Form</h1>

            <div class="form">
                <input type="text" name="firstname" id="firstname" placeholder="first name"/>
                <label for="firstname">Your first name. </label>

                <input type="nr" name="middlename" id="middlename" placeholder="middle name"/>
                <label for="middlename">Your middle name if applicable. </label>

                <input type="text" name="lastname" id="lastname" placeholder="last name"/>
                <label for="lastname">Your last name.</label>

                <!-- clearfix -->
                <div class="clear"></div>

                <input class="submit" type="submit" name="submit_first" id="submit_first" value="" />
            </div>

        </div>

        <!-- #second step -->
        <div id="second_step">
            <h1>KIS Volunteer Signup Form</h1>

            <div class="form">

                <h2>Summary</h2>

                <div id="leftSummary">
                    <table>
                        <tr><td>First Name: </td><td></td></tr>
                        <tr><td>Middle Name:</td><td></td></tr>
                        <tr><td>Last Name: </td><td></td></tr>
                    </table>

                </div>

                <span id="result"></span>

            </div>
            <!-- clearfix -->
            <div class="clear"></div>
            <!-- /clearfix -->
            <input class="back" type="button" value=""/>
            <input class="send submit" type="submit" name="submit_second" id="submit_second" value=""/>
        </div>

        <div id = "third_step">

        </div>

    </form>

</div>
<div id="progress_bar">
    <div id="progress"></div>
    <div id="progress_text">0% Complete</div>
</div>


</body>
</html>

Please show us the error:

I think this part is not complete, you need to specify table fields.

$query = ("INSERT INTO users VALUES ('$firstname', '$middlename', '$lastname')");

change to

$query = ("INSERT INTO users (firstname, middlename, lastname) VALUES ('$firstname', '$middlename', '$lastname')");

Or if you don't want to specify, put '' in the field

$query = ("INSERT INTO users VALUES ('', '$firstname', '$middlename', '$lastname')");

I'm assuming that the first field is userid.

Okay, so based on what you showed us above, from what i understand, the problem is here in this code

 $query = ("INSERT INTO users VALUES ('$firstname', '$middlename', '$lastname')");

If you are not specifying the fields that you want to insert the values into, you must complete the fields in the query

For example. In your users table you have 5 fields, if you want only to insert into firstname , middlename , and lastname specify them.

 $query = ("INSERT INTO users(firstname, middlename, lastname) VALUES ('$firstname', '$middlename', '$lastname')");

But if your table have only have 3 fields, i don't see anything wrong with your query.

But if your gonna use that syntax of yours, you should at least complete the 5 fields(Assuming you have 5 fields.)

$query = ("INSERT INTO users VALUES ('$id','$firstname', '$middlename', '$lastname','$username')");

Conclusion:

If you have 5 fields on your table, and you do not want to specify the fields on your query, your query must contain 5 values in the values();

But if you only want to insert into 3 fields in a 5 field table, you must specify the fields or it will throw an error.

Ok, I finally figured it out.

First of all, originally I wasn't returning a MySQL error, I was returning my own coded error so I didn't have visibility into the real problem. So I echoed, 'mysql_error(), and determined that the error was that the database was undefined. I was using the 'user' table as the 'mysql_select_db()' argument. When I put the database in the argument, everything started working fine.

Here is the fix:

mysql_select_db('kis'); <---'kis' is the database, not 'users.'

I also put the database connection code in the same php file as the select code instead of as an 'include' file, but I'm sure that doesn't have anything to do with it.

My sincere thanks to Nikko, Kimbarcelona, Patrick Q and user3462511 for taking your time and effort to help.

Here is the updated php file:

<?php

$conn = mysql_connect('localhost', 'root', '');
 mysql_select_db('kis');

if(!$conn){
    die("Can not connect: " .mysql_error());
}

    $firstname = $_POST['firstname'];
    $middlename = $_POST['middlename'];
    $lastname = $_POST['lastname'];

    if(mysql_query("INSERT INTO users VALUES ('','$firstname', '$middlename', '$lastname')")) {
        echo "Successfully Inserted Data";

    } else echo "Error: " , mysql_error();

     mysql_close($conn);      

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM