Jump to content

What is the problem with my code?


john_wenhold

Recommended Posts

I have a form that when filled out, the info gets sent to mysql database.  If my database table is completely empty, then everything works.  But if there is an entry in the table already, I get the message 'error querying database'.

 

I know it has something to do with the following code I wrote:

$query = "SELECT * FROM textnook WHERE phonenumber = '{$_POST['phonenumber']}'";

		$data = mysql_query ($query);			
		if (mysql_num_rows($data) == 0) {

The purpose of the code above is to not allow someone to sign up with the same PHONENUMBER more than once.  But it is causing my database to only let ONE total entry into the table.

 

Here is the entire code.  If anyone knows the problem please help.  Thanks

 


<?php


		$firstname = ($_POST['firstname']);

		$lastname = ($_POST['lastname']);		

		$username = ($_POST['username']); 

		$password1 = ($_POST['password1']);

		$password2 = ($_POST['password2']);

		$phonenumber = ($_POST['phonenumber']);

		$sex = ($_POST['sex']);

		$month = ($_POST['month']);

		$day = ($_POST['day']);

		$year = ($_POST['year']);

		$categories = ($_POST['categories']);

		$days = ($_POST['days']);

		$text = ($_POST['text']);

		$comments = ($_POST['comments']);

		$submit = ($_POST['signup']);		




		if (isset($submit)) {
		$output_form = false;

		if (isset($firstname) && !empty($firstname)
		&& isset($lastname) && !empty($lastname) 
		&& isset($username) && !empty($username)
		&& isset($password1) && !empty($password1)
		&& isset($password2) && !empty($password2)
		&& isset($phonenumber) && !empty($phonenumber)
		&& isset($month) && !empty($month)
		&& isset($day) && !empty($day)
		&& isset($year) && !empty($year)
		&& isset($categories) && !empty($categories)
		&& isset($days) && !empty($days)
		&& isset($text) && !empty($text)
		&& isset($sex) && !empty($sex)
		&& ($password1 == $password2)) {

		$dbc = mysql_connect ('thetextnook.db.5566025.hostedresource.com', 'thetextnook', 'Jpw78413', 'thetextnook')
		      or die ('Error connecting to MySQL server.');

		mysql_select_db ('thetextnook', $dbc);

		$query = "SELECT * FROM textnook WHERE phonenumber = '{$_POST['phonenumber']}'";

		$data = mysql_query ($query);			
		if (mysql_num_rows($data) == 0) {


		$query1 = "INSERT INTO textnook ( first_name, last_name, user_name, password, phonenumber, sex, month, day, year, category, days, text, comments)
			 VALUES ( '$firstname', '$lastname', '$username', '$password1', '$phonenumber', '$sex', '$month','$day', '$year', '$categories', '$days', '$text','$comments')";

		$result = mysql_query ($query1, $dbc)
		      or die ("MYSQL ERROR: $query1 - " . mysql_error());


		echo 'Sign Up Confirmed' . '<br /> <br />';					

		echo 'Thanks for joining TextNook!' . '<br /> <br />'; 	

		echo 'You have signed up to receive :' .implode(", ",$_POST['categories']) . '<br /> <br />';

		echo 'You have signed up to receive :'  .implode(", ",$_POST['days']) . '<br /> <br />';

		echo 'And you have signed up to receive :' . $_POST['text'] . '<br /> <br />';

		echo 'You can change these selections at anytime by logging into your account.' . '<br /> <br />';

		echo 'Coming Soon: You will be able to choose individual stores and restraunts instead of entire categories.</span>' . '<br /> <br /> <br /> <br /> <br /> <br /> <br /> <br />';

		mysql_close ($dbc);
		exit();
		} 

		else {

		echo '<p> An account already exists with this phone number.  Please select a different phone number. </p>';
		$output_form = true;			

		}
		}

		else {			

		echo  'Attention! Unfortunately you did not provide all of the required sign up information. <br /> <br /> To sign up, please fill out all of the required form fields and make sure you confirm your password correctly.  Thankyou';
		$output_form = true;

		}			
		}

		else {

		$output_form = true; }

		if ($output_form) {

		?>	

 

Link to comment
Share on other sites

You probably want to edit out your database password. I'd also recommend changing it now.

 

As for your issue though, I read through your code and nothing stuck out to me as incorrect. Have you actually looked at the row it inserts into the database to make sure it's getting written correctly? Perhaps somewhere along the line the phonenumber isn't getting set correctly and defaulting to null or something else which makes mysql_num_rows return non zero

Link to comment
Share on other sites

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.