Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New to the CF scene
    Join Date
    Apr 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Unhappy Register/Login Scripts in PHP and MYSQL Problem

    I've currently got some register and login scripts, scripted in PHP, and I need to setup a database. I've got an SQL file, and i'm using phpMyAdmin for the database, and the database name is 107887.

    This is the PHP script for registering i'm using:

    Code:
    <?
    include 'config.php';
    
    if(isset($_POST['submit']))
    {
    	$first		= addslashes(trim($_POST['firstname']));
    	$surname	= addslashes(trim($_POST['surname']));
    	$username	= addslashes(trim($_POST['username']));
    	$email		= addslashes(trim($_POST['email']));
    	$pass		= addslashes(trim($_POST['password']));
    	$conf		= addslashes(trim($_POST['confirm']));
    	$activ		= addslashes(trim($_POST['activation']));
    	$date = date("d/m/y");
    	
    	$errormsg='Errors:\n';
    	# pop up window if password is not equal to confirmed password
    	if ( $_POST['password'] == $_POST['confirm'] )
    	{$error1=0;}else{
    		$error1=1;
    		$errormsg=$errormsg.'Passwords were not the same\n';
    		#echo '<script>alert("Your passwords were not the same, please enter the same password in each field.");</script>';
    		#echo '<script>history.back(1);</script>';
    		#exit;
    	}
    	
    	# encode the password
    	$password = md5($pass);	
    	
    	# pop up window if any field was left empty
    	if ((((( empty($first) ) || ( empty($surname) ) || ( empty($username) ) || ( empty($email) ) || ( empty($password) )))))
    	{
    		$error2=1;
    		$errormsg=$errormsg.'One or more fields were left empty\n';
    		#echo '<script>alert("One or more fields was left empty, please try again.");</script>';
    		#echo '<script>history.back(1);</script>';
    		#exit;
    	}else{$error2=0;}
    	
    	# pop up window if an invalid email address was entered
    	if((!strstr($email , "@")) || (!strstr($email , ".")))
    	{
    		$error3=1;
    		$errormsg=$errormsg.'Invalid email address\n';
    		#echo '<script>alert("You entered an invalid email address. Please try again.");</script>';
    		#echo '<script>history.back(1);</script>';
    		#exit;
    	}else{$error3=0;}
    	
    	# check if username is already in use in the database and pop up if there is
    	$q = mysql_query("SELECT * FROM Users WHERE Username = '$username'") or die(mysql_error());
    	if(mysql_num_rows($q) > 0)
    	{
    		$error4=1;
    		$errormsg=$errormsg.'Username already in use\n';
    		#echo '<script>alert("The username you entered is already in use, please try again.");</script>';
    		#echo '<script>history.back(1);</script>';
    		#exit;		
    	}else{$error4=0;}
    	
    	# check if activation code is correct
    	$q1 = mysql_query("SELECT code FROM codes WHERE id = 1") or die(mysql_error());
    	$q2 = mysql_query("SELECT code FROM codes WHERE id = 2") or die(mysql_error());
    	
    	$row1 = mysql_fetch_array($q1);
    	$row2 = mysql_fetch_array($q2);
    	
    	if ( $_POST['activation'] == $row1['code'] ){
    		$code = 0;
    		$error5=0;
    	}elseif ( $_POST['activation'] == $row2['code'] ){
    		$code = 9;
    		$error5=0;
    	}else
    	{
    		$error5=1;
    		$errormsg=$errormsg.'Activation code incorrect\n';
    		#echo '<script>alert("Your activation code was incorrect, please enter the correct code to register.");</script>';
    		#echo '<script>history.back(1);</script>';
    		#exit;
    	}
    	
    	if ($error1 || $error2 || $error3 || $error4 || $error5){
    		echo '<script>alert("'.$errormsg.'");</script>';
    		echo '<script>history.back(1);</script>';
    		exit;
    	}
    	
    	$name = $first . ' ' . $surname;
    		
    	$query = mysql_query("INSERT INTO Users (Username, Password, Name, Email, Date, Level) VALUES ('$username','$password','$name','$email','$date','$code')") or die(mysql_error());	
    	
    	if($query)
    	{
    		echo '	<html>
    				<head>
    				<title>Success</title>
    				<link href="style.css" rel="stylesheet" type="text/css">
    				</head>			
    				<body>
    				<div id="success">
    				<p>Success! You are now a registered member</p>
    				<p>Click <a href="login.php">here</a> to login</p>
    				</div>
    				</body>
    				</html>
    				';					
    	} else {
    		echo '
    				<html>
    				<head>
    				<title>Error</title>
    				<link href="style.css" rel="stylesheet" type="text/css">
    				</head>	
    				<body>				
    				<div id="error">
    				<p>We are sorry, there appears to be a problem with our script at the moment.</p>
    				<p>Your data was not lost. Username: '.$username.' | Password: '.$pass.' | Email: '.$email.' | Full name: '.$name.'</p>
    				<p>Please try again by clicking <a href="login.php">here</a>.</p>
    				</div>
    				</body>
    				</html>
    				';	
    	}
    } else {
    ?>
    <html>
    <head>
    <title>Register</title>
    <link href="style.css" rel="stylesheet" type="text/css">
    </head>
    <body>
    <div id="wrapper">
    <div id="head">the registration page</div>
    <br>
    <div id="main"> 
      <p>Welcome to the registration, fill out the form below and hit Submit. All fields are required,so fill them all out! </p>
      <form action="<?= $_SERVER['PHP_SELF'] ?>" method="post">
    	  <table width="100%"  border="0" cellpadding="0" cellspacing="0">
    		<tr class="firstRow">
    		  <td width="50%">First name </td>
    		  <td width="50%"><input name="firstname" type="text" class="textBox" id="firstname"></td>
    		</tr>
    		<tr class="secondRow">
    		  <td>Surname</td>
    		  <td><input name="surname" type="text" class="textBox" id="surname"></td>
    		</tr>
    		<tr class="firstRow">
    		  <td>Email Address </td>
    		  <td><input name="email" type="text" class="textBox" id="email"></td>
    		</tr>
    		<tr class="secondRow">
    		  <td>Username</td>
    		  <td><input name="username" type="text" class="textBox" id="username"></td>
    	    </tr>
    		<tr class="firstRow">
    		  <td>Password</td>
    		  <td><input name="password" type="password" class="textBox" id="password"></td>
    		</tr>
    		<tr class="secondRow">
    		  <td>Confirm Password </td>
    		  <td><input name="confirm" type="password" class="textBox" id="confirm"></td>
    		</tr>
    		<tr class="firstRow">
    		  <td>User Activation Code *</td>
    		  <td><input name="activation" type="password" class="textBox" id="activation"></td>
    		</tr>
    		<tr class="secondRow">
    		  <td>Register</td>
    		  <td><input name="submit" type="submit" class="textBox" value="Submit"></td>
    		</tr>
    		<tr class="firstRow">
    		  <td>* Activation codes assessed by your administrator</td>
    		  <td>If you are already registered click <a href="login.php">here</a> to login</td>
    		</tr>
    	  </table>
      </form>
    </div>
    </div>	
    </body>
    </html>
    <?
    }
    ?>
    And this is my SQL file, which i'm trying to import into phpMyAdmin:

    Code:
    mysql_select_db('107887');
    
    CREATE TABLE `songs` (
      `id` int(10) NOT NULL auto_increment,
      `artist` varchar(255) NOT NULL,
      `title` varchar(255) NOT NULL,
      `size` varchar(255) NOT NULL,
      `URL` varchar(255) NOT NULL,
      PRIMARY KEY  (`id`)
    );
    
    
    CREATE TABLE `videos` (
      `id` int(10) NOT NULL auto_increment,
      `picture` varchar(255) NOT NULL,
      `artist` varchar(255) NOT NULL,
      `title` varchar(255) NOT NULL,
      `size` varchar(255) NOT NULL,
      `URL` varchar(255) NOT NULL,
      PRIMARY KEY  (`id`)
    );
    
    
    CREATE TABLE `ringtones` (
      `id` int(10) NOT NULL auto_increment,
      `artist` varchar(255) NOT NULL,
      `title` varchar(255) NOT NULL,
      `size` varchar(255) NOT NULL,
      `URL` varchar(255) NOT NULL,
      PRIMARY KEY  (`id`)
    );
    
    
    CREATE TABLE `backingmusic` (
      `id` int(10) NOT NULL auto_increment,
      `artist` varchar(255) NOT NULL,
      `title` varchar(255) NOT NULL,
      `size` varchar(255) NOT NULL,
      `midURL` varchar(255) NOT NULL,
      `docURL` varchar(255) NOT NULL,
      PRIMARY KEY  (`id`)
    );
    
    
    CREATE TABLE `projects` (
      `id` int(10) NOT NULL auto_increment,
      `picture` varchar(255) NOT NULL,
      `language` varchar(255) NOT NULL,
      `title` varchar(255) NOT NULL,
      `size` varchar(255) NOT NULL,
      `URL` varchar(255) NOT NULL,
      PRIMARY KEY  (`id`)
    );
    
    
    CREATE TABLE `pictures` (
      `id` int(10) NOT NULL auto_increment,
      `pic` varchar(255) NOT NULL,
      PRIMARY KEY  (`id`)
    );
    
    
    CREATE TABLE `programs` (
      `id` int(10) NOT NULL auto_increment,
      `picture` varchar(255) NOT NULL,
      `title` varchar(255) NOT NULL,
      `size` varchar(255) NOT NULL,
      `URL` varchar(255) NOT NULL,
      PRIMARY KEY  (`id`)
    );
    
    
    CREATE TABLE `codes` (
      `id` int(10) NOT NULL auto_increment,
      `code` varchar(255) NOT NULL,
      PRIMARY KEY  (`id`)
    );
    
    CREATE TABLE `Users` (
      `id` int(10) NOT NULL auto_increment,
      `Username` varchar(255) NOT NULL,
      `Password` varchar(255) NOT NULL,
      `Name` varchar(255) NOT NULL,
      `Email` varchar(255) NOT NULL,
      `Date` varchar(255) NOT NULL,
      `Level` varchar(255) NOT NULL,
      PRIMARY KEY  (`id`)
    );
    
    
    CREATE TABLE `animation` (
      `id` int(10) NOT NULL auto_increment,
      `title` varchar(255) NOT NULL,
      `episodes` varchar(255) NOT NULL,
      `language` varchar(255) NOT NULL,
      `price` varchar(255) NOT NULL,
      `numcds` int(10) NOT NULL,
      PRIMARY KEY  (`id`)
    );
    And when I try to import the above SQL file into phpMyAdmin, I get the error below:

    Code:
    Error
    SQL query:  
    
    mysql_select_db(
    
    '107887'
    );
    
    
    
    MySQL said:  
    
    #1064 - 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 'mysql_select_db('107887')' at line 1
    What's wrong with the SQL file?

    All help is appreciated, thanks!

  • #2
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,266
    Thanks
    6
    Thanked 48 Times in 48 Posts
    You don't need mysql_select_db() if you're importing to phpmyadmin

  • #3
    New to the CF scene
    Join Date
    Apr 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Without that, I get this error:

    Code:
    Error
    SQL query: 
    
    CREATE TABLE `songs` (
    
    `id` int( 10 ) NOT NULL AUTO_INCREMENT ,
    `artist` varchar( 255 ) NOT NULL ,
    `title` varchar( 255 ) NOT NULL ,
    `size` varchar( 255 ) NOT NULL ,
    `URL` varchar( 255 ) NOT NULL ,
    PRIMARY KEY ( `id` ) 
    );
    
    
    
    MySQL said:  
    
    #1046 - No Database Selected

  • #4
    Senior Coder Nightfire's Avatar
    Join Date
    Jun 2002
    Posts
    4,266
    Thanks
    6
    Thanked 48 Times in 48 Posts
    Are you selecting the database from the drop down menu on the left of the screen before importing?

  • #5
    New to the CF scene
    Join Date
    Apr 2007
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Smile

    Man i'm an idiot, I wasn't selecting it from the drop down menu! Oh well, thanks for the help!

  • #6
    Super Moderator guelphdad's Avatar
    Join Date
    Mar 2006
    Location
    St. Catharines, Ontario Canada
    Posts
    2,631
    Thanks
    4
    Thanked 147 Times in 138 Posts
    In the SQL file itself change:
    mysql_select_db('107887');

    to

    USE `107887`

    it then doesn't matter which database you had selected, it will use this one you have specified.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •