Php form wont insert Mysql into Mysql database

  • (2 Pages)
  • +
  • 1
  • 2

16 Replies - 1696 Views - Last Post: 14 November 2008 - 05:06 PM

#1 revolution14  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 11-December 07

Php form wont insert Mysql into Mysql database

Post icon  Posted 14 November 2008 - 03:36 PM

My problem is i am used to using SQL not MySQL therefore some commands are slightly different.

I am trying to submit contents of a valid PHP form to my MYSQL database!
the database is called 'customer'. I am using wamp to show my php page


Please help me! i have spent hours looking around on Google and Php.net to no avail :^:



<html>
<head>
<title>PHP Data Entery</title>
</head>
<body>
<form action="" method="post">
   <B>Name: </B><input type="text" name="Name"/><br><br>
   <B>Address: </B><input type="text" name="Address"/><br><br>
   <B>Postcode: </B><input type="text" name="Postcode"/><br><br>
   <B>Telephone: </B><input type="text" name="Telephone"/<br><br><br>
   <B>Email: </B><input type="text" name="Email"/><br><br>
   <input type="submit" name="submit" id="submit" value="submit" />
   </form>
<?php if ((strlen($_POST['Name'])<=50) && ctype_alpha($_POST['Name'])){
		 $Name=$_POST['Name'];}
		 else {$Name=invalid;}

	  if (strlen($_POST['Address'])<=200){
		 $Address= $_POST['Address'];}
		 else {$Address=invalid;}

	  if (strlen($_POST['Postcode'])<=10){
		 $Postcode=$_POST['Postcode'];}
		 else {$Postcode=invalid;}
	  
	  if ((strlen($_POST['Telephone'])<=15)&&(!preg_match("/[^\d]/m", $_POST['Telephone']))) {
		  $Telephone=$_POST['Telephone'];}
		  else {$Telephone=invalid;}


	 if ((eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST['Email']))&&(strlen($POST_['Email'])<=100)){
	  $Email=$_POST['Email'];}
		  else {$Email=invalid;}


	 if ($Name!='invalid' && $Address!='invalid' && $Postcode!='invalid' && $Telephone!='invalid' && $Email!='invalid'){
		 $Form=valid;}
		 else {$Form=invalid;}
	
	 if ($Form =='valid'){
	 $con = mysql_connect("localhost","ian","password");
	 if (!$con)
				{
				die('Could not connect: ' . mysql_error());
				}

		 mysql_select_db("customer", $con);
		 $result = mysql_query($query);
		 $query=("INSERT INTO customer SET (Name , Address, Postcode, Telephone, Email) Values ('$Name', '$Address', '$Postcode', '$Telephone', '$Email')");
		 echo "the form has been submitted";
	 mysql_close($con);}
		 else echo "The form was invalid and not submitted";
		  

?>
<BR>
<BR>
<BR>

Form: <?php echo $Form; ?>

<BR>
	
</body>
</html>



Is This A Good Question/Topic? 0
  • +

Replies To: Php form wont insert Mysql into Mysql database

#2 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Php form wont insert Mysql into Mysql database

Posted 14 November 2008 - 03:49 PM

$result = mysql_query($query);
$query=("INSERT INTO customer SET (Name , Address, Postcode, Telephone, Email) Values ('$Name', '$Address', '$Postcode', '$Telephone', '$Email')");


Seems to be in the wrong order, right?
But then are you really supposed to have the single quotes around the variables or are they treated as strings?
Long time since I experimented with this but the order has to be wrong for sure..

This seems to be wrong as well..

$Form=valid;}
else {$Form=invalid;}

if ($Form =='valid'){

When you assign the variable Form a value then you should use the single-quotes around the string.

Oh, and perhaps you actually did, but you shouldn't post the login and password to the DB like you did in the code and especially NEVER use 'password' or 'pass' as password
Was This Post Helpful? 0
  • +
  • -

#3 revolution14  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 11-December 07

Re: Php form wont insert Mysql into Mysql database

Posted 14 November 2008 - 03:51 PM

Oops yeh they are the wrong way round! what a dumbass mistake to make!

It still doesnt work after i change that. I dont no if it is my MySQL code??

Thanks for your reply
Was This Post Helpful? 0
  • +
  • -

#4 revolution14  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 11-December 07

Re: Php form wont insert Mysql into Mysql database

Posted 14 November 2008 - 04:05 PM

'Oh, and perhaps you actually did, but you shouldn't post the login and password to the DB like you did in the code and especially NEVER use 'password' or 'pass' as password '

this isnt too much of an issue as the code is only for my own personal practise! I am trying to get to grips with php!

as regards to

$Form=valid;}
else {$Form=invalid;}

if ($Form =='valid'){

I see what you mean here i should be writing as $Form='valid';}

Still no luck! i have changed all of the things you mentioned! any more ideas people?

I have just noticed that the loop is being processed becuase the
'the form has been submitted' echo is being processed

therefore i can assume that this is an error with my inerst into??

if ($Form =='valid'){
	 $con = mysql_connect("localhost","ian","password");
	 if (!$con)
				{
				die('Could not connect: ' . mysql_error());
				}

		 mysql_select_db("customer", $con);
		 
		 $query=("INSERT INTO customer SET (Name , Address, Postcode, Telephone, Email) Values ($Name, $Address, $Postcode, $Telephone, $Email)");
		 echo "the form has been submitted";
	 
	 $result = mysql_query($query);
	 mysql_close($con);}
		 else echo "The form was invalid and not submitted";


Was This Post Helpful? 0
  • +
  • -

#5 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Php form wont insert Mysql into Mysql database

Posted 14 November 2008 - 04:05 PM

repost the code please and I'll take another look.
Was This Post Helpful? 0
  • +
  • -

#6 revolution14  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 11-December 07

Re: Php form wont insert Mysql into Mysql database

Posted 14 November 2008 - 04:07 PM

<html>
<head>
<title>PHP Data Entery</title>
</head>
<body>
<form action="" method="post">
   <B>Name: </B><input type="text" name="Name"/><br><br>
   <B>Address: </B><input type="text" name="Address"/><br><br>
   <B>Postcode: </B><input type="text" name="Postcode"/><br><br>
   <B>Telephone: </B><input type="text" name="Telephone"/<br><br><br>
   <B>Email: </B><input type="text" name="Email"/><br><br>
   <input type="submit" name="submit" id="submit" value="submit" />
   </form>
<?php if ((strlen($_POST['Name'])<=50) && ctype_alpha($_POST['Name'])){
		 $Name=$_POST['Name'];}
		 else {$Name=invalid;}

	  if (strlen($_POST['Address'])<=200){
		 $Address= $_POST['Address'];}
		 else {$Address=invalid;}

	  if (strlen($_POST['Postcode'])<=10){
		 $Postcode=$_POST['Postcode'];}
		 else {$Postcode=invalid;}
	  
	  if ((strlen($_POST['Telephone'])<=15)&&(!preg_match("/[^\d]/m", $_POST['Telephone']))) {
		  $Telephone=$_POST['Telephone'];}
		  else {$Telephone=invalid;}


	 if ((eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST['Email']))&&(strlen($POST_['Email'])<=100)){
	  $Email=$_POST['Email'];}
		  else {$Email=invalid;}


	 if ($Name!='invalid' && $Address!='invalid' && $Postcode!='invalid' && $Telephone!='invalid' && $Email!='invalid'){
		 $Form ='valid';}
		 else {$Form ='invalid';}
	
	 if ($Form =='valid'){
	 $con = mysql_connect("localhost","ian","password");
	 if (!$con)
				{
				die('Could not connect: ' . mysql_error());
				}

		 mysql_select_db("customer", $con);
		 
		 $query=("INSERT INTO customer SET (Name , Address, Postcode, Telephone, Email) Values ($Name, $Address, $Postcode, $Telephone, $Email)");
		 echo "the form has been submitted";
	 
	 $result = mysql_query($query);
	 mysql_close($con);}
		 else echo "The form was invalid and not submitted";
		  

?>
<BR>
<BR>
<BR>

Form: <?php echo $Form; ?>

<BR>


 
	
</body>
</html>



I appreciate you taking the time too look at my code Gloin!!
Was This Post Helpful? 0
  • +
  • -

#7 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Php form wont insert Mysql into Mysql database

Posted 14 November 2008 - 04:07 PM

You know, you could echo the query (remember to comment the actual querying) and see what the query-string looks like.

Usually when the program works and you don't get the result you want, it's the query that fails.
Was This Post Helpful? 0
  • +
  • -

#8 revolution14  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 11-December 07

Re: Php form wont insert Mysql into Mysql database

Posted 14 November 2008 - 04:08 PM

sorry thats before i added the extra ' 's

<html>
<head>
<title>PHP Data Entery</title>
</head>
<body>
<form action="" method="post">
   <B>Name: </B><input type="text" name="Name"/><br><br>
   <B>Address: </B><input type="text" name="Address"/><br><br>
   <B>Postcode: </B><input type="text" name="Postcode"/><br><br>
   <B>Telephone: </B><input type="text" name="Telephone"/<br><br><br>
   <B>Email: </B><input type="text" name="Email"/><br><br>
   <input type="submit" name="submit" id="submit" value="submit" />
   </form>
<?php if ((strlen($_POST['Name'])<=50) && ctype_alpha($_POST['Name'])){
		 $Name=$_POST['Name'];}
		 else {$Name='invalid';}

	  if (strlen($_POST['Address'])<=200){
		 $Address= $_POST['Address'];}
		 else {$Address='invalid';}

	  if (strlen($_POST['Postcode'])<=10){
		 $Postcode=$_POST['Postcode'];}
		 else {$Postcode='invalid';}
	  
	  if ((strlen($_POST['Telephone'])<=15)&&(!preg_match("/[^\d]/m", $_POST['Telephone']))) {
		  $Telephone=$_POST['Telephone'];}
		  else {$Telephone='invalid';}


	 if ((eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST['Email']))&&(strlen($POST_['Email'])<=100)){
	  $Email=$_POST['Email'];}
		  else {$Email='invalid';}


	 if ($Name!='invalid' && $Address!='invalid' && $Postcode!='invalid' && $Telephone!='invalid' && $Email!='invalid'){
		 $Form ='valid';}
		 else {$Form ='invalid';}
	
	 if ($Form =='valid'){
	 $con = mysql_connect("localhost","ian","password");
	 if (!$con)
				{
				die('Could not connect: ' . mysql_error());
				}

		 mysql_select_db("customer", $con);
		 
		 $query=("INSERT INTO customer SET (Name , Address, Postcode, Telephone, Email) Values ($Name, $Address, $Postcode, $Telephone, $Email)");
		 echo "the form has been submitted";
	 
	 $result = mysql_query($query);
	 mysql_close($con);}
		 else echo "The form was invalid and not submitted";
		  

?>
<BR>
<BR>
<BR>

Form: <?php echo $Form; ?>

<BR>


 
	
</body>
</html>


Was This Post Helpful? 0
  • +
  • -

#9 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Php form wont insert Mysql into Mysql database

Posted 14 November 2008 - 04:09 PM

SET is only used when updating so that should be removed from the query.
Was This Post Helpful? 0
  • +
  • -

#10 revolution14  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 11-December 07

Re: Php form wont insert Mysql into Mysql database

Posted 14 November 2008 - 04:14 PM

i have just done that and here is my output

submittedINSERT INTO customer SET (Name , Address, Postcode, Telephone, Email) Values (test, test, test, 000, test@test.com)





it must be an error with this! I thought perhaps my information wasn't posting but that seems fine

i have just noticed on my echo there is no semi colon to exectute the statement!

Let me check that


checked it! sill no joy :-(

This post has been edited by revolution14: 14 November 2008 - 04:15 PM

Was This Post Helpful? 0
  • +
  • -

#11 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Php form wont insert Mysql into Mysql database

Posted 14 November 2008 - 04:21 PM

Ok, so again, you shouldn't use SET when you INSERT. Then as I said before, I'm not 100% sure about the single quotes around the parameters in VALUES.
Was This Post Helpful? 0
  • +
  • -

#12 revolution14  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 11-December 07

Re: Php form wont insert Mysql into Mysql database

Posted 14 November 2008 - 04:26 PM

I have changed them now!

i have even tried

"INSERT INTO customer "."(Name=".$Name." , Address= ".$Address. ", Postcode=".$Postcode." Telephone= ".$Telephone. " Email=".$Email.");";


Was This Post Helpful? 0
  • +
  • -

#13 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Php form wont insert Mysql into Mysql database

Posted 14 November 2008 - 04:34 PM

Syntax:

INSERT INTO table_name (column1, column2, column3,...)
VALUES (value1, value2, value3,...)

there's no ; in SQL

ex: (One of these 2 should work)

$query=("INSERT INTO customer (Name , Address, Postcode, Telephone, Email) Values ($Name, $Address, $Postcode, $Telephone, $Email)");

$query=("INSERT INTO customer (Name , Address, Postcode, Telephone, Email) Values ('$Name', '$Address', '$Postcode', '$Telephone', '$Email')");

Can't quite remember which.. otherwise I'm guessing the problem is found elsewhere but since your debugging session got here the problem should be here as well.
Was This Post Helpful? 0
  • +
  • -

#14 revolution14  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 49
  • Joined: 11-December 07

Re: Php form wont insert Mysql into Mysql database

Posted 14 November 2008 - 04:49 PM

Rite ok! the problem has been solved now!

Basically the problem as i gatherd was my sql code i kept posting the code out and running it in Mysql and getting errors. The variable names needed ' ' around them and the statment needed " " around it.

here is my finalized code! just incase anyone else comes across similar problems!! also a Big thanks to Gloin!!!

<html>
<head>
<title>PHP Data Entery</title>
</head>
<body>
<form action="" method="post">
   <B>Name: </B><input type="text" name="Name"/><br><br>
   <B>Address: </B><input type="text" name="Address"/><br><br>
   <B>Postcode: </B><input type="text" name="Postcode"/><br><br>
   <B>Telephone: </B><input type="text" name="Telephone"/<br><br><br>
   <B>Email: </B><input type="text" name="Email"/><br><br>
   <input type="submit" name="submit" id="submit" value="submit" />
   </form>
<?php if ((strlen($_POST['Name'])<=50) && ctype_alpha($_POST['Name'])){
		 $Name=$_POST['Name'];}
		 else {$Name='invalid';}

	  if (strlen($_POST['Address'])<=200){
		 $Address= $_POST['Address'];}
		 else {$Address='invalid';}

	  if (strlen($_POST['Postcode'])<=10){
		 $Postcode=$_POST['Postcode'];}
		 else {$Postcode='invalid';}
	  
	  if ((strlen($_POST['Telephone'])<=15)&&(!preg_match("/[^\d]/m", $_POST['Telephone']))) {
		  $Telephone=$_POST['Telephone'];}
		  else {$Telephone='invalid';}


	 if ((eregi("^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*(\.[a-z]{2,3})$", $_POST['Email']))&&(strlen($POST_['Email'])<=100)){
	  $Email=$_POST['Email'];}
		  else {$Email='invalid';}


	 if ($Name!='invalid' && $Address!='invalid' && $Postcode!='invalid' && $Telephone!='invalid' && $Email!='invalid'){
		 $Form ='valid';}
		 else {$Form ='invalid';}
	
	 if ($Form =='valid'){
	 $con = mysql_connect("localhost","ian","password");
	 if (!$con)
				{
				die('Could not connect: ' . mysql_error());
				}

		 mysql_select_db("customer", $con);
		 
		 $query=("INSERT INTO customer SET Name ='$Name', Address ='$Address', Postcode ='$Postcode', Telephone =$Telephone, Email ='$Email'");
		 echo "the form has been submitted";
	 
	 $result = mysql_query($query);
	 mysql_close($con);}
		 else echo "The form was invalid and not submitted";
		 echo $query;
		  

?>
<BR>
<BR>
<BR>

Form: <?php echo $Form; ?>

<BR>



	
</body>
</html>
 




Was This Post Helpful? 0
  • +
  • -

#15 Gloin  Icon User is offline

  • Expert Schmexpert...
  • member icon

Reputation: 235
  • View blog
  • Posts: 4,489
  • Joined: 04-August 08

Re: Php form wont insert Mysql into Mysql database

Posted 14 November 2008 - 04:53 PM

Thanks right back at ya, good to get reminded of how it works sometimes. Long time, no sql (or php) :P
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2