0

New to learning PHP form validation on same page. Please advise as to why my data might not be posting to the database. After filling out the form, it redirects to thank you page without sending data. Thanks!

http://pastebin.com/3T1W9Krx

Edit: Now that I know where my problem was, I have updated the Pastebin file to show the working code, which validates in the same page and checks the database for duplicate email addresses.

I was able to use Rick Kuipers suggestion below to find this error. I was trying to include a column for the primary key under VALUES, however I only needed the values for the INSERT keys, not ID or timestamp, as ID is set to auto-increment.

$sql = "INSERT INTO table (last_name, first_name, age) 

VALUES (". 
                        PrepSQL($last_name) . ", " .
                        PrepSQL($first_name) . ", " .
                        PrepSQL($age) . ")";

        mysql_query($sql);
        header("Location: volthankyou.php");
        exit();
    }
}
8
  • This is Q&A site, not fix-my-code site Commented Feb 28, 2012 at 19:54
  • I've worked really hard on this and just need some advice. Thanks though. Commented Feb 28, 2012 at 19:56
  • You are welcome to ask for the advise, not just throwing the code to fix Commented Feb 28, 2012 at 19:58
  • We are all working hard, just to let you know. Commented Feb 28, 2012 at 19:58
  • before echoing 'Thank you', add var_dump($_POST) and send the result. After that, you should check why data is not being inserted. Commented Feb 28, 2012 at 20:01

2 Answers 2

2

This could be because of a problem with your query.

Try doing the following:

echo mysql_error($db);
//header("Location: volthankyou.php");

This should display the error if there is any.

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

3 Comments

AH! Got it- Column count doesn't equal...the problem is that the first column is the primary key, however I'm not sure how to set this under VALUES using the PrepSQL prefix and $value... trying a few things- thank you!
If the first column "id" is set to auto_increment you can just leave it out, it will fill itself in.
Thank you Rick- you helped me find the solution.
0

Check if mysql_query is true or false for your insert. Otherwise, it will ALWAYS try and then, redirect to thankyou. And as spencercw points out, mysql_select_db could also be failing. Always check the result of such methods.

P.S.: always check server logs

1 Comment

mysql_select_db could also be failing.

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.