12 Replies - 1184 Views - Last Post: 26 July 2012 - 12:22 PM Rate Topic: -----

#1 Daner++  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 18-July 12

INSERT INTO is not inserting data into my database

Posted 26 July 2012 - 08:41 AM

I dont run into any errors, it just simply doesnt enter the data into my database.
<?php

echo "<h1>Register</h1>";

$submit = filter_input(INPUT_POST,'submit');
$fullname = strip_tags(filter_input(INPUT_POST,'fullname')); 
$username = strip_tags(filter_input(INPUT_POST,'username')); 
$password = strip_tags(filter_input(INPUT_POST,'password')); 
$repeatpassword = strip_tags(filter_input(INPUT_POST,'repeatpassword')); 
$date = date("y-m-d");

if($submit)

{
if ($username&&$fullname&&$password&&$repeatpassword)//check for existance.

{

if ($password == $repeatpassword)

{


if(strlen($username) > 25 || strlen($fullname) > 25) // check char length of username and fullname

{

echo "Length of username or password is too long!";

}

else 

{

if(strlen($password) > 25 || strlen($password) < 6) //check password length

{
echo "password must be between 6 and 25 characters long!";
}
else
{

$password = md5($password); // encrypt password
$repeatpassword = md5($repeatpassword);

//register user

echo "Success!";				

//open database

$connect = mysql_connect('localhost', 'data', 'password');
mysql_select_db('mylogin');			

$queryreg = mysql_query("INSERT INTO users('date','name','username','password') VALUES ('$date','$fullname','$username','$password'"); 



die("You have been registered! <a href ='index.php'>Return to login page!</a>");
}

}

}

else
	echo "Your passwords do not match";

}

else	
	echo ("Please fill in <b>all</b> fileds!");

}



?>



<html>
<p>
<form action = 'register.php' method = 'POST'>
	<table>
	
		<tr>
			<td>
			Your full name:
			</td>
			<td>
			<input type = 'text' name = 'fullname' value ='<?php echo $fullname; ?>' >
			</td>
		</tr>	
		<tr>
			<td>
			Choose a username:
			</td>
			<td>
			<input type = 'text' name = 'username' value ='<?php echo $username; ?>'>
			</td>
		</tr>	
		<tr>
			<td>
			Choose a password:
			</td>
			<td>
			<input type = 'password' name = 'password'>
			</td>
		</tr>	
		<tr>
			<td>
			Repeat your password:
			</td>
			<td>
			<input type = 'password' name = 'repeatpassword'>
			</td>
		</tr>	
	</table>
	<p>
	<input type = 'submit' name = 'submit' value = 'Register'>
</form>

</html>


Is This A Good Question/Topic? 0
  • +

Replies To: INSERT INTO is not inserting data into my database

#2 Duckington  Icon User is offline

  • D.I.C Addict

Reputation: 169
  • View blog
  • Posts: 606
  • Joined: 12-October 09

Re: INSERT INTO is not inserting data into my database

Posted 26 July 2012 - 08:53 AM

Is it getting that far? So you see "Success!" and then "You have been registered!" printed out on the screen?

Change the queries so they die if there is an error, then you can see if there's a problem:

$connect = mysql_connect('localhost', 'data', 'password') or die('cannot connect');
mysql_select_db('mylogin') or die('cannot select db');         
 
$queryreg = mysql_query("INSERT INTO users('date','name','username','password') VALUES ('$date','$fullname','$username','$password'") or die('error: ' . mysql_error());



I'd also suggest using prepared data objects as opposed to those mysql_* functions, as they are safer.

This post has been edited by Duckington: 26 July 2012 - 08:54 AM

Was This Post Helpful? 1
  • +
  • -

#3 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2890
  • View blog
  • Posts: 10,019
  • Joined: 08-August 08

Re: INSERT INTO is not inserting data into my database

Posted 26 July 2012 - 09:04 AM

Some things you're missing that would make your life easier:

Was This Post Helpful? 2
  • +
  • -

#4 Daner++  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 18-July 12

Re: INSERT INTO is not inserting data into my database

Posted 26 July 2012 - 09:14 AM

Done what you suggest Duckington still not luck. I do get the "success, registered printed out.

CTphpnwb, I'll make sure i check them links out. Thanks
Was This Post Helpful? 0
  • +
  • -

#5 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: INSERT INTO is not inserting data into my database

Posted 26 July 2012 - 09:35 AM

Please, do as CTphpnwb suggested and indent your code. It makes it easier for you, and everyone else, to read.

Here is your code cleaned up:
<?php

echo "<h1>Register</h1>";

$submit = filter_input(INPUT_POST,'submit');
$fullname = strip_tags(filter_input(INPUT_POST,'fullname')); 
$username = strip_tags(filter_input(INPUT_POST,'username')); 
$password = strip_tags(filter_input(INPUT_POST,'password')); 
$repeatpassword = strip_tags(filter_input(INPUT_POST,'repeatpassword')); 
$date = date("y-m-d");

if($submit)
{
	if ($username&&$fullname&&$password&&$repeatpassword)//check for existance.
	{
		if ($password == $repeatpassword)
		{
			if(strlen($username) > 25 || strlen($fullname) > 25) // check char length of username and fullname
			{
				echo "Length of username or password is too long!";
			}
			else 
			{
				if(strlen($password) > 25 || strlen($password) < 6) //check password length
				{
					echo "password must be between 6 and 25 characters long!";
				}
				else
				{
					$password = md5($password); // encrypt password
					$repeatpassword = md5($repeatpassword);

					//register user
					echo "Success!";				

					//open database
					$connect = mysql_connect('localhost', 'data', 'password');
					mysql_select_db('mylogin');			

					$queryreg = mysql_query("INSERT INTO users('date','name','username','password') VALUES ('$date','$fullname','$username','$password'"); 

					die("You have been registered! <a href ='index.php'>Return to login page!</a>");
				}

			}

		}
		else
			echo "Your passwords do not match";
	}
	else	
		echo ("Please fill in <b>all</b> fileds!");

}



?>



<html>
	<p>
		<form action = 'register.php' method = 'POST'>
			<table>
			
				<tr>
					<td>
					Your full name:
					</td>
					<td>
					<input type = 'text' name = 'fullname' value ='<?php echo $fullname; ?>' >
					</td>
				</tr>	
				<tr>
					<td>
					Choose a username:
					</td>
					<td>
					<input type = 'text' name = 'username' value ='<?php echo $username; ?>'>
					</td>
				</tr>	
				<tr>
					<td>
					Choose a password:
					</td>
					<td>
					<input type = 'password' name = 'password'>
					</td>
				</tr>	
				<tr>
					<td>
					Repeat your password:
					</td>
					<td>
					<input type = 'password' name = 'repeatpassword'>
					</td>
				</tr>	
			</table>
			<p>
			<input type = 'submit' name = 'submit' value = 'Register'>
		</form>
	<p>
</html>




See how much easier that is to read and follow? If you plan on doing any kind of programming you should definitely be doing this.

You are seeing your success message because on line 34 you echo "Success" before you actually try to insert the data into your database. Instead do as Duckington suggested and check to see if the query is successful before returning a success message.
Was This Post Helpful? 0
  • +
  • -

#6 Daner++  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 18-July 12

Re: INSERT INTO is not inserting data into my database

Posted 26 July 2012 - 09:37 AM

This is what i get after using using mysql_error());


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 ''date','name','username','password') VALUES ('12-07-26','test','test','0b4e7a0e5' at line 1

Was This Post Helpful? 0
  • +
  • -

#7 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: INSERT INTO is not inserting data into my database

Posted 26 July 2012 - 09:46 AM

Hi,

You don't use single quotes around the field names in your INSERT statement. You can use `'s around field but it's only necessary if your field name has a space in it. So your query should be:
INSERT INTO users(date,name,username,password) VALUES ('$date','$fullname','$username','$password')



What can be helpful is to put the query in a separate variable so you can ouput the actual query if there is a problem. So
$sql = "INSERT INTO users(date,name,username,password) VALUES ('$date','$fullname','$username','$password');
mysql_query($sql);



Speaking of which, I would highly suggest you read CTphpnwb's link to prepared statements, which is a tutorial on PDO. You should not be using the mysql* database access functions as they are deprecated. If you are just starting to learn PHP you should learn PDO so you are doing things correctly from the start :)
Was This Post Helpful? 0
  • +
  • -

#8 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6036
  • View blog
  • Posts: 23,428
  • Joined: 23-August 08

Re: INSERT INTO is not inserting data into my database

Posted 26 July 2012 - 09:47 AM

The fields need to be backticked, not quoted.

` not '

You will definitely need to do this as you're using a reserved word as a column name, i.e., date.
Was This Post Helpful? 1
  • +
  • -

#9 Jstall  Icon User is offline

  • Lurker
  • member icon

Reputation: 434
  • View blog
  • Posts: 1,042
  • Joined: 08-March 09

Re: INSERT INTO is not inserting data into my database

Posted 26 July 2012 - 09:59 AM

Right didn't think about reserved words, good catch!
Was This Post Helpful? 0
  • +
  • -

#10 Daner++  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 18-July 12

Re: INSERT INTO is not inserting data into my database

Posted 26 July 2012 - 10:51 AM

Still no luck, ive done what has been suggest, but it still fails to transfer the information to the database :/
Was This Post Helpful? 0
  • +
  • -

#11 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6036
  • View blog
  • Posts: 23,428
  • Joined: 23-August 08

Re: INSERT INTO is not inserting data into my database

Posted 26 July 2012 - 11:50 AM

Show what your code looks like now.
Was This Post Helpful? 0
  • +
  • -

#12 no2pencil  Icon User is offline

  • Toubabo Koomi
  • member icon

Reputation: 5182
  • View blog
  • Posts: 26,879
  • Joined: 10-May 07

Re: INSERT INTO is not inserting data into my database

Posted 26 July 2012 - 12:04 PM

View PostDaner++, on 26 July 2012 - 01:51 PM, said:

Still no luck

Please keep in mind that no one is holding answers back. If a suggestion doesn't work, please reply with updated code so we can troubleshoot further.

You'll get quicker results if you provide more information, than us having to ask for it.
Was This Post Helpful? 2
  • +
  • -

#13 Daner++  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 12
  • Joined: 18-July 12

Re: INSERT INTO is not inserting data into my database

Posted 26 July 2012 - 12:22 PM

Got it working now, thank you for all the responses.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1