Hello 🙂
I need some help
i'm getting this error when i submit my information to the database
Error!
The following error(s) occurred:
- You could not be registered due to a system error. We apologize for any inconvenience.
- 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 ''testuser@gmail.com', 'blah blah')' at line 1
Query: INSERT INTO participants ('testuser@gmail.com', 'blah blah')
Please try again.
Here is my php code.
<?php
if (isset($_POST['submitted'])){
require_once('Connections/encourage.php');
$fields = array(
'email',
'state',
);
foreach($fields as $fieldName) {
if(isset($_POST[$fieldName]) and safe(trim(stripslashes($_POST[$fieldName]))) !==''){
$$fieldName = safe(trim(stripslashes($_POST[$fieldName])));
}else {
$errors[] = "Please enter your". $fieldName .""; //code to validate fields
}
if(filter_var($email, FILTER_VALIDATE_EMAIL) == true){
} else {
$errors[] = 'Please enter a valid email address';
}
}
if(!isset($errors)){
$query = "SELECT user_id FROM participants WHERE email='$email'";
$result = mysql_query($query);
if(mysql_num_rows($result) == 0) {
$query = "INSERT INTO participants ('$email', '$state')"; //databasse connection
$result = mysql_query ($query);
if ($result){
$url = 'http://'. $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
if ((substr($url, -1) == '/') || (substr($url, -1) == '\\')) {
$url = substr ($url, 0, -1);
}
$url .= '/thanks.php';
header("Location: $url"); //This is the line of error that is reporting after submitting to the database
exit();
} else {
$errors[] = 'You could not be registered due to a system error. We apologize for any inconvenience.';
$errors[] = mysql_error() . '<br /><br />Query: ' . $query;
}
} else {
$errors[] = 'The email address has already been registered.';
}
}
mysql_close();
} else {
$errors = NULL;
}
if (!empty($errors)){
echo '<h1 id="mainhead">Error!</h1>
<p class="error">The following error(s) occurred:<br />';
foreach($errors as $msg) {
echo " - $msg<br/>\n";
}
echo '</p><p>Please try again.</p><p><br/></p>';
}
function safe($string)
{
$pattern = "/\r|\n|\%0a|\%0d|Content\-Type:|bcc:|to:|cc:/i";
return preg_replace($pattern, '', $string);
}
?>
Here is the html code just in case
<form id="form1" name="form1" method="post" action="registration.php">
<fieldset class="first">
<label class="email" for="email">Email:* </label>
<input name="email" value="<?php if(isset($_POST['email'])) echo $_POST['email'];?>"/>
<label for="state"/>
Your State* </label>
<input name="state" value="<?php if(isset($_POST['state'])) echo $_POST['state'];?>"/>
<!--Re-EnterPHP Code-->
</fieldset>
<fieldset>
<input name="submit" type="submit" class="btn1" value="Complete your donation" />
<input class="btn" name="reset" type="reset" value="Clear Form" />
<input type="hidden" name="submitted" value="TRUE" />
</fieldset>
</form>
Thanks in advanced