1,105,254 Community Members

Unable to insert data into MySQL table

Member Avatar
ruchir7
Newbie Poster
2 posts since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

This is my first post on Daniweb.

I am developing a social networking site and the register form isn't quite working as expected.


Here is the PHP code :

<? $title = 'Register' ?>
<?require ('scripts/top.php'); ?>

<link rel='stylesheet' type='text/css' href='scripts/form.css'/>

<div id='content'> 

<h1> Register an account </h1>

<div id='form'>


<?

//Main form
$form = "

<div id ='message'>Fields marked with <font color='#A61616'>*</font> are required</div>

<form action='register.php' method='POST'>

	<table cellspacing='8px'>
	
		<tr>
			<td>
				First name <font color='#A61616'>*</font>
			</td>
			<td>
				<input type='text' name='firstname' class='textbox' size='35'/>
			</td>
		</tr>
		
		<tr>
			<td>
				Last name <font color='#A61616'>*</font>
			</td>
			<td>
				<input type='text' name='lastname'class='textbox' size='35'/>
			</td>
		</tr>
		
		<tr>
			<td>
				Username <font color='#A61616'>*</font>
			</td>
			<td>
				<input type='text' name='username'class='textbox' size='35'/>
			</td>
		</tr>
		
		<tr>
			<td>
				Gender <font color='#A61616'>*</font>
			</td>
			<td>
				<input type='radio' name='gender' value='Male' class='radio'/> Male
				<input type='radio' name='gender' value='Female'class='radio'/> Female
			</td>
		</tr>
		
		<tr>
			<td>
				Email <font color='#A61616'>*</font>
			</td>
			<td>
				<input type='text' name='email'class='textbox' size='35'/>
			</td>
		</tr>
		
		<tr>
			<td>
				Password <font color='#A61616'>*</font>
			</td>
			<td>
				<input type='password' name='pass'class='textbox' size='35'/>
			</td>
		</tr>
		
		<tr>
			<td>
				Confirm password <font color='#A61616'>*</font>
			</td>
			<td>
				<input type='password' name='repass'class='textbox' size='35'/>
			</td>
		</tr>
		
		<tr>
			<td>
				Relationship status
			</td>
			<td>
				<select name='relation' class='list'>
				<option>Select your relationship status</option>
				<option value=\"Single\">Single</option>
				<option value=\"In a relationship\">In a relationship</option>
				<option value=\"Married\">Married</option>
				</select>
			</td>
		</tr>
		
		<tr>
			<td>
				Here for
			</td>
			<td>
				<select name='interest' class='list'>
				<option>Select what you are interested in</option>
				<option value=\"Making friends\">Making friends</option>
				<option value=\"Networking\">Networking</option>
				<option value=\"Fun\">Fun</option>
				<option value=\"Other\">Other</option>
				</select>
			</td>
		</tr>
		
		<tr>
			<td>
				Display Picture
			</td>
			<td>
				<input type='file' name='dp'/>
			</td>
		</tr>
		
		<tr>
			<td>
				About
			</td>
			<td>
				<textarea name='about' rows='5' cols='25' class='textbox'></textarea>
			</td>
		</tr>
		
		<tr>
			<td>
				Hobbies
			</td>
			<td>
				<textarea name='hobbies' rows='3' cols='25' class='textbox'></textarea>
			</td>
		</tr>
		
		<tr>
			<td>
			</td>
			<td>
				<input type='submit' name='submitbtn' value='Register' class='button'/>
			</td>
		</tr>
		
	</table>
	
</form>
";

// Checking for entered values and assigning variables

if (isset($_POST['submitbtn'])) {

$firstname = strip_tags($_POST['firstname']);
$lastname = strip_tags($_POST['lastname']);
$username = strip_tags($_POST['username']);

if ($_POST['gender']){
$gender = strip_tags($_POST['gender']);
}
else {$gender = 'Not specified';}

$email = strip_tags($_POST['email']);
$pass = strip_tags($_POST['pass']);
$repass = strip_tags($_POST['repass']);

if ($relation){
$relation = strip_tags($_POST['relation']);
} else {$relation = '&nbsp;';}

if ($interest){
$interest = strip_tags($_POST['interest']);
} else {$interest = '&nbsp;';}

if ($about){
$about = strip_tags($_POST['about']);
} else {$about = '&nbsp;';}

if ($hobbies) {
$hobbies = strip_tags($_POST['hobbies']);
} else {$hobbies = '&nbsp;';}

if ($_POST['dp']) {

$name = $_FILES['dp']['name'];
$type = $_FILES['dp']['type'];
$size = $_FILES['dp']['size'];
$tmpname = $_FILES['dp']['tmpname'];
$ext = substr($name, strpos($name,'.'));
} 


// The real registration process begins

// Checking to see if all required fields have been entered

	if ($firstname && $lastname && $username && $gender && $email && $pass && $repass) {

// Checking if both entered passwords match

			if ($pass == $repass){

			
// Checking if the email address entered is valid
			
				if (strstr($email, '@') && strstr($email, '.') && strlen($email)>=6) {
				
// Connecting to database by requiring 'connect' file
								
				require ("scripts/connect.php");
				
				
				
// Checking if username already exists
								
				$query = mysql_query("SELECT * FROM members WHERE username='$username'");
				$num_rows = mysql_num_rows ($query);
				
						if ($numrows == 0) {
			

			
// Checking if e-mail address already exists
						
								$query = mysql_query("SELECT * FROM members WHERE email='$email'");
								$num_rows = mysql_num_rows ($query);
								
										if ($numrows == 0) {
										
										
// Encrypting password					
										$password = md5(md5($pass));
										$date = date("F d, Y");
								
// Uploading avatar								
											if ($name) {
											move_uploaded_file($tmpname, "avatars/$username.$ext");
											$dp = "$username.$ext";
											}
											
											else{
											
											if ($gender == "Male") {$dp = "images/defaultavatars/male.png";}
											else
											if ($gender == "Female") {$dp = "images/defaultavatars/female.png";}
											
											}

// Generating random code
											
											$code = substr(md5(mt_rand(111111111,9999999999)), 2, 20);
											


// Inserting values
											
											"INSERT INTO `members` ('id','first_name','last_name','username','email','password,'gender','about','hobbies','looking_for','relationship_status','dp','last_visit','active','code','blocked','date','showemail','friends')
														 VALUES('','$firstname','$lastname','$username','$email','$password,'$gender','$about','$hobbies','$interest','$relation','$dp','','0','','0','$date','0','')";
											
																					
											
// Sending email for activation
											
											$webmaster = "My email goes here";
											$subject = "Activate your account on My sitename";
											$headers = "From: My sitename<$webmaster>";
											$message = "Hey $firstname! Thank you for registering on My sitename. Please follow the link below to activate your account: 
														http://mysite/activate.php?code=$code If the above link does not work, go to http://mysite/activate.php and type in this code: $code ";
																						
											
											mail($email, $subject, $message, $headers);
											

// Giving success message											
											
											echo "<div id = 'success'> <b>Thanks for registering!</b> <br/> You will receive an email with a link to activate your account.<br/> Your email address is $email <br/> <br/> <i>No email? Check your <b>spam</b> folder. </i> <br/> Still having problems? <a href='contact.php'> Contact us </div>";
											
											
											
											
											
											}
										
								
								else {echo "<div id='error'> That e-mail address is already taken </div>";
									  echo $form;}
						
				
				}
				
				else {echo "<div id='error'> That username is already taken </div>";
					  echo $form;}
				
				
				
				
// Error on incorrect email
				
				}
				
				else {echo "<div id='error'> Invalid email address entered </div>";
					  echo $form;}
			
			
// Error on passwords not matching
			
			}
			else {echo "<div id='error'> Your passwords do not match </div>";
				  echo $form;}

}
// Error on not entering all fields


	
		else {echo "<div id='error'> Please fill all the required fields </div>";
		echo $form ;}

}


// If the submit button has not yet been pressed, the form will be echoed

else {
	echo $form;
}


?>

</div>

</div>

<?require ('scripts/bottom.php'); ?>

:S The script almost works fine. I even get the success message. But when I check into my database table called 'members' using PHPMyAdmin, there is just no row created :icon_cry:

Here is how my 'Connect.php' file is set up :

<?php

$server = 'myservername';
$dbuser = 'mydatabaseusername';
$dbpass = 'mydatabasepassword';
$database = 'mydatabasename';

$con = mysql_connect ($server, $dbuser, $dbpass);
mysql_select_db ($database, $con);

?>

Any help would be appreciated

Member Avatar
NinjaMediaD
Light Poster
44 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

Nothing is added because to create a row in a database your query needs to be an insert statement for instance in your case:

$insertQuery = "INSERT INTO members (`column_names_here`,`seperated_by_commas`) VALUES ('$firstColumnValue','$secondColumnValue')";
$result = mysql_query($insertQuery) or die('Invalid query: ' . mysql_error());
Member Avatar
ruchir7
Newbie Poster
2 posts since Mar 2012
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

Nothing is added because to create a row in a database your query needs to be an insert statement for instance in your case:

$insertQuery = "INSERT INTO members (`column_names_here`,`seperated_by_commas`) VALUES ('$firstColumnValue','$secondColumnValue')";
$result = mysql_query($insertQuery) or die('Invalid query: ' . mysql_error());

Hey thanks! It worked. Up and running now. :)

I know I forgot to add the query. Plus I used ' instead of ` in line one. So, that was another error.

Thanks again.

Question Answered as of 2 Years Ago by NinjaMediaD
Member Avatar
NinjaMediaD
Light Poster
44 posts since Sep 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 2 [?]
Skill Endorsements: 0 [?]
 
0
 

its the simple things, you should check my last thread...

You
This question has already been solved: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: