0

I am getting the proper log in the console but it isn't going to the database I cannot see anything wrong with my PHP, though. Can anyone see what might be the problem?

JQuery code:

 $( "#dialog-form" ).dialog({
      autoOpen: false,
      height: 300,
      width: 350,
      modal: true,
      buttons: {
        "Create an account": function() {

        var fname = $( "#guestfname" ).val();
        var lname = $( "#guestlname" ).val();
        var gender = $( "#guestgender" ).val();
        var address = $( "#guestaddress" ).val();
        var city = $( "#guestcity" ).val();
        var state = $( "#gueststate" ).val();
        var zip = $( "#guestzip" ).val();
        var phone = $( "#guestphone" ).val();
        var email = $( "#guestemail" ).val();
        var dob = $( "#guestdob" ).val();

        var dataString ={fname:fname, lname:lname, gender:gender, address:address,
                            city:city, state:state, zip:zip, phone:phone, email:email,
                            dob:dob};
        console.log(dataString);    
            $.ajax({        
                type: "POST",
                url: "classes/add_guest.php",
                data: dataString,
                cache: false,
                success: function(html)
                {
                    $('.guestinfo').html(html);
                }
            });
            $( this ).dialog( "close" );
        },
        Cancel: function() {
          $( this ).dialog( "close" );
        }
      },
      close: function() {
      }
    });

    $( "#create-user" )
      .button()
      .click(function() {
        $( "#dialog-form" ).dialog( "open" );
      });
  });

PHP mySQL query code:

<?php
//open connection
require_once('../config/db.php');
$con = mysqli_connect(DB_HOST, DB_USER, DB_PASS, DB_NAME);

//get date
$fname = $_POST['fname'];
$lname =  $_POST['lname'];
$gender = $_POST['gender'];
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$zip = $_POST['zip'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$dob = $_POST['dob'];


mysqli_query($con, 'INSERT INTO guests(fname, lname, gender, address, city, state, zip, phone, email, dob)
                    VALUES("'.$fname.'","'.$lname.'","'.$gender.'","'.$address.'","'.$city.'","'.$state.'",'.
                    $zip.','.$phone.',"'.$email.'",'.$dob.'');

mysqli_close($con);
?>

4 Answers 4

1

First off, you're not escaping anything. Or using prepared statements. Hello, Bobby Tables (or SQL injection if you like).

Second, you're not wrapping your values in your statements in quotes. For instance, $phone isn't wrapped. I don't think MySQL would take too kindly to

INSERT INTO table(phone) VALUES(123-867-5309)

So we need to quote all your strings

INSERT INTO table(phone) VALUES("123-867-5309")

Third, What's going on with your AJAX? Did you use something like Firebug to make sure your AJAX was being sent properly? And have you ever heard of serialize? MUCH easier way to post your form data.

Sign up to request clarification or add additional context in comments.

Comments

1

There is a slight problem, string must be single quoted before passed to MySQL server:

mysqli_query($con, 'INSERT INTO guests(fname, lname, gender, address, city, state, zip, phone, email, dob)
                VALUES("'.$fname.'","'.$lname.'","'.$gender.'","'.$address.'","'.$city.'","'.$state.'",'.
                $zip.','.$phone.',"'.$email.'",'.$dob.'');

=> Good syntax here:

mysqli_query($con, "INSERT INTO guests(fname, lname, gender, address, city,
  state, zip, phone, email, dob)
VALUES('$fname', '$lname', '$gender', '$address', '$city',
  '$state', '$zip', '$phone', '$email', '$dob'");

Comments

1

First of all, your query string doesn't need to be added together, PHP can pick out which are variables. So try this

mysqli_query($con, "INSERT INTO guests (fname, lname, gender, address, city, state, zip, phone, email, dob) VALUES ('$fname','$lname','$gender','$address','$city','$state','zip','$phone','$email','$dob')";

If that doesn't work add error_reporting(E_ALL); to the top of your page try changing all your $_POST's to $_REQUEST's. Then go to the page with the variables in the URL (GET Method). That should tell you the error. For example website.com/add_guest.php?lname=Dave

Comments

0

Thanks for the help all. We all missed it, though. mysqli_query($con, "INSERT INTO guests(fname, lname, gender, address, city, state, zip, phone, email, dob) VALUES('$fname', '$lname', '$gender', '$address', '$city', '$state', '$zip', '$phone', '$email', '$dob')");

Forgot that ) to close the VALUES.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.