将表单数据推送到Database PHP,MYSQL时,“查询为空”错误

时间:2013-06-07 14:06:53

标签: php mysql

我正在使用的代码(下面)由于某些原因不起作用,我检查了html表单中的所有匹配项 - 我只是无法解决这个问题。

我是否可以收到错误,因为HTML方面有问题?我正在使用jQuery表单验证器(这很好)可能导致问题吗? http://jqueryvalidation.org/

<?php

$host="localhost"; // Host name 
$username="USERNAME"; // Mysql username 
$password="PASSWORD"; // Mysql password 
$db_name="DBNAME"; // Database name 
$tbl_name="Persons"; // Table name 

// Connect to server and select database.
mysql_connect("$host", "$username", "$password")or die("cannot connect"); 
mysql_select_db("$db_name")or die("cannot select DB");

// Get values from form 
$fname=$_POST['fname'];
$lname=$_POST['lname'];
$company=$_POST['company'];
$email=$_POST['email'];
$registered=$_POST['registered'];
$percentage=$_POST['percentage'];
$products=$_POST['products'];
$prize=$_POST['prize'];
$terms=$_POST['terms'];
$newsletter=$_POST['newsletter'];


// Insert data into mysql 
$sql="INSERT INTO $tbl_name(FirstName, LastNname, Company, EmailAddress, Registered, PercentOfBusiness, ProductsSold, WhichPrize, Newsletter)VALUES('$fname', '$lname', '$company', '$email', '$registered', '$percentage', '$products', '$prize', '$terms', '$newsletter')";
$result=mysql_query($sql);

// if successfully insert data into database, displays message "Successful". 
if($result){
echo "Successful";
echo "<BR>";
echo "<a href='insert.php'>Back to main page</a>";
}

else {
echo "ERROR";
}
?> 

<?php
$result = mysql_query($query) or die(mysql_error());
?>

<?php 
// close connection 
mysql_close();
?>

2 个答案:

答案 0 :(得分:-1)

您收到错误

 <?php
  $result = mysql_query($query) or die(mysql_error());
 ?>

稍后在您的脚本中执行,并且未设置$query。你真正的插入是在上面。

$sql="INSERT INTO $tbl_name(FirstName, LastNname, Company, EmailAddress, Registered, PercentOfBusiness, ProductsSold, WhichPrize, Newsletter)VALUES('$fname', '$lname', '$company', '$email', '$registered', '$percentage', '$products', '$prize', '$terms', '$newsletter')";
$result=mysql_query($sql);

您还应将其更改为$result=mysql_query($sql) or die(mysql_error());

并查看实际插入是否有错误。删除辅助查询执行。

答案 1 :(得分:-1)

$sql="INSERT INTO $tbl_name VALUES(FirstName, LastNname, Company, EmailAddress, Registered, PercentOfBusiness, ProductsSold, WhichPrize, Newsletter)VALUES('$fname', '$lname', '$company', '$email', '$registered', '$percentage', '$products', '$prize', '$terms', '$newsletter')";
$result=mysql_query($sql);