Jump to content

[SOLVED] help with errror in sql


ndjustin20

Recommended Posts

I am having trouble with the following script:

 


<?php

#$page_title = 'New Register Page';

#include('./includes/Header.html');

if(isset($_POST['submitted'])) {

require_once('mysql_connect.php');
$errors = array();

if(empty($_POST['first_name'])) {
$errors[] = 'You forgot to enter your first name';
}else{
$fn = escape_data($_POST['first_name']);
}

if(empty($_POST['last_name'])) {
$errors[] = 'You forgot to enter your last name.';
}else{
$ln = escape_data($_POST['last_name']);
}

if(empty($_POST['email'])) {
$errors[] = 'You forgot to enter your email address';
}else{
$e = escape_data($_POST['email']);
}

if(!empty($_POST['password1']) && ($_POST['password1'] == $_POST['password2'])) {
$pw = escape_data($_POST['password1']);
}else{
$errors[] = 'The password you entered and the password confirmation do not match each other.';
}

if(empty($errors)) {
$query = "SELECT userid FROM users WHERE email='$e'";
$result = @mysql_query($query);
$rows = mysql_num_rows($result);
}
if ($rows > 0) {
echo "I am sorry we can not register you at this time because $e is already taken";
}else{
$npw = SHA1('$pw');
$query = "INSERT INTO users ('first_name', 'last_name', 'email', 'password', 'registration_date')
VALUES ('$fn', '$ln', '$e', '$npw', 'NOW()')";
$result = @mysql_query($query);
}
if($result) {

$url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);   
   if ((substr($url, -1) == '/') OR
     (substr($url, -1) == '\\') ) {$url = substr ($url, 0, -1);   
   }
   $url .= '/thanks.php';
   header("Location: $url");
   exit();
}elseif($result == FALSE){
$errors[] = 'You could not be registered do to a system error';
$errors[] = mysql_error() . '<br /><br />Query: ' . $query;

}
}else{
$errors[] = NULL;
}
$page_title = 'Register';
include('./includes/Header.html');

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>';
   }

?>
<h1>Register Here</h1>
<form action="register_brandnew.php" method="post">
<p>First Name<input type="text" name="first_name" value="<?php if(isset($_POST['first_name'])) echo $_POST['first_name'];?>" size="15" maxlength="20" /></p>
<p>Last Name<input type="text" name="last_name" value="<?php if(isset($_POST['last_name'])) echo $_POST['last_name'];?>" size="15" maxlength="20" /></p>
<P>Email Address<input type="text" name="email" value="<?php if(isset($_POST['email'])) echo $_POST['email'];?>" size="15" maxlength="20" /></P>
<p>Enter Password<input type="password" name="password1" size="15" maxlength="40" /></p>
<p>Confirm Password<input type="password" name="password2" size="15" maxlength="40" /></p>
<p><input type="submit" name="submit" value="Register" /></p>
<p><input type="hidden" name="submitted" value="TRUE" /></p>
</form>
<?php
include('./includes/Footer.html');
?>

 

 

The error I receive is the following from page www.freedomthinking.com/register_brandnew.php

Error!
The following error(s) occurred:
You could not be registered do to a system 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 ''first_name', 'last_name', 'email', 'password', 'registration_date') VALUES ('ik' at line 1

Query: INSERT INTO users ('first_name', 'last_name', 'email', 'password', 'registration_date') VALUES ('ik', 'ik', 'ik', '39458956a98d956366756222d98698dce788a347', NOW())


Please try again.

 

 

I am trying to figure out why I am receiving an sql error when it looks to be putting in the correct information including the correct field names in the db.  Any help would be greatly appreciated.

 

 

Justin

 

Link to comment
https://forums.phpfreaks.com/topic/103429-solved-help-with-errror-in-sql/
Share on other sites

simple solution :)

 

in your query:

INSERT INTO users ('first_name', 'last_name', 'email', 'password', 'registration_date') VALUES ('ik', 'ik', 'ik', '39458956a98d956366756222d98698dce788a347', NOW())

 

you have the fields in apostrophe's

 

remove the apostrophe's ... hehe.

 

INSERT INTO users (first_name, last_name, email, password, registration_date) VALUES ('ik', 'ik', 'ik', '39458956a98d956366756222d98698dce788a347', NOW())

 

oooooooorr you can use the inverted apostrophe (which i prefer) ... it's on the same key as tilde (~) ... so:

INSERT INTO users (`first_name`, `last_name` .....

 

:)

OMG!  It's so funny when you sit and stare at something at can't get it and it's something so simple.  I am really new, about two weeks, and love this site cause everyone is so helpful and friendly.  Thank you very much for your help on this one.  Now I just gotta figure out why it's showing an error before I submit the data but that's something I need to go back through the code for and won't ask for an answer as I won't learn anything that way.  Thank you very much for your help I really appreciate it.

 

Justin

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.