-2

我的 php 出现问题,没有将它从我的 ajax 脚本获取的值写入我的 MySQL 数据库。我知道 php 脚本正在获取这些值,因为它们正在我的浏览器中回显。但是当我检查我的数据库时,五个值中只有两个被输入。我确信这不是细微差别,但我似乎无法破解它。

==============编辑==============

未写入的值是名字、姓氏和工作。(分别为 $fname、$lname 和 $job)

==============编辑==============

PHP

<?php

//db connecting variables
  $hostname = "foobase";
  $username = "foobase";
  $dbname = "contactformbase";
  $password = "password";
  $con = new mysqli($hostname, $username, $password, $dbname);
  $tbl_name = "client_base";

//Connecting to your database
  if ($con->connect_error) {
        die('Connect error (' . mysqli_connect_errno() . ')' . mysqli_connect_errno());
    }
    echo 'success!...' . $con->host_info . "\n";
  print_r($_POST);
  $fname = $_POST['fname'];
  $lname = $_POST['lname'];
  $email = $_POST['email'];
  $address = $_POST['address'];
  $job = $_POST['job'];
  $message = $_POST['message'];

//adding values into the database.
  $sql="INSERT INTO $tbl_name (First Name, Last Name, Email, Address, Job)VALUES('POST_['first_name']', '$lname', '$address', '$email')";
  $result = mysqli_query($con, $sql); 
    if($result){
      echo "success";
      }
    else {
      echo "error";
      }

Javascript

<script type="text/javascript">
    $("#submit").click(function(e) {
        e.preventDefault();
var data_string = $("form#contact").serializeArray();
alert(data_string);
$.ajax({
    type: "POST",
    url: "database.php",
    data: data_string,
    success: function(){
        alert(data_string);
    }
});
return false;
</script>

HTML

<form action="" method="POST" id="contact">
    <table>
        <tbody>
            <tr>
                <td><h2>First Name: </h2></td>
                <td><h2>Last Name: </td>
                <td><h2>Email Address: </td>
            </tr>
            <tr>
                <td><input type="text" name="first_name" placeholder="Johnny"></td>
                <td><input type="text" name="last_name" placeholder="Appleseed"></td>
                <td><input type="text" name="email" placeholder="johnny@email.com"></td>
            </tr>
            <tr>
                <td><h2>Street Address:</h2></td>
                <td><h2>What's Dirty?</h2></td>
            </tr>
            <tr>
                <td><input type="text" name="address" placeholder="123 Applegrove Rd. Appletown, VA 12345"></td>
                <td>
                    <select name="job" form="contact">
                        <option value="house">House</option>
                        <option value="roof">Roof</option>
                        <option value="garage-shed">Garage/shed</option>
                        <option value="other">Other</option>
                    </select>
                </td>
            </tr>
            <tr>
                <td><h2>Message: </h2></td>
            </tr>
        </tbody>
    </table>
   <textarea name="message">
</form>
4

3 回答 3

0

您正在使用奇怪的变量POST_['first_name']

试试这个查询:

注意:您必须为字段使用引号(' '),因为字段名称中有空格或使用 PDO 来准备和执行查询。)

$sql="INSERT INTO $tbl_name (First Name, Last Name, Email, Address, Job)
      VALUES('$fname', '$lname', '$address', '$email')";

另一个问题是POST数组,你发送first_name而不是fnamelast_name而不是lname

<tr>
    <td><input type="text" name="first_name" placeholder="Johnny"></td>
    <td><input type="text" name="last_name" placeholder="Appleseed"></td>
    <td><input type="text" name="email" placeholder="johnny@email.com"></td>
</tr>

这将返回:

$_POST = array(
   "first_name" => "Johnny",
   "last_name" => "Appleseed",
   "email" => "johnny@email.com"
);

如您所见,您没有$_POST['fname']$_POST['lname'] 因此您必须更改:

$fname = $_POST['first_name'];
$lname = $_POST['last_name'];

关于job,您只是没有在INSERT声明中添加它:

$sql="INSERT INTO $tbl_name (First Name, Last Name, Email, Address, Job)
      VALUES('$fname', '$lname', '$address', '$email', '$job')";
于 2013-06-07T01:18:59.550 回答
0
$sql="INSERT INTO". $tbl_name ."(First Name, Last Name, Email, Address, Job) VALUES('". $_POST['first_name'] ."', '". $_POST['last_name'] ."', '". $_POST['adress'] ."', '". $_POST['email'] ."')";
于 2013-06-07T01:19:44.777 回答
0

您的PHP.

改变...

$fname = $_POST['fname'];
$lname = $_POST['lname'];

$fname = $_POST['first_name'];
$lname = $_POST['last_name'];

还要检查insert声明。它的字段值错误 - 电子邮件地址,地址 Eamil。

于 2013-06-07T01:19:58.577 回答