Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

Trying to create a simple PHP MySql insert form that inserts an email address and random ten digit code and it keeps returning:

Please check email 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 ' NcB44PeYbI)' at line 1

NcB44PeYbI is obviously the random code generated.

function generateRandomString($length = 10) {
    $characters = '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
    $randomString = '';
    for ($i = 0; $i < $length; $i++) {
        $randomString .= $characters[rand(0, strlen($characters) - 1)];
    }
    return $randomString;
}

$con=mysqli_connect("localhost","username","password","dbname");
// Check connection
if (mysqli_connect_errno())
{
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
}

// escape variables for security
$email = mysqli_real_escape_string($_POST['email']);
$acticode = generateRandomString();



$sql="INSERT INTO xActivate (EMAIL_ADDRESS, ACTIVATION_CODE) VALUES ($email, $acticode)";

if (!mysqli_query($con,$sql))
{
  die('Error: ' . mysqli_error($con));
}
echo "1 record added";

mysqli_close($con);

Thanks for your assistance!

share|improve this question
add comment

2 Answers

up vote 1 down vote accepted

Try this:

$sql="INSERT INTO xActivate (EMAIL_ADDRESS, ACTIVATION_CODE) VALUES ('$email', '$acticode')";

share|improve this answer
    
Worked perfectly, thank you very much! –  TheDoonker Apr 13 at 2:07
    
No problem, glad I could help. –  Outsider Apr 13 at 2:07
add comment

Put the values in quotes:

$sql="INSERT INTO xActivate (EMAIL_ADDRESS, ACTIVATION_CODE) VALUES ('$email', '$acticode')";
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.