MrARM MrARM - 7 months ago 18
SQL Question

Simple Register Form in php

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.

Answer

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);
}