jbman223 Posted January 28, 2012 Share Posted January 28, 2012 $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 https://forums.phpfreaks.com/topic/255941-mysql-error/ Share on other sites More sharing options...
lonewolf217 Posted January 28, 2012 Share Posted January 28, 2012 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 https://forums.phpfreaks.com/topic/255941-mysql-error/#findComment-1311995 Share on other sites More sharing options...
AyKay47 Posted January 28, 2012 Share Posted January 28, 2012 "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 https://forums.phpfreaks.com/topic/255941-mysql-error/#findComment-1311996 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.