0

我在控制台中获得了正确的日志,但它没有进入数据库,不过我看不出我的 PHP 有什么问题。谁能看到可能是什么问题?

jQuery代码:

 $( "#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查询代码:

<?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

4 回答 4

1

有一个小问题,字符串在传递给 MySQL 服务器之前必须单引号:

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_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'");
于 2013-10-30T00:10:51.677 回答
1

首先,您的查询字符串不需要添加在一起,PHP 可以挑选出哪些是变量。所以试试这个

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')";

如果这不起作用,请添加error_reporting(E_ALL);到页面顶部尝试将所有$_POST's 更改为$_REQUEST's。然后转到带有 URL 中变量的页面(GET 方法)。那应该告诉你错误。例如website.com/add_guest.php?lname=Dave

于 2013-10-30T00:12:56.510 回答
1

首先,你没有逃避任何事情。或者使用准备好的语句。您好,Bobby Tables(如果您愿意,也可以使用 SQL 注入)。

其次,你没有用引号将你的价值观包装在你的陈述中。例如,$phone未包装。我不认为 MySQL 会太客气

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

所以我们需要引用你所有的字符串

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

第三,你的 AJAX 是怎么回事?您是否使用过 Firebug 之类的东西来确保您的 AJAX 被正确发送?你听说过serialize吗?发布表单数据的更简单方法。

于 2013-10-30T00:14:49.597 回答
0

感谢大家的帮助。不过,我们都错过了。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')");

忘记)关闭VALUES.

于 2013-10-30T00:28:57.183 回答