Problem submiting to msql with html form $_POST.[solved]

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

toby_c500
Forum Commoner
Posts: 50
Joined: Fri May 11, 2007 11:29 am
Location: Leeds, England

Problem submiting to msql with html form $_POST.[solved]

Post by toby_c500 »

Hi,

I posted here yesterday with a problem on submiting a form. THANKS to those people who took the time out to help this poor newbie.

I re-evaluated my entire script. Below is what I have now. It still doesn't work. All I get is a blank page. I have the form on a standard html page with no scripting on it. which targets this PHP. As I say, all I get is a blank page with no errors (see the first two lines).

Could someone please talk to me like a stupid 4yr old' 'cause I just don't seem to be grasping this.

Thanks in advance for your help.

The PHP community ROCKS!!

Code: Select all

<?php

error_reporting(E_ALL);
ini_set('display_errors', true);

$link = mysql_connect('localhost', 'root', 'root');
    $db = mysql_select_db('jobs4alltrades', $link);



	if(isset($_POST['loginid'])) {
		$insert = "INSERT INTO members ('loginid', 'password', 'firstname', 'surname', 'email', 'trade', 'address1', 'address2', 'address3', 'address4', 'postzip', 'country', 'yearsexp', 'about') VALUES ('".$_POST['loginid']."', '".$_POST['password']."', '".$_POST['firstname']."', '".$_POST['surname']."', '".$_POST['email']."', '".$_POST['trade']."', '".$_POST['address1']."', '".$_POST['address2']."', '".$_POST['address3']."','".$_POST['address4']."', '".$_POST['postzip']."', '".$_POST['country']."','".$_POST['yearsexp']."','".$_POST['about']."')";
		$result = mysql_query($insert, $link);
				
				if(!$link){
					echo "problem linking to mysql";
					
				}
				
				if(!$db){
					echo "prob with db name";
					exit;
				}
				
				if (!!$result) {
					echo "Your details was succesfully added to our database. Use the navigation bar above to take you where you want to go next<br><br>"";
				}
				
				
				else ($result) {
					echo "Nope, not this time";
				}
	}

 $QUERY = "SELECT 'loginid', 'password' FROM members";

    $RESULT = mysql_query($QUERY);

    

    if(mysql_num_rows($RESULT) > 0) {

        echo "<p>Rows in table: <ol>";

        while($row = mysql_fetch_assoc($RESULT)) {

            echo "<li>". $row['loginid'] ." = ". $row['password'] ."</li>";

        }

        echo "</ol></p>";

    } 

    else {

        echo "<p>No rows exist in the table</p>";

    }




 ?>
Last edited by toby_c500 on Tue May 15, 2007 12:44 pm, edited 1 time in total.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post by aceconcepts »

Hi,

Can u show ur HTML form?
toby_c500
Forum Commoner
Posts: 50
Joined: Fri May 11, 2007 11:29 am
Location: Leeds, England

Post by toby_c500 »

Hi,

This is my html form. Do you think the problem lies here?

Code: Select all

<form name="register" action="register.php" method="post">
																<div align="center">
																	<table width="580" border="0" cellpadding="4" cellspacing="2">
																		<tr>
																			<td width="120">
																				<div align="right">
																					<b><font color="#0099ff" face="Verdana, Arial, Helvetica, sans-serif">Login name</font></b></div>
																			</td>
																			<td colspan="2">
																				<div align="left">
																					<input type="text" name="loginid" size="40" /></div>
																			</td>
																		</tr>
																		<tr>
																			<td width="120">
																				<div align="right">
																					<b><font color="#0099ff" face="Verdana, Arial, Helvetica, sans-serif">Password</font></b></div>
																			</td>
																			<td colspan="2">
																				<div align="left">
																					<input type="password" name="password" size="24" /></div>
																			</td>
																		</tr>
																		<tr height="5">
																			<td width="120" height="5"></td>
																			<td height="5"></td>
																			<td width="100" height="5"></td>
																		</tr>
																		<tr>
																			<td width="120">
																				<div align="right">
																					<b><font color="#0099ff" face="Verdana, Arial, Helvetica, sans-serif">First Name</font></b></div>
																			</td>
																			<td colspan="2">
																				<div align="left">
																					<input type="text" name="firstname" size="40" /></div>
																			</td>
																		</tr>
																		<tr>
																			<td width="120">
																				<div align="right">
																					<b><font color="#0099ff" face="Verdana, Arial, Helvetica, sans-serif"> Surname</font></b></div>
																			</td>
																			<td colspan="2">
																				<div align="left">
																					<input type="text" name="surname" size="40" /></div>
																			</td>
																		</tr>
																		<tr>
																			<td width="120">
																				<div align="right">
																					<b><font color="#0099ff" face="Verdana, Arial, Helvetica, sans-serif">Email</font></b></div>
																			</td>
																			<td colspan="2"><input type="text" name="email" size="40" /></td>
																		</tr>
																		<tr height="5">
																			<td width="120" height="5"></td>
																			<td height="5"></td>
																			<td width="100" height="5"></td>
																		</tr>
																		<tr>
																			<td width="120">
																				<div align="right">
																					<b><font color="#0099ff" face="Verdana, Arial, Helvetica, sans-serif">Trade</font></b></div>
																			</td>
																			<td colspan="2">
																				<div align="left">
																					<select name="trade">
																						<option selected="selected" value="none">Please Select</option>
																						<option value="Joiner">Joiner</option>
																					</select></div>
																			</td>
																		</tr>
																		<tr>
																			<td width="120">
																				<div align="right">
																					<b><font color="#0099ff" face="Verdana, Arial, Helvetica, sans-serif">Other</font></b></div>
																			</td>
																			<td colspan="2">
																				<div align="left">
																					<input type="text" name="other" size="40" /></div>
																			</td>
																		</tr>
																		<tr height="5">
																			<td width="120" height="5"></td>
																			<td height="5"></td>
																			<td width="100" height="5"></td>
																		</tr>
																		<tr height="5">
																			<td width="120" height="5">
																				<div align="right">
																					<b><font color="#0099ff" face="Verdana, Arial, Helvetica, sans-serif">Address</font></b></div>
																			</td>
																			<td colspan="2" height="5">
																				<div align="left">
																					<input type="text" name="address1" size="40" /></div>
																			</td>
																		</tr>
																		<tr height="5">
																			<td width="120" height="5"></td>
																			<td colspan="2" height="5">
																				<div align="left">
																					<input type="text" name="address2" size="40" value="" /></div>
																			</td>
																		</tr>
																		<tr height="5">
																			<td width="120" height="5"></td>
																			<td colspan="2" height="5">
																				<div align="left">
																					<input type="text" name="address3" size="40" value="" /></div>
																			</td>
																		</tr>
																		<tr height="5">
																			<td width="120" height="5"></td>
																			<td colspan="2" height="5">
																				<div align="left">
																					<input type="text" name="address4" size="40" value="" /></div>
																			</td>
																		</tr>
																		<tr>
																			<td width="120">
																				<div align="right">
																					<b><font color="#0099ff" face="Verdana, Arial, Helvetica, sans-serif">Zip/Postal Code</font></b></div>
																			</td>
																			<td colspan="2">
																				<div align="left">
																					<input type="text" name="post_zip" size="40" value="" /></div>
																			</td>
																		</tr>
																		<tr>
																			<td width="120">
																				<div align="right">
																					<b><font color="#0099ff" face="Verdana, Arial, Helvetica, sans-serif">Country</font></b></div>
																			</td>
																			<td colspan="2"><select name="country">
																					<option selected="selected" value="0">Please Select      </option>
																					<option value="England">England</option>
																					<option></option>
																					<option></option>
																				</select></td>
																		</tr>
																		<tr height="4">
																			<td width="120" height="4">
																				
																			</td>
																			<td height="4"></td>
																			<td width="100" height="4"></td>
																		</tr>
																		<tr>
																			<td width="120">
																				<div align="right">
																					<b><font color="#0099ff" face="Verdana, Arial, Helvetica, sans-serif">Years of Experience</font></b></div>
																			</td>
																			<td colspan="2">
																				<div align="left">
																					<input type="text" name="yearsexp" size="40" /></div>
																			</td>
																		</tr>
																		<tr height="5">
																			<td width="120" height="5">
																				<div align="right">
																					</div>
																			</td>
																			<td colspan="2" height="5">
																				<div align="left">
																					</div>
																			</td>
																		</tr>
																		<tr height="300">
																			<td valign="top" width="120" height="300">
																				<div align="right">
																					<b><font color="#0099ff" face="Verdana, Arial, Helvetica, sans-serif">About you</font></b></div>
																			</td>
																			<td colspan="2" valign="top" height="300"><textarea name="about" cols="60" rows="25"></textarea></td>
																		</tr>
																		<tr>
																			<td width="120"></td>
																			<td>
																				<div align="right">
																					<input type="reset" value="Clear" /></div>
																			</td>
																			<td align="right" width="100">
																				<div align="right">
																					<input type="submit" name="submit" value="Submit" /></div>
																			</td>
																		</tr>
																	</table>
																	
																</div>
															</form>
														</div>
													<!-- InstanceEndEditable --></td>
											</tr>
											<tr height="20">
												<td height="20">
													<div align="center">
														<font size="1" color="#fff200" face="Verdana, Arial, Helvetica, sans-serif">jobs4alltrades.com © Copyright 2007. All rights reserved.</font></div>
												</td>
											</tr>
										</table>
Thanks for looking aceconcepts.

Toby
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

The syntax highlighting of this board should have made the problem self evident...

Code: Select all

if (!!$result) {
                                        echo "Your details was succesfully added to our database. Use the navigation bar above to take you where you want to go next<br><br>"";
                                }
you have a double end quote.
Z3RO21
Forum Contributor
Posts: 130
Joined: Thu Aug 17, 2006 8:59 am

Post by Z3RO21 »

And 2 ! in the condition
toby_c500
Forum Commoner
Posts: 50
Joined: Fri May 11, 2007 11:29 am
Location: Leeds, England

Thanks for the input.

Post by toby_c500 »

Thanks for the input guys. I have corrected those mistakes to no avail.

I still get a blank screen after I submit the form.

Any other ideas??

I'm grateful of the help.
Toby
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

post the new code please
toby_c500
Forum Commoner
Posts: 50
Joined: Fri May 11, 2007 11:29 am
Location: Leeds, England

Post by toby_c500 »

Hi Jcart.

the code is the same as above but I have removed the extra ! in (!!$result) and the extra " at the end to the echo statement.

Is the code flawed? or am I on the right track?

Thanks
Toby
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Not that I don't believe you, you would be suprised how often a user sais, "oh I fixed that, but its still not working", when infact they did something wrong or misunderstood.
I took a glance at your code, however, I did not see anything immediatly wrong syntax-wise.

It's just better that you re-post the code after you make changes. Better safe than sorry.

It would be better if you turned display_errors to ON in your php.ini and restarted your server, so we could pinpoint the error.
toby_c500
Forum Commoner
Posts: 50
Joined: Fri May 11, 2007 11:29 am
Location: Leeds, England

Post by toby_c500 »

Thanks for taking the time out to help me.

I have just changed the php.ini to display_errors = on.
I found four php.ini file on my computer. Is this right?

I am running MAMP on my mac. These are the locations of the files I have found:

/Applications/MAMP/conf/php4
/Applications/MAMP/conf/conf/php4

/Applications/MAMP/conf/php5
/Applications/MAMP/conf/conf/php5

The is the code:

Code: Select all

<?php

error_reporting(E_ALL);
ini_set('display_errors', true);

$link = mysql_connect('localhost', 'root', 'root');
    $db = mysql_select_db('jobs4alltrades', $link);



	if(isset($_POST['loginid'])) {
		$insert = "INSERT INTO members ('loginid', 'password', 'firstname', 'surname', 'email', 'trade', 'address1', 'address2', 'address3', 'address4', 'postzip', 'country', 'yearsexp', 'about') VALUES ('".$_POST['loginid']."', '".$_POST['password']."', '".$_POST['firstname']."', '".$_POST['surname']."', '".$_POST['email']."', '".$_POST['trade']."', '".$_POST['address1']."', '".$_POST['address2']."', '".$_POST['address3']."','".$_POST['address4']."', '".$_POST['postzip']."', '".$_POST['country']."','".$_POST['yearsexp']."','".$_POST['about']."')";
		$result = mysql_query($insert, $link);
				
				if(!$link){
					echo "problem linking to mysql";
					
				}
				
				if(!$db){
					echo "prob with db name";
					exit;
				}
				
				if (!$result) {
					echo "Your details was succesfully added to our database. Use the navigation bar above to take you where you want to go next<br><br>";
				}
				
				
				else ($result) {
					echo "Nope, not this time";
				}
	}

 $QUERY = "SELECT 'loginid', 'password' FROM members";

    $RESULT = mysql_query($QUERY);

    

    if(mysql_num_rows($RESULT) > 0) {

        echo "<p>Rows in table: <ol>";

        while($row = mysql_fetch_assoc($RESULT)) {

            echo "<li>". $row['loginid'] ." = ". $row['password'] ."</li>";

        }

        echo "</ol></p>";

    } 

    else {

        echo "<p>No rows exist in the table</p>";

    }




 ?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

create a new file, and put

Code: Select all

phpinfo();
in it, it should should the path to the correct php.ini

You might also consider indenting your code properly ;)
toby_c500
Forum Commoner
Posts: 50
Joined: Fri May 11, 2007 11:29 am
Location: Leeds, England

Post by toby_c500 »

Thanks Jcart.

i have sorted the ini file and got a bit neater. I have done that much copy and pasting and to try to sort this out. It is slowly sinking in. Can you tell I am not a natural programer???

Still nothing as I enter the PHP file. What the hell am I doing wrong?

I hope this is easier to read...

Code: Select all

<?php

error_reporting(E_ALL);
ini_set('display_errors', true);

$link = mysql_connect('localhost', 'root', 'root');
$db = mysql_select_db('jobs4alltrades', $link);


if(isset($_POST['loginid'])) {
	$insert = "INSERT INTO members ('loginid', 'password', 'firstname', 'surname', 'email',
									'trade', 'address1', 'address2', 'address3', 'address4',
									'postzip', 'country', 'yearsexp', 'about')
				VALUES ('".$_POST['loginid']."', '".$_POST['password']."', '".$_POST['firstname']."',
						'".$_POST['surname']."', '".$_POST['email']."', '".$_POST['trade']."', '".$_POST['address1']."',
						'".$_POST['address2']."', '".$_POST['address3']."','".$_POST['address4']."', '".$_POST['postzip']."',
						'".$_POST['country']."','".$_POST['yearsexp']."','".$_POST['about']."')";
	
	
$result = mysql_query($insert, $link);
				
		if(!$link){
			echo "problem linking to mysql";
			}
				
		if(!$db){
			echo "prob with db name";
			exit;
			}
				
		if (!$result) {
			echo "Your details was succesfully added to our database. Use the navigation bar above
				  to take you where you want to go next<br><br>";
			}
				
				
		else ($result) {
			echo "Nope, not this time";
			}
	}

 $QUERY = "SELECT 'loginid', 'password' FROM members";

 $RESULT = mysql_query($QUERY);

    if(mysql_num_rows($RESULT) > 0) {
        echo "<p>Rows in table: <ol>";

        while($row = mysql_fetch_assoc($RESULT)) {
			echo "<li>". $row['loginid'] ." = ". $row['password'] ."</li>";
		 }
		echo "</ol></p>";

   	} 
    else {
		echo "<p>No rows exist in the table</p>";
	}




 ?>
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Jcart wrote:It would be better if you turned display_errors to ON in your php.ini and restarted your server, so we could pinpoint the error.
Did you restart apache?
toby_c500
Forum Commoner
Posts: 50
Joined: Fri May 11, 2007 11:29 am
Location: Leeds, England

Post by toby_c500 »

Sorry, yep. You where right about the newbies.

Parse error: syntax error, unexpected '{' in /Applications/MAMP/htdocs/register.php on line 44

which is the else:


else ($result) {
echo "Nope, not this time";
}
User avatar
Ollie Saunders
DevNet Master
Posts: 3179
Joined: Tue May 24, 2005 6:01 pm
Location: UK

Post by Ollie Saunders »

Code: Select all

else { /* code */ }
or

Code: Select all

else if ($condition) { /* code */ }
not

Code: Select all

else ($condition) { /* code */ }
Post Reply