wondernate Posted September 1, 2009 Share Posted September 1, 2009 I am doing a tutorial from a book and I can't get it to work. I have proofread the code over and over. I am getting an error when trying to use the second function named emailChecker. The first is just my DB connection and I blocked out the info for security... Here is the error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'VALUES ('test@testing.com')' at line 2 Here is the code Here is my PHP code that has the SQL in it. Anyone see a prob? <?php //Function to connect to the database and test the connection function doDB() { global $mysqli; // Establish a connection to the database $mysqli = new mysqli("xxxxx", "xxxxx","xxxx","xxxxx"); // if connection fails, stop script execution if (mysqli_connect_errno()){ printf("DATABASE CONNECTION FAILED: ", mysqli_connect_error()); } } function emailChecker ($email) { global $mysqli, $check_res; //check that email is not already in list $check_sql = "SELECT id FROM subscribers WHERE email = '".$email."'"; $check_res = mysqli_query($mysqli, $check_sql) or die(mysqli_error($mysqli)); } ?> Quote Link to comment Share on other sites More sharing options...
PFMaBiSmAd Posted September 1, 2009 Share Posted September 1, 2009 The query that is failing is probably an INSERT query, not the one that you posted (unless $email contains some SQL in it.) Quote Link to comment Share on other sites More sharing options...
wondernate Posted September 1, 2009 Author Share Posted September 1, 2009 Here is the code that is executing the function... I put in an echo test and the email is in POST. I tested the header function. I tested the connection. The problem seems to hit when I get to using the emailChecker function... //trying to subscribe, validate email address if ($_POST["email"] == "") { header("Location: manage.php"); exit; } else { //connect to database doDB(); //check that email is in list emailChecker($_POST["email"]); //get number of results and do action if (mysqli_num_rows($check_res) < 1) { // free result mysqli_free_result($check_res); //add record $add_sql = "INSERT INTO subscribers (email VALUES ('".$_POST["email"]."')"; $add_res = mysqli_query($mysqli, $add_sql) or die(mysqli_error($mysqli)); $display_block = "<p>Thanks for Signing Up!</p>"; //close connection to mysql mysqli_close($mysqli); } else { //print failure message $display_block = "<p> You are already subscribed!</p>"; } } } Quote Link to comment Share on other sites More sharing options...
wondernate Posted September 1, 2009 Author Share Posted September 1, 2009 How do you mark something as solved? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.