Jump to content

[SOLVED] mysqli error...


wondernate

Recommended Posts

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

}

 

?>

Link to comment
Share on other sites

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>";

  }

  }

}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.