6 Replies - 628 Views - Last Post: 14 August 2010 - 12:02 PM Rate Topic: -----

#1 maniacalsounds  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 472
  • Joined: 27-June 10

Problem registering users in MySql database

Posted 13 August 2010 - 07:21 PM

Hello Dream.in.coders! It's my next question in PHP (my second, I hope this is the last for a while, atleast). I am creating a little website where users can register for a profile and can then can login. I have the login portion working, I just can't seem to get the INSERT working. Here's my code:
<?php
	ob_start();
	$host = "localhost";
	$user = "root";
	$pass = "PASSWORDHERE";
	$dbname = "personal";
	$connect = mysql_connect($host, $user, $pass);
	if (!$connect)
		die ("Could not connect: " . mysql_error());
	mysql_select_db("$dbname");
	$age = $_POST["ageindex"];
	$username = $_POST["myusername"];
	$username = stripslashes($username);
	$username = mysql_real_escape_string($username);
	$passwordone = $_POST["pwdone"];
	$passwordtwo = $_POST["pwdtwo"];
	if ($passwordone === $passwordtwo) {
		$password = $passwordone;
		$password = stripslashes($password);
		$password = mysql_real_escape_string($password);
		$sql = "INSERT INTO `personal`.`users` (`id`, `Username`, `Password`) VALUES (NULL, '$username', '$password');";
		if (!$sql) {
			die ("Could not create user in sql database: " . mysql_error());
		} else {
			echo "You are now a member, $username .";
			header("Location:personallogin.html");
			ob_end_flush();
		}
	} else {
		die("I'm sorry. The passwords did not match.");
	}
?>


I must be missing something obvious, but please bare with me. This is my first time using INSERT. Thanks in advance, everyone! :)

This post has been edited by maniacalsounds: 13 August 2010 - 07:22 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Problem registering users in MySql database

#2 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Problem registering users in MySql database

Posted 13 August 2010 - 09:10 PM

If id is the primary key, inserting NULL into it is going to give you problems.

Also, the reason it's not inserting is because you're not actually running the query. You're writing it out, but then instead of running mysql_query() on it and making sure it ran okay, you're testing the string variable $sql that the SQL statement is contained in.
Was This Post Helpful? 1
  • +
  • -

#3 maniacalsounds  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 472
  • Joined: 27-June 10

Re: Problem registering users in MySql database

Posted 14 August 2010 - 07:21 AM

I use phpMyAdmin, and in the "id" portion of it, it auto-increments. What would I put there besides NULL? Because it worked fine with keeping it.
Was This Post Helpful? 0
  • +
  • -

#4 ShaneK  Icon User is offline

  • require_once("brain.php"); //Fatal error :/
  • member icon

Reputation: 240
  • View blog
  • Posts: 1,224
  • Joined: 10-May 09

Re: Problem registering users in MySql database

Posted 14 August 2010 - 07:31 AM

You should leave it as NULL if it auto-incs. I think Valek was assuming it does not.

Yours,
Shane~
Was This Post Helpful? 0
  • +
  • -

#5 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2982
  • View blog
  • Posts: 10,299
  • Joined: 08-August 08

Re: Problem registering users in MySql database

Posted 14 August 2010 - 07:44 AM

If it auto increments you shouldn't insert anything into it:
		$sql = "INSERT INTO `personal`.`users` (`Username`, `Password`) VALUES ( '$username', '$password');";


Of course as Valek points out, you do need to execute the query.
$result = mysql_query($sql);
if(!$result)
{
	error_log("Query failed: " . mysql_error());
	die("There was an error with the database.  It has been logged.");
}


Was This Post Helpful? 1
  • +
  • -

#6 Valek  Icon User is offline

  • The Real Skynet
  • member icon

Reputation: 542
  • View blog
  • Posts: 1,713
  • Joined: 08-November 08

Re: Problem registering users in MySql database

Posted 14 August 2010 - 10:01 AM

I was actually assuming as CT said: You wouldn't insert anything into id. You'd let MySQL handle that on its own.
Was This Post Helpful? 0
  • +
  • -

#7 maniacalsounds  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 117
  • View blog
  • Posts: 472
  • Joined: 27-June 10

Re: Problem registering users in MySql database

Posted 14 August 2010 - 12:02 PM

Okay thanks for the help, I left it blank now. It auto-increments.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1