Jump to content

mysql_query() trouble


lilman

Recommended Posts

I have been at this for hours, and I am stumped and feeling dumb. I for the life of me cannot figure out why query will not execute.  I know it has nothing to do with the database connection, seeing that I've tested it. I have even gone as far as trying to insert default values instead of calling variables. I have tried both queries, and both return false. I know the variables contain something as it prints all the values if the query doesn't execute. I have pasted the code below, if anyone could help me out I would really appreciate it. Thanks

/*
if(mysql_query("INSERT INTO users(group, user_name, password, first_name, last_name, gender, email_address) VALUES(".$group.", ".$user_name.", ".$password.", ".$first_name.", ".$last_name.", ".$gender.", ".$email_address.")"))
*/

if(mysql_query("INSERT INTO usesrs (group, user_name, password, first_name, last_name, gender, email_address) VALUES ('2', 'user', 'pass', 'isaac', 'meals', 'male', 'email')"))
echo "successful in registering you an account";
else
{
echo "couldn't submit";
echo "<br />";
echo $group;
echo "<br />";
echo $first_name;
echo "<br />";
echo $last_name;
echo "<br />";
echo $user_name;
echo "<br />";
echo $email_address;
echo "<br />";
echo $password;
echo "<br />";
echo $gender;
}
Link to comment
Share on other sites

[code]
$sql = "INSERT INTO usesrs (group, user_name, password, first_name, last_name, gender, email_address) VALUES ('2', 'user', 'pass', 'isaac', 'meals', 'male', 'email')";
$result = mysql_query($sql);
if($result){
      echo "successful in registering you an account";
}
else
{
      echo "couldn't submit";
      echo "
";
      echo $group;
      echo "
";
      echo $first_name;
      echo "
";
      echo $last_name;
      echo "
";
      echo $user_name;
      echo "
";
      echo $email_address;
      echo "
";
      echo $password;
      echo "
";
      echo $gender;
}
[/code]
Link to comment
Share on other sites

Is the table name users or use[b]s[/b]rs?

You can get much better error trapping to help you resolve your problem.  Change this:
[code]if(mysql_query("INSERT INTO usesrs (group, user_name, password, first_name, last_name, gender, email_address) VALUES ('2', 'user', 'pass', 'isaac', 'meals', 'male', 'email')"))[/code]

to this:
[code]$query = "INSERT INTO usesrs (`group`, user_name, password, first_name, last_name, gender, email_address) VALUES ('2', 'user', 'pass', 'isaac', 'meals', 'male', 'email')";
$result = mysql_query($query) or die("Error ". mysql_error(). " with query ". $query); // useful error message
if ($result) {
.... etc[/code]

Notice the backticks around group.  Group is a MySQL reserved word and you should avoid using those as table or field names.  Using backticks is a work-around.

Full list of reserved words - http://www.htmlite.com/mysql002a.php
Link to comment
Share on other sites

Thanks for the replies so far.  I've done what has been suggested, changed the column groud to class and edited my code. Below is both the code and the error message I get when I run it.

Line 22 would be the one with $result.

Notice: Use of undefined constant mysql_error - assumed 'mysql_error' in /registering.php on line 22
Error mysql_error with query INSERT INTO usesrs (class, user_name, password, first_name, last_name, gender, email_address) VALUES ('2', 'lilman', 'ed9961646291a608b6c586c38a28cac45bf97c15', 'isaac', 'meals', 'Male', 'lilman87@password.com.')


$query = "INSERT INTO usesrs (class, user_name, password, first_name, last_name, gender, email_address) VALUES ('$group', '$user_name', '$password', '$first_name', '$last_name', '$gender', '$email_address')";
$result = mysql_query($query) or die("Error ". mysql_error. " with query ". $query);;
if($result)
echo "successful in registering you an account";
else
{
                echo "failed";
      }
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.