-1

The code I am using (below) doesn't work for some reason, I check everything matches in the html form - I just can't figure this out.

Could I be getting the error because something that is wrong on the HTML side? I'm using the jQuery form validator (this works fine) could this cause problems? 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();
?>
Dave
  • 3,280
  • 2
  • 22
  • 40
RICKY DAWN
  • 31
  • 2
  • 8

2 Answers2

-1

Your getting the error on

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

Which is executed later down your script and $query is not set. Your real insert is way up above.

$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);

You should also change that to $result=mysql_query($sql) or die(mysql_error());

and see if there is errors on the actual insert. remove the secondary query execution.

DevZer0
  • 13,433
  • 7
  • 27
  • 51
  • Thanks for the help, changed the line you mentions now I get the error: Column count doesn't match value count at row 1 – RICKY DAWN Jun 07 '13 at 14:34
  • you have 9 columns in your insert and 10 items in your values side I believe , '$terms' is the offending item. Either remove that value from the values side or add a terms column into the left side. – Dave Jun 07 '13 at 15:00
-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);
ketan
  • 19,129
  • 42
  • 60
  • 98