PHP Mysql Insert Into不工作,没有错误,没有数据发布到DB

时间:2013-09-04 20:22:41

标签: php mysql insert-into

尝试为朋友公司设置保修注册页面,我不擅长Mysql或PHP(阅读:Noob)。我已经在网上搜索了答案,并尝试了以下代码的几种变体而没有成功。

我的表设置包含匹配的列名。我相信,表单提交也正确设置。

不确定是什么阻止它实际将数据发布到数据库。任何帮助将不胜感激。

这是我的邮政编码。

<?php
error_reporting(-1);


$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$address = $_POST['address'];
$city = $_POST['city'];
$state = $_POST['state'];
$zipcode = $_POST['zipcode'];
$country = $_POST['country'];
$phone = $_POST['phone'];
$email = $_POST['email'];
$ordernumber = $_POST['ordernumber'];
$receivedate = $_POST['receivedate'];
$placeofpurchase = $_POST['placeofpurchase'];
$newsletter = $_POST['newsletter'];
?>

<?php
$con = mysqli_connect("localhost","DB_Username","PASSWORD","DB_NAME");
if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }

$sql = ("INSERT INTO warrantyregistration (firstname, lastname, address, city, state, zipcode, country, phone, email, ordernumber, receivedate, placeofpurchase, newsletter)
VALUES
($firstname, $lastname, $address, $city, $state, $zipcode, $country, $phone, $email, $ordernumber, $receivedate, $placeofpurchase, $newsletter)");


if (mysqli_query($con,$sql))
  {
  die('Error: ' . mysqli_error($con));
  }
echo "Success!";

mysqli_close($con)
?>

4 个答案:

答案 0 :(得分:0)

更改此行:

if (mysqli_query($con,$sql))

为:

if (!mysqli_query($con,$sql))

,您应该会看到一条错误消息。

答案 1 :(得分:0)

您应该使用预先准备好的查询并使用准备好的语句。这将保护您免受SQL注入并修复您的问题。您需要做的简略版本是:

$stmt = mysqli_prepare($con, "INSERT INTO warrantyregistration
    (firstname, lastname, address) VALUES (?, ?, ?)");
mysqli_stmt_bind_param($stmt, "sss", $firstname, $lastname, $address);
mysqli_stmt_execite($stmt);

答案 2 :(得分:0)

我得到了同样的错误。如果没有参数化,您可以在sql语句中修复该错误:

...
$sql = "INSERT INTO warrantyregistration VALUES ($firstname, $lastname, $address, $city, $state, $zipcode, $country, $phone, $email, $ordernumber, $receivedate, $placeofpurchase, $newsletter)";
...

答案 3 :(得分:0)

SQL语句的语法错误。您必须将Php变量设置为´$variable´

在这里查看我的工作代码:

insert into user
  (
    userid,
    signedin_at,
  )
  values
  (
    '$vp_userid',
    now()
  );