Cant get info from html form into my database

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
benjihubbs
New php-forum User
New php-forum User
Posts: 11
Joined: Sun Dec 12, 2010 3:20 am

Sun Dec 12, 2010 2:38 pm

Hi,


I am using this code:

Code: Select all

<?php
$username="root";
$password="";
$database="capricorncards";
$firstname=$_POST['First Name'];
$lastname=$_POST['Last Name'];
$homephone=$_POST['Home Phone'];
$mobile=$_POST['Mobile'];
$email=$_POST['Email Address'];
$con= mysql_connect(localhost,$username,$password);

$con;
mysql_select_db($database) or die( "ERROR");
mysql_query($query);

$query = "INSERT INTO customers Values ('','$firstname','$lastname','$homephone','$mobile','$emailaddress')";
if ("$query$con")
{
echo("success");
}
else
{
echo("FAIL");
mysql_close(localhost,$username,$password); 
}
?>

To move data entered in an html form to my database, when i enter the info and hit submit. It comes up with success (like i set it to if it works), but then when i go to myphpadmin and check the database its still empty.

Any ideas?

Thanks :help: :help:
User avatar
Barrikor
New php-forum User
New php-forum User
Posts: 16
Joined: Sun Dec 12, 2010 3:14 pm

Sun Dec 12, 2010 3:33 pm

Ah, I think you just accidentally typed it in the wrong order, you're doing mysql_query($query) before giving $query it's value =) See what happens when you switch the two lines.
benjihubbs
New php-forum User
New php-forum User
Posts: 11
Joined: Sun Dec 12, 2010 3:20 am

Sun Dec 12, 2010 3:39 pm

That has got it to enter into the database now thank you, but it has only entered the mobile number ($mobile), any ideas why that is?
User avatar
Barrikor
New php-forum User
New php-forum User
Posts: 16
Joined: Sun Dec 12, 2010 3:14 pm

Sun Dec 12, 2010 3:49 pm

Not sure, but that's your only $_POST var without a space in it... PHP doesn't care about spaces but I know that HTML can't send GET data right if there's spaces in the input name (<input name="XXXXXXXXXX">)... Not sure if it would cause problems on POST data though
benjihubbs
New php-forum User
New php-forum User
Posts: 11
Joined: Sun Dec 12, 2010 3:20 am

Sun Dec 12, 2010 3:51 pm

I did notice that, and have now changed them all so theres no spaces in, but still nothing :/

Thanks for your help by the way :)
User avatar
Barrikor
New php-forum User
New php-forum User
Posts: 16
Joined: Sun Dec 12, 2010 3:14 pm

Sun Dec 12, 2010 4:18 pm

No prob =)

I don't see what the problem is... Here, I changed the code a small bit to make it slightly easier to read, but I didn't see anything sticking out...

You're sure you also took the spaces out in the html input names?

Code: Select all

<?php
$username="root";
$password="";
$database="capricorncards";

$firstname=$_POST['FirstName'];
$lastname=$_POST['LastName'];
$homephone=$_POST['HomePhone'];
$mobile=$_POST['Mobile'];
$email=$_POST['EmailAddress'];

$con= mysql_connect(localhost,$username,$password);
if($con)
{
mysql_select_db($database) or die( "ERROR");

$query = "INSERT INTO customers 
VALUES ('','$firstname','$lastname','$homephone','$mobile','$emailaddress')";

queryWasExecuted = mysql_query($query);
if(queryWasExecuted)
{
echo "query success";
}
else
{
echo "query failed";
}


mysql_close(localhost,$username,$password); 

}
else
{
echo "connection failed";
}
?>
benjihubbs
New php-forum User
New php-forum User
Posts: 11
Joined: Sun Dec 12, 2010 3:20 am

Sun Dec 12, 2010 4:24 pm

well it seems to be working now, well except the email address one, but i'll figure it out myself. Dunno what i did to get it working though lol

Thanks a lot :D
Post Reply