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
https://forums.phpfreaks.com/topic/26894-mysql_query-trouble/
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
https://forums.phpfreaks.com/topic/26894-mysql_query-trouble/#findComment-122980
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
https://forums.phpfreaks.com/topic/26894-mysql_query-trouble/#findComment-122984
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', '[email protected].')


$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
https://forums.phpfreaks.com/topic/26894-mysql_query-trouble/#findComment-122992
Share on other sites

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.