php只将一些值写入数据库

时间:2013-06-07 01:10:43

标签: php javascript html ajax mysqli

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

============== EDIT =============

未写入的值是名字,姓氏和作业。 ($ fname,$ lname和$ job)

============== EDIT =============

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>

3 个答案:

答案 0 :(得分: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'];

关于作业,您刚刚没有将其添加到INSERT声明中:

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

答案 1 :(得分:0)

$sql="INSERT INTO". $tbl_name ."(First Name, Last Name, Email, Address, Job) VALUES('". $_POST['first_name'] ."', '". $_POST['last_name'] ."', '". $_POST['adress'] ."', '". $_POST['email'] ."')";

答案 2 :(得分:0)

PHP中有错误的字段名称。

更改...

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

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

同时检查insert声明。它对字段有错误的值 - 电子邮件地址,地址Eamil。