PDA

View Full Version : Help with php mysql and html forms



shalli
12-08-2008, 11:39 AM
Hi

Wondering if anyone can assist me with php mysql and html forms errors that I am getting

I am trying to get this page to print what the user select in the drop down menu 'areaName'. At the moment the print command does nothing at all.:confused::confused:

Any help will be much appreciated

thanks in advance

shalli

<?php

include("connection/dbconnect.php");

// STEP 1: Connect to the database:
mysql_select_db("$database_TestConnect") or die ("Unable to Connect to the database");

//STEP 2: See if there is a table that you require...
$table = "propertyValues";
if( mysql_num_rows( mysql_query("SHOW TABLES LIKE '".$table."'")))
{
//print("Found Table. Ready to proceed!<br>");
}else{
print("Table was not found, I will create it now.<br>");

//STEP 3: Create the table:
$sql = "CREATE TABLE `propertyValues` (
`propertyVal_id` INT NOT NULL AUTO_INCREMENT ,
`propertyTitle` VARCHAR( 64 ) NOT NULL ,
`address` VARCHAR( 300 ) NOT NULL ,
`city` VARCHAR( 64 ) NOT NULL ,
`county` VARCHAR( 64 ) NOT NULL ,
`postcode` VARCHAR( 7 ) NOT NULL,
`propertyPrice` INT NOT NULL ,
INDEX ( `propertyVal_id` ) )";

mysql_query($sql) or die(mysql_error()) ;
print("Table Creation Complete. . .<br>");
}

if(isset($function) && $function=="add"){

print("This is the county name selected - '$areaName'<br>");

$sqlInsert = "INSERT INTO `propertyValues` (
`propertyTitle`,
`address`,
`city`,
`county`,
`postcode`,
`propertyPrice`)
VALUES ( '$propertyTitle', '$address', '$city', '$areaName', '$postcode', $propertyPrice )";

//print("$sqlInsert<br>");
mysql_query($sqlInsert) or die(mysql_error());
print("Data OK!!");

}

$regSQL = "SELECT `areaName` FROM area";
$resultQuery = mysql_query($regSQL) or die(mysql_error());


?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>PHP/MySQL Programming Course Part 2</title>
</head>

<body>
<form id="enterForm" name="dateEnter" method="post" action="index.php?function=add">
Enter Property Name
<input type="text" name="propertyTitle" id="propertyTitle" />
<br />
Enter Property Address
<input type="text" name="address" id="address" />
<br />
Enter City
<input type="text" name="city" id="city"/>
<br />
Select County
<select name="areaName" id="areaName" />
<? while ( $myrow = mysql_fetch_array($resultQuery) ) { ?>

<option value="<? echo $myrow['areaName']; ?>"><? echo $myrow['areaName']; ?></option>

<? } ?>
</select><br />
Enter PostCode
<input type="text" name="postcode" id="postcode" />
<br />
Enter Property Price
<input type="text" name="propertyPrice" id="propertyPrice" />
<br />
<input type="submit" name="addData" id="addData" value="Ok" />
</form>
</body>
</html>

pghcollectibles
12-09-2008, 11:16 AM
im thinking it is because you have register globals turned off in your php.ini file (http://http://www.hostmonsterforum.com/showthread.php?t=1293&highlight=enable%2Cregister%2Cglobal).

AS YOU SHOULD

you will need to add a line of code to the beginning of the script ie: change:


<?php

include("connection/dbconnect.php");
to this:


<?php
$areaName=$_POST["areaName"];
include("connection/dbconnect.php");
but you should perform some type of filtering as well. you could find info on that from php.net

shalli
12-09-2008, 02:25 PM
thanks pghcollectibles for all your help!!!:)

It seems to work a treat :)

thanks again

shalli
12-09-2008, 03:40 PM
Sorry to bother you again pghcollectibles but it now seems that the form wont update the database?

Completely confused?? :confused::confused::confused:

Any suggestions

Thanks for your time

DataMan
12-10-2008, 03:20 AM
While not a specific answer, allow me to recommend that you get into the habit of running diagnostics when programs are not functioning correctly. In your case, insert print or echo statements strategically at the different portions of your forms processing program. For example:

$sq1="insert into mytable (f1, f2) values ('$f1', '$f2')";
print '<br>sq1='.$sq1;
$res=mysql_query($sq1);
$check=mysql_affected_rows();
print '<br>number of rows inserted is'.$check;

As you isolate parts that are working correctly, turn off your de-bugging diagnostics (print statements).

Hope this is of some benefit.

DataMan

pghcollectibles
12-10-2008, 06:20 AM
you will need to add
$variable_name=$_POST["variable_name"];for each input in your form that you are sending

pghcollectibles
12-10-2008, 06:27 AM
you have an insert sql command and may want to have conditions for checking supplied input and comparing it then using insert or update depending on what is best for the situation look here (http://www.w3schools.com/php/php_mysql_intro.asp) for some info on mysql

are you getting an error message?