Jump to content

User registration problem


HardCoreMore

Recommended Posts

Hi all, this is my first post to phpfreaks.com. I am new to php programming so i am watching some video tutorial but its some old one and guy use like php 3 version and i am using php 5,2,5 vers and mysql 4.1.22. So i am trying to make user registraion page and i just can't make it. With the exact code from the video tutorial php gives me error and i don't know what to do so if somebody know it its must be people here on phpfreaks.

This is  how my registration html form looks like:

<html>

<body>

<title>Register</title>

</head>

<body>

<h1>Register</h1>

<form method="post" action="register.php">

Please enter a username:

<input name="user" type="text" maxlength="20" size="20" />

<br />

<br />

Please enter a password:

<input name="pass" type="password" maxlength="10" size="10"  />

<br />

<br />

<input type="submit" value="Register" />

</form>

</body>

</html>

 

And this is php code:

 

<?php

session_start();

?>

<?php

$user = $_POST["user"];

$pass = $_POST["pass"];

if ($user && $pass) {

$db = mysql_connect("localhost","root","chaky");

mysql_select_db("userlist",$db);

$select = mysql_query("SELECT * FROM users where name = $user");

if ($select = !$user) {

mysql_query ("insert into users set user = '$user', pass = 'password($pass)'");

if ($select) {

$logged_in_user = $user;

session_register("logged_in_user");

echo "You details have been added to the database, $user<br /><br />";

echo "<a href='main.php'>Click here to go to proceed to the main page.</a><br /><br />";

echo "<a href = 'logout.php'>Click here to logout.</a><br />";

echo $select;

exit;

}else{

echo "Sorry, there has been a technical hitch. We cannot enter your details";

exit;

}

}

}

?>

 

 

So please help what should i do what should i change to get this work.

Link to comment
https://forums.phpfreaks.com/topic/92970-user-registration-problem/
Share on other sites

There is a problem here

 

$select = mysql_query("SELECT * FROM users where name = $user");

if ($select = !$user) {

 

 

$select will not = $user here, it will be a Resource ID.  You need to use a mysql_fetch command to grab the row of info from your SELECT statement.

 

You also want to put single quotes around $user in your SELECT statement.

 

Once you get this working, you need to learn about SQL injections as well.

the entire script is pretty much flawed

 

i tried to re~write it a little bit I don't know how well it'll work:

<?php
session_start();

$user = $_POST["user"];
$pass = $_POST["pass"];
if ($user && $pass) {
$db = mysql_connect("localhost","root","chaky");
mysql_select_db("userlist",$db);
$select = mysql_query("SELECT * FROM users where name = $user");
if (!mysql_num_rows($select)) {
	$adduser = mysql_query ("insert into users set user = '$user', pass = 'password($pass)'");
	if ($adduser) {
		$logged_in_user = $user;
		session_register("logged_in_user");
		echo "You details have been added to the database, ".$user."\n\n\n";
		echo "<a href='main.php'>Click here to go to proceed to the main page.</a>\n\n\n";
		echo "<a href = 'logout.php'>Click here to logout.</a>";
		echo $select;
		exit;
	}else{
		echo "Sorry, there has been a technical hitch. We cannot enter your details";
		exit;
	}
}
}
?>

Thank you for your answers. I used code that darkangel write and i get error Warning:mysql_num_rows():supplied argument is not valid Mtsql result resource in C:\webroot\......... on line 10

Sorry, there has been a technical hitch. we cannot enter your details. But when i put single quotes around $user in $select = mysql_query line he don't give me mysql_num_rows error but just echo Sorry there has been a technical hitch...... and still don't write it to database.

When i change this line =>  $select = mysql_query("SELECT * FROM users where name = $user"); to this like darkfraks told me =>

mysql_query ("insert into users set user = '$user', pass = 'password($pass)'")or die (.mysql_error());  i get error Parse error:syntax error, unespected '.' When i remove dot before mysql_error()); It again just echo Sorry, there has been a technical hitch...

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.