-4

I'm Trying To Write a login/register system, i got the login down but i need help with registration

Could not enter data: 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 '' at line 1

My Written code

<?php
$dbhost = '';
$dbuser = '';
$dbpass = '';
$conn = mysql_connect($dbhost, $dbuser, $dbpass);
if(! $conn )
{
  die('Could not connect: ' . mysql_error());
}
$sql = 'INSERT INTO members '.
       '(id,username,password) '.
       'VALUES ( 2, test, test';

mysql_select_db('');
$retval = mysql_query( $sql, $conn );
if(! $retval )
{
  die('Could not enter data: ' . mysql_error());
}
echo "Registered\n";
mysql_close($conn);
?>

Also i need a checker to see if the username is taken and assign a id, going up from whats in my DB, my db is a table called members, inside the table is ID(Ascending number(currently at 1)) Username, password.

I don't need the password encrypted, I have something in place for that elsewhere.

John Conde
  • 217,595
  • 99
  • 455
  • 496
MrARM
  • 25
  • 7

2 Answers2

1

You have two errors in your query:

  1. You're missing quotes around your string values
  2. You're missing the closing parenthesis around your values to be inserted

Try this:

$sql = "INSERT INTO members (id,username,password) VALUES ( 2, 'test', 'test')";

FYI, this is much easier to read on one line. I would avoid concatenation in your query if you can help it.

Please, don't use mysql_* functions in new code. They are no longer maintained and are officially deprecated. See the red box? Learn about prepared statements instead, and use PDO or MySQLi - this article will help you decide which. If you choose PDO, here is a good tutorial.

Community
  • 1
  • 1
John Conde
  • 217,595
  • 99
  • 455
  • 496
1

Try This :

    // first check if username exist or not 
$rows = "SELECT * FROM members WHERE username='" . $_POST['username'] . "'";
$chk = mysql_query($rows);
if (mysql_num_rows($chk) >= 1) {
$userexist = "* This User already exist"; 
die();
// Then if not exist add it to DB 
} else {
$sql = "INSERT INTO members (id,username,password) VALUES ( 2, '" . $_POST['username'] . "', '" . $_POST['password'] . "')";
$result = mysql_query($sql,$conn);
}
Sattar
  • 108
  • 4
  • 12