Jump to content

MYSQL Error?


jbman223

Recommended Posts

$con=mysql_connect("****","****","****");
mysql_select_db("****");

$queryuser=mysql_query("SELECT * FROM users WHERE name='$name'");
$checkuser=mysql_num_rows($queryuser);
if($checkuser != 0)
{ $error = $name." is already in the database. <br /> <a href='****'>Return</a>"; 
$errortype = "1";}
else {
/* Now we will write a query to insert user details into database */
$insert = mysql_query("INSERT INTO users (name, group, sex, grouprank) VALUES ('$name', '$group', '$sex', '$grouprank')");

if($insert)
{ $error = $name. "Was successfully added to the database. <br /> <a href='****'>Return</a>"; 
$errortype = "0";}
else
{ $error = "Error in registration: ".mysql_error(); 
$errortype = "1";}

/* closing the if else statements */
}
echo $error;
?>

Error:

Error in registration: 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 'group, sex, grouprank) VALUES ('ljkh', 'lnkb', 'female', 'lkjh')' at line 1

 

Please help. THanks

Link to comment
Share on other sites

since we dont know where your inputs are coming from, simplest way to check is to do this

<?php
echo "INSERT INTO users (name, group, sex, grouprank) VALUES ('$name', '$group', '$sex', '$grouprank')";
?>

throw the query into SQL manually to see what the error is.

One possibility is that SQL doesn't like variable names.  you can try one of these two options to see if it helps (I dont know the reasoning behind it, but i read it somewhere once before :) )

 

<?php
$insert = mysql_query("INSERT INTO users (name, group, sex, grouprank) VALUES ('{$name}', '{$group}', '{$sex}', '{$grouprank}')");
$insert = mysql_query("INSERT INTO users (name, group, sex, grouprank) VALUES ('".$name."', '".$group."', '".$sex."', '".$grouprank."')");
?>

Link to comment
Share on other sites

"group" is a mysql reserved word, in order to use this keyword in your query, it needs to be wrapped in back ticks..

 

$insert = mysql_query("INSERT INTO users (name, `group`, sex, grouprank) VALUES ('$name', '$group', '$sex', '$grouprank')");

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.