HTML Form Troubles

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
camhabib
Forum Commoner
Posts: 37
Joined: Tue Aug 16, 2005 8:36 pm
Location: Boston, MA

HTML Form Troubles

Post by camhabib »

I'm trying to write a script to post information from a HTML form to a MySQL database. I wrote this, it executes when the submit button is pressed, that much I got it to do. However nothing happens. It doesn't return any message, doesn't post any information. I changed the password and username for posting, but it logs in fine, just nothing happens.

Code: Select all

<?php

$password="password";
$username="username";
$database="mysql";

$Quote=$_POST['quote'];
$FName=$_POST['fname'];
$LName=$_POST['lname'];
$City=$_POST['city'];
$State=$_POST['state'];
$Country=$_POST['country'];
$Age=$_POST['age'];
$Gender=$_POST['age'];
$Marital=$_POST['marital'];
$Occupation=$_POST['occu'];
$Education=$_POST['edu'];
$Mail=$_POST['email'];

mysql_connect($database,$username,$password);
@mysql_select_db($database) or die("Unable to connect to database");

$query="INSERT INTO Submissions.info SET Quote='$quote', FName='$fname', LName='$lname', 
city='$city', state='$state', country='$country', age='$age', gender='$gender', 
marital='$marital', occupation='$occupation', education='$education', mail='$mail';
mysql_query($query)" = $conf;

if ($conf) echo"Sucsessful entry";
else echo"Entry failure";

mysql_close();

?>

feyd | Please use

Code: Select all

tags when posting php code.[/color][/size]
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

uhm.. you need a mysql_query() call.
camhabib
Forum Commoner
Posts: 37
Joined: Tue Aug 16, 2005 8:36 pm
Location: Boston, MA

Post by camhabib »

I have one, 5th line from the bottom, or are you talking about something else?
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Re: HTML Form Troubles

Post by nielsene »

Code: Select all

<?php

$password="password";
$username="username";
$database="mysql";

$Quote=$_POST['quote'];
$FName=$_POST['fname'];
$LName=$_POST['lname'];
$City=$_POST['city'];
$State=$_POST['state'];
$Country=$_POST['country'];
$Age=$_POST['age'];
$Gender=$_POST['age'];
$Marital=$_POST['marital'];
$Occupation=$_POST['occu'];
$Education=$_POST['edu'];
$Mail=$_POST['email'];

mysql_connect($database,$username,$password);
@mysql_select_db($database) or die("Unable to connect to database");

$query="INSERT INTO Submissions.info SET Quote='$quote', FName='$fname', LName='$lname', 
city='$city', state='$state', country='$country', age='$age', gender='$gender', 
marital='$marital', occupation='$occupation', education='$education', mail='$mail'";
$conf = mysql_query($query);

if ($conf) echo"Sucsessful entry";
else echo"Entry failure";

mysql_close();

?>
Your double quotes were extending to include the mysql_query and you had the function call on the wrong side of an equality.
Last edited by nielsene on Wed Aug 17, 2005 5:20 pm, edited 1 time in total.
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

your code specifically says it's not there. You notice how mysql_query() is red? That means it's inside a string.

and assignments go right to left, not left to right (swap $conf and mysql_query()'s location in the line after fixing the string issue.)
camhabib
Forum Commoner
Posts: 37
Joined: Tue Aug 16, 2005 8:36 pm
Location: Boston, MA

Post by camhabib »

WOW, I feel really pretty stupid right now. So it was just the order of the variable being defined and the "" that screwed that whole script up?
User avatar
nielsene
DevNet Resident
Posts: 1834
Joined: Fri Aug 16, 2002 8:57 am
Location: Watertown, MA

Post by nielsene »

Failing to close a quote will always wreck havoc on a script. Its one reason why a lot of people use tools that will highlight the different parts of the code in different colors, makes it very easy to see when you've got an unclosed quote somewhere.

Ditto for "side-ness" (left versus right) on assignments.
User avatar
Ambush Commander
DevNet Master
Posts: 3698
Joined: Mon Oct 25, 2004 9:29 pm
Location: New Jersey, US

Post by Ambush Commander »

Of course, when you're typing, and then the colors start flashing, you know that your syntax highlighting is hypersensitive. :?
camhabib
Forum Commoner
Posts: 37
Joined: Tue Aug 16, 2005 8:36 pm
Location: Boston, MA

Post by camhabib »

Alright, so the script was working. I then decided to add a function that made it so that if any field was left blank it would redirect to another page instead of continuing to post to the database. However, when I use this new script, nothing happens. Its just like before. I checked all of the "" and {} not to menion the order of everything. I think I'm going to have to quite this whole PHP thing if this keeps up.

Code: Select all

<?php 

$password="password"; 
$username="username"; 
$database="mysql"; 

$Quote=$_POST['quote']; 
$FName=$_POST['fname']; 
$LName=$_POST['lname']; 
$City=$_POST['city']; 
$State=$_POST['state']; 
$Country=$_POST['country']; 
$Age=$_POST['age']; 
$Gender=$_POST['age']; 
$Marital=$_POST['marital']; 
$Occupation=$_POST['occu']; 
$Education=$_POST['edu']; 
$Mail=$_POST['email'];

mysql_connect($database,$username,$password); 
@mysql_select_db($database) or die("Unable to connect to database.  Please contact the webmaster for further assistance."); 

$incor=""

if (strcmp($Quote,$incor) != 0) {
require("incomp.html");
} else if (strcmp($FName,$incor) != 0) {
require("incomp.html");
} else if (strcmp($LName,$incor) != 0) {
require("incomp.html");
} else if (strcmp($City,$incor) != 0) {
require("incomp.html");
} else if (strcmp($State,$incor) != 0) {
require("incomp.html");
} else if (strcmp($Country,$incor) != 0) {
require("incomp.html");
} else if (strcmp($Age,$incor) != 0) {
require("incomp.html");
} else if (strcmp($Gender,$incor) != 0) {
require("incomp.html");
} else if (strcmp($Marital,$incor) != 0) {
require("incomp.html");
} else if (strcmp($Occupation,$incor) != 0) {
require("incomp.html");
} else if (strcmp($Education,$incor) != 0) {
require("incomp.html");
} else if (strcmp($Mail,$incor) != 0) {
require("incomp.html");
} else {

$query="INSERT INTO Submissions.info SET Quote='$quote', FName='$fname', LName='$lname', city='$city', state='$state', country='$country', age='$age', gender='$gender', marital='$marital', occupation='$occupation', education='$education', mail='$mail'"; 
$conf = mysql_query($query); 

if ($conf) {
require("form_comf.html");
}
else {
require("form_deny.html"); 
}

}

mysql_close(); 

?>
pentiumhead
Forum Newbie
Posts: 9
Joined: Wed Aug 17, 2005 11:24 am

Post by pentiumhead »

instead of setting:

$inCor = "";

then comparing with each variable
myadvice is use:

if (!strlen($variable_name))
{
require("error_page.html");
}

That might just solve it.

Kay
camhabib
Forum Commoner
Posts: 37
Joined: Tue Aug 16, 2005 8:36 pm
Location: Boston, MA

Post by camhabib »

Forgot a ; on the $incorr statement. Plus changed != to ==. Stupid mistakes as usual that I didn't catch until I posted.
Post Reply