Dynadot โ€” .com Registration $8.99

MySQL INSERT INTO query

Spaceship Spaceship
Watch

SiKing

Registered MemberEstablished Member
Impact
6
Hello, I'm trying to run the following mysql query. Basically, the user fills out a registration form which then gets forwarded to this script. However, the data isn't being stored.

PHP:
$name=$_POST['name'];
$email=$_POST['email'];
$username=$_POST['username'];
$password=$_POST['password'];
$confirm=$_POST['confirm'];



mysql_connect(localhost,$db_username,$db_password);
@mysql_select_db($database) or die( "Unable to select database");

if ( $_POST['password'] == $_POST['confirm'] )
{}else{

echo '<script>alert("Your passwords were not the same, please enter the same password in each field.");</script>';
echo '<script>history.back(1);</script>';
exit;

} 

if (((( empty($name) ) || ( empty($username) ) || ( empty($email) ) || ( empty($password) ))))
{

echo '<script>alert("One or more fields were left empty, please try again.");</script>';
echo '<script>history.back(1);</script>';
exit;

}

if((!strstr($email , "@")) || (!strstr($email , ".")))
{

echo '<script>alert("You entered an invalid email address. Please try again.");</script>';
echo '<script>history.back(1);</script>';
exit;

}






$q = mysql_query("SELECT * FROM users WHERE email = '$email'") or die(mysql_error());
if(mysql_num_rows($q) > 0)
{

echo '<script>alert("The email you entered is already in use, please try again.");</script>';
echo '<script>history.back(1);</script>';
exit;

} 

$e = mysql_query("SELECT * FROM users WHERE username = '$username'") or die(mysql_error());
if(mysql_num_rows($e) > 0)
{

echo '<script>alert("The username you entered is already in use, please try again.");</script>';
echo '<script>history.back(1);</script>';
exit;

}



$query = "INSERT INTO users VALUES ('$name,'$email','$username','$password')";
mysql_query($query);





$i=0;
while ($i < $num) {


$name=mysql_result($result,$i,"name");
$email=mysql_result($result,$i,"email");
$username=mysql_result($result,$i,"username");
$password=mysql_result($result,$i,"password");



echo "<b>$name</b><br>Email: $email<br>Username: $username<br>Password: $password<br><hr><br>";

$i++;
}



$yoursite = 'The Flavell Studio';
$sitename = 'The Flavell Studio';
$webmaster = 'Simon King';
$youremail = '[email protected]';

$subject = "You have successfully registered at $sitename...";
$message = "Dear $name,
 
You are now registered at our web site. To login, simply go to the website and enter in the following details in the login form:

.........................................

Username: $username
Password: $password

..........................................
   
Please remember these details for future use.
   
Thanks,

$webmaster
$yoursite";
   
mail($email, $subject, $message, "From: $yoursite <$youremail>\nX-Mailer:PHP/" . phpversion());
include("head.php");
echo "<h1>Congratulations</h1><p>Your information has been stored in our database and mailed to your email address.</p>";
include("foot.php");

mysql_close();


Can someone please help?
 
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
AfternicAfternic
could we also see the form that you are using
 
0
•••
Make the query:
"INSERT INTO users (`name`, `email`, `username`, `password`) VALUES ('$name,'$email','$username','$password')"

Changing the titles to whatever you column names are.
 
0
•••
Or for specific reasons why it's not inserting, you can do more detailed error handling:

Code:
$query = "INSERT INTO users VALUES ('$name,'$email','$username','$password')";
mysql_query($query) or die ("Error : " . mysql_error());

But your problem has probably already been solved witht he fixed insert statement. You can only use the barebones "insert into users values" if your table structure fits your values exactly.
 
0
•••
Sounds to me like your DB is in a bit of a mess?

INSERT INTO users VALUES ('$name,'$email','$username','$password')

The current script means that $name is going into the 1st field in the table, isn't that the 'ID' column? Normally the ID is put at the beginning of the table.

More than likely, the reason it's not working is you're trying to insert a string into an INT.

You can try:
Code:
$query = "INSERT INTO users VALUES ('', '$name, '$email', '$username', '$password')";
mysql_query($query) or die (mysql_error());

It'll help a lot if you then paste the mysql error here.
 
0
•••
The problem is the missing quote in $names.

This:

PHP:
$query = "INSERT INTO users VALUES ('$name,'$email','$username','$password')";
mysql_query($query);

Should be this:

PHP:
$query = "INSERT INTO users VALUES ('$name','$email','$username','$password')";
mysql_query($query);

WHat I always do is print the error if one exists like this:

PHP:
$query = "INSERT INTO users VALUES ('$name','$email','$username','$password')";

mysql_query($query) or die("Database Error: " . mysql_error());

Cheers!

-Bob
 
0
•••
Ah, yes, well seen Bob, I can't believe I didn't notice that. Just shows how easy silly things like that are to miss.
 
0
•••
mituozo said:
Ah, yes, well seen Bob, I can't believe I didn't notice that. Just shows how easy silly things like that are to miss.

When my SQL statements do not work,this is the FIRST thing I look for. Been there, done that. . . STILL make that error :lol:

Happy coding.

-Bob
 
0
•••
moondog said:
When my SQL statements do not work,this is the FIRST thing I look for. Been there, done that. . . STILL make that error :lol:

Happy coding.

-Bob
Quote and semi-colons

I've done it a thousand times...

-Steve
 
0
•••
Thank you so much people!
 
0
•••
mituozo said:
INSERT INTO users VALUES ('$name,'$email','$username','$password')

The current script means that $name is going into the 1st field in the table, isn't that the 'ID' column? Normally the ID is put at the beginning of the table.

More than likely, the reason it's not working is you're trying to insert a string into an INT.

Although that is ussually the case it doesnt have to be that way.
 
0
•••
and just for a side tip, you should specify the columns in your insert statement. That way if you ever change your table layout, or add columns, you don't have to go back and change the code. just a maintainability tip for ya.

Mike
 
0
•••
Appraise.net
Unstoppable Domains
Domain Recover
DomainEasy โ€” Live Options
  • The sidebar remains visible by scrolling at a speed relative to the pageโ€™s height.
Back