Jump to content

[PHP-MySQL] User Registration with drop down - help?


Recommended Posts

Hi! I'm new with php scripts and need some help here.

 

I have got a simple online chat which taken elsewhere (don't remember now) and want it to add additional feature..

 

I have copied somewhere here a "User Registration Scripts" and it work withuot any problem, but I have to add feature like dropdown selection.

 

As you can see in the php script I have commented the area that I needed to incorporate (dropdown selection). But I have tried so many things with my little knowledge without success.

 

Please help.

 

The database Tables:

-- ----------------------------

-- Table structure for room

-- ----------------------------

CREATE TABLE `room` (

  `rid` int(11) NOT NULL auto_increment,

  `name` varchar(20) default NULL,

  `descript` varchar(255) default NULL,

  `typ` varchar(1) default NULL,

  `adminid` int(11) default NULL,

  PRIMARY KEY  (`rid`)

) ENGINE=MyISAM AUTO_INCREMENT=6 DEFAULT CHARSET=utf8;

 

-- ----------------------------

-- Table structure for user

-- ----------------------------

CREATE TABLE `user` (

  `uid` int(11) NOT NULL auto_increment,

  `name` varchar(20) default NULL,

  `last` timestamp NOT NULL default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,

  `rid` int(11) default NULL,

  `pass` varchar(64) default NULL,

  PRIMARY KEY  (`uid`),

  UNIQUE KEY `uid` (`uid`,`name`)

) ENGINE=MyISAM AUTO_INCREMENT=16 DEFAULT CHARSET=utf8;

 

The Registration Script:

<?php 
if (isset($_POST['submit'])) {
if($_POST['pass'] == $_POST['confirmpass']) {
	$pass   = 'Ok';
	$dbhost = "localhost";
	$dbuser = "***";
	$dbpass = "******";
	$dbname = "chat";
	$connect = mysql_connect($dbhost,$dbuser,$dbpass);
	if (!$connect) {
		die('MySQL Error ' . mysql_error()); 
	} else {
	$selectdb =	mysql_select_db($dbname);
	if (!$selectdb){
		die('MySQL Error ' . mysql_error());
	} else {
	$checkuser = mysql_query("SELECT * FROM user WHERE name = '".$_POST[name]."' ");
	if (!$checkuser) {
		die('MySQL Error' . mysql_error());
	} else {
	if(mysql_num_rows($checkuser) > 0) {
		$error = '<font color="red"> User Already Exists </font>';
	} else {
	$name = $_POST['name'];
	$pass = $_POST['pass'];
	$rid = $_POST1['roomid'];
	$adduser = mysql_query("INSERT INTO user (name, pass) VALUES ('$name', '$rid', PASSWORD('$pass'))");
	if (!$adduser) {
		die('MySQL Error ' . mysql_error());
	} else {
	$error = '<font color="green">User Added Succesfully </font>';
	}		
	}		
	}
}
}
} else {
$error = '<font color="red"> Passwords Do Not Match </font>';		
}
}
/*
$query=("select * from room order by name, name desc");
$result=mysql_query($query) or die ("Unable to Make the Query:" . mysql_error() ); 
echo "<form action=select method=POST1>"; 
echo "<select name=roomid>"; 
while($row=mysql_fetch_array($result)) { 
echo "<OPTION VALUE=".$row['rid'].">".$row['name']."</OPTION><br>"; }
echo "</select>"; 
echo "</form>"; 
*/
?>
<html>
<head>
<title> Registration </title>
<link rel="stylesheet" href="styles.css" type="text/css">
</head>
<body>
<center>
<table width="300px">
	<form action="<?php echo $PHP_SELF; ?>" method="POST">
	<tr><td>Username:</td><td><input type="text" name="name"></td></tr>
	<tr><td>Password:</td><td><input type="password" name="pass"></td></tr>
	<tr><td>Confirm Password:</td><td><input type="password" name="confirmpass"></td></tr>
	<tr><td></td><td><input type="submit" name="submit" value="Register"></td></tr>
	</form>	
</table>
<p><?php echo $error; ?></p>
</center>
</body>
</html>

 

Edit: While a user is trying to register a drop down menu should be available for a user to select which room he/she is trying to register, so once the register button is press values should be availble to insert to user table. The dropdown menu should throw a value of 'rid' column from room table that can be added to user table.

THANKS for no response.

 

But I finally made it after playing with it (for almost a day with such a few hours of sleep) by breaking the codes into three to minimized and concentrate only with the main code... blah blah blah.

 

Then finally works, the only thing is the code is quite ugly maybe you wiz guys can simplify it.

 

This is the working code I just crack down.

 

<?php

include ('config.php');
include ('connect.php');

?>

<?php

$sql="SELECT rid, name FROM room"; 
$result=mysql_query($sql); 
$options=""; 
while ($row=mysql_fetch_assoc($result)) { 
    $rid=$row["rid"]; 
    $rname=$row["name"]; 
    $options.="<OPTION VALUE=\"$rid\">".$rname.'</option>'; 
	} 

?>

<?php 
if (isset($_POST['Submit'])) {
if($_POST['pass'] == $_POST['confirmpass']) {
	$pass   = 'Ok';
	$checkuser = mysql_query("SELECT * FROM user WHERE name = '".$_POST[name]."' ");
	if (!$checkuser) {
		die('MySQL Error' . mysql_error());
	} else {
	if(mysql_num_rows($checkuser) > 0) {
		$error = '<font color="red"> User Already Exists </font>';
	} else {
	$name = $_POST['name'];
	$pass = $_POST['pass'];
	$options = $_POST['rid'];
	$adduser = mysql_query("INSERT INTO user (name, rid, pass) VALUES ('$name', '$options', PASSWORD('$pass'))");
	if (!$adduser) {
		die('MySQL Error ' . mysql_error());
	} else {
	$error = '<font color="green">User Added Succesfully </font>';
	}		
	}		
	}
} else {
$error = '<font color="red"> Passwords Do Not Match </font>';		
}
}

?>

<html>
<head>
<title> Registration </title>
<link rel="stylesheet" href="styles.css" type="text/css">
</head>
<body>
	<table width="300px">
	<form action="<?php echo $PHP_SELF; ?>" method="POST">
	<tr><td>Username:</td><td><input type="text" name="name"></td></tr>
	<tr><td>Password:</td><td><input type="password" name="pass"></td></tr>
	<tr><td>Confirm Password:</td><td><input type="password" name="confirmpass"></td></tr>
<tr><td>
	<SELECT NAME=rid> 
	<OPTION VALUE=0>Choose 
	<?echo $options?> 
	</SELECT> 
</td></tr>		
	<tr><td></td><td><input type="Submit" name="Submit" value="Register"></td></tr>
	</form>	
</table>
<p><?php echo $error; ?></p>

</body>
</html>

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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