0

I'm trying to make this information insert into my table in mysql database with this script I wrote.

<?php
require("../includes/db.php");
$nme = $_POST["nme"];
$email = $_POST["email"];
$address = $_POST["address"];
$city = $_POST["city"];
$state = $_POST["state"];
$zip = $_POST["zip"];
$phone = $_POST["phone"];
$options = implode($_POST["options"],", ");
$query = mysql_query("insert into peeps (name, email, address, city, zip, phone, type) values ('$nme','$email','$address','$city','$state','$zip','$phone','$options')");
if($query)
    print "yes";
else
    print "no";

?>

The output of this code is no.

3
  • I can connect to my database fine. Commented Feb 8, 2012 at 5:24
  • print mysql_error(); to see what is wrong. Commented Feb 8, 2012 at 5:28
  • @zachdyer You should not use the mysql_ functions any more (read more). Use the mysqli (notice the i) class instead. Also, you are open to SQL injection attacks. Commented Feb 8, 2012 at 5:37

2 Answers 2

1

If mysql_query() returns false, it means the query failed.

Try this:

if (false === $query)  // make sure it's actually boolean false
  print mysql_error(); // print a nice plain-english description of the problem.
else
  print "Yes";

This should give you a good idea of where the problem is.

Sign up to request clarification or add additional context in comments.

1 Comment

Thank you. 'state' did not exist in my table so I added it. Now it works great.
1

If you modify this part of your code you can see the exact error you get.

if($query)
{
    print "yes";
}
else
{
    print mysql_error(); ;
}

However the way you have written may generate errors if you do not have enabled Magic Quotes. If you have that kind of error you better use mysql_escape_string

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.