Forum Moderators: coopster

Message Too Old, No Replies

Php form to Mysql database woes

Please help one newbie

         

Phobia1

6:34 pm on Sep 26, 2008 (gmt 0)

10+ Year Member



Hi Guys.
I know that this must have been asked a 1000 times and I tried to find the answer before posting. Perhaps all here already have their stuff working:-)
All I want to do is add the data to my mysql database.
html>
<HEAD>
<title>New Data Input </title>
</head>
<body bgcolor="#FFFFFF">
<center>
<h1>New Data Input </h1>
<form method=post action="submittedinfo.php">

<table>
<tr><td colspan="2"><font SIZE="+0" face="verdana"></td></tr>

<!-- creating the name/id that will be associated with $ID etc-->
<tr><td align="right"><font SIZE="+0" face="verdana">Property ID <input type="text" name="id" id="id"></td></tr>
<tr><td align="right"><font SIZE="+0" face="verdana">Portfolio <input type="text" Portfolio="name" id="Portfolio"></td></tr>
<tr><td align="right"><font SIZE="+0" face="verdana">Town <input type="text" name="Town" id="Town"></td></tr>
<tr><td align="right"><font SIZE="+0" face="verdana">Price <input type="text" name="Price" id="Price"></td></tr>
<tr><td align="right"><font SIZE="+0" face="verdana">Location <input type="text" name="Location" id="Location"></td></tr>
<tr><td align="right"><font SIZE="+0" face="verdana">Size in sqm <input type="text" name="Size" id="Size"></td></tr>
<tr><td align="right"><font SIZE="+0" face="verdana">Condition <input type="text" name="Condition" id="Condition"></td></tr>
<tr><td align="right"><font SIZE="+0" face="verdana">Plotsize <input type="text" name="Plotsize" id="Plotsize"></td></tr>
<tr><td align="right"><font SIZE="+0" face="verdana">Road Access <input type="text" name="Roadaccess" id="Roadaccess"></td></tr>
<tr><td align="right"><font SIZE="+0" face="verdana">Electricity <input type="text" name="Electricity" id="Electricity"></td></tr>
<tr><td align="right"><font SIZE="+0" face="verdana">Water inside <input type="text" name="Water" id="Water"></td></tr>
<tr><td align="right"><font SIZE="+0" face="verdana">Garage <input type="text" name="Garage" id="Garage"></td></tr>
<tr><td align="right"><font SIZE="+0" face="verdana">No. of Bedrooms <input type="text" name="Bedrooms" id="Bedrooms"></td></tr>
<tr><td align="right"><font SIZE="+0" face="verdana">Kitchen <input type="text" name="Kitchen" id="Kitchen"></td></tr>
<tr><td align="right"><font SIZE="+0" face="verdana">WC <input type="text" name="WC" id="WC"></td></tr>
<tr><td align="right"><font SIZE="+0" face="verdana">Picture1 <input type="text" name="pic1" id="pic1"></td></tr>
<tr><td align="right"><font SIZE="+0" face="verdana"><input type="submit" value="ADD New Data"></td></tr>

</table>
</form>
</body>
</html>

and here is the submittedinfo.php
<?
require("./resources/globals.php") ;
require("./resources/common.php");
require("./resources/foots.php");

//Check data

// Connect to the Database
if (!($link=mysql_connect($location,$userName,$password) or die (mysql_error()))) {

DisplayErrMsg(sprintf("error connecting to host %s, by user %s",
$location, $userName)) ;
exit() ;
}

// Select the Database
if (!mysql_select_db($dbname, $link)) {
DisplayErrMsg(sprintf("Error in selecting %s database", $dbname)) ;
DisplayErrMsg(sprintf("error:%d %s", mysql_errno($link), mysql_error($link))) ;
exit() ;
}

// Execute the Statement
if (!($result =mysql_query($selectStmt, $link))) { //where Detail =?
DisplayErrMsg(sprintf("Error in executing %s stmt", $selectStmt)) ;
DisplayErrMsg(sprintf("error:%d %s", mysql_errno($link), mysql_error($link))) ;
exit() ;
}
//$result = mysql_query("select * from Garant");

$sqlquery = "INSERT INTO Garant (id, Portfolio, Town, Price, Location, Size, Condition, Plotsize, Roadaccess, Electricity,
Water, garage, Bedrooms, Kitchen, WC, pic1) VALUES('$_POST[id]','$_POST[Portfolio]','$_POST[Town]','$_POST[Price]','$_POST[Location]','$_POST[Size]','$_POST[Condition]',$_POST[Plotsize]', $_POST[Roadaccess]', $_POST[Electricity]', $_POST[Water]', $_POST[garage]', $_POST[Bedrooms]', $_POST[Kitchen]', $_POST[WC]', $_POST[pic1])";

//Tell mySQL to insert the values from the form into the databse coresponding with the form
$results = mysql_query($sqlquery); //Query the results
mysql_close();
echo "
<html>
<head>
<title> PHP and MySQL </title>
</head>
<body>
<center>
<table border='0' width='500'>
<tr>";
echo " <td>
<font face='verdana' size='+0'>
<center>
<p>You Just Entered This Information Into the Database</p>
</center>";

//display the information that user submitted in the previous form
echo " <blockquote>
<center>
<p>
ID : $id </p> <p>$Town : $Location </p> <p>$Price : $Bedrooms <p> Portfolio </P
</p>
</center>
</blockquote>
</td>
</tr>
</table>
</center>
</body>
</html>";
?>

best regards
Frederick

cameraman

7:24 pm on Sep 26, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Welcome to Webmaster World, Phobia1.

Here:
// Execute the Statement
if (!($result =mysql_query($selectStmt, $link))) { //where Detail =?
DisplayErrMsg(sprintf("Error in executing %s stmt", $selectStmt)) ;
DisplayErrMsg(sprintf("error:%d %s", mysql_errno($link), mysql_error($link))) ;
exit() ;
}
That's not doing much for you, and where is $selectStmt defined?

Add this line here:
//Tell mySQL to insert the values from the form into the databse coresponding with the form
$results = mysql_query($sqlquery); //Query the results
if(!$results) echo mysql_error();

Or you can integrate it into your error function.

Phobia1

7:10 pm on Sep 27, 2008 (gmt 0)

10+ Year Member



Hi, yes, I kind of understand, and will give it roll later.
Thank you very much for the reply.
F

Phobia1

6:51 pm on Sep 30, 2008 (gmt 0)

10+ Year Member



I tried your suggestion but still cant get it to work.The values from thr form are being transferred but not placed into the mysql database. Could you give ne another direction to follow?
best
F

AlexK

7:08 pm on Sep 30, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Hi Phobia1

Put the following lines in just before the point where it says "

// Execute the Statement
" & report back what appears on screen:

echo "<pre>";
echo "\$selectStmt=$selectStmt";
echo "</pre>";
exit;

grallis

10:20 pm on Sep 30, 2008 (gmt 0)

10+ Year Member



Hi Phobia1 -

I use this syntax for inserting ... helps me look at it a little more cleanly I think ...

INSERT INTO
Garant
SET
id = '".$_POST['id']."',
Portfolio = '".$_POST['Portfolio']."',
Town = '".$_POST[Town]."',
etc ...

cameraman

9:38 am on Oct 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In every field after $_POST['Condition'] you left off the leading quote:
,'$_POST[Plotsize]', '$_POST[Roadaccess]', '$_POST[Electricity]',...
I don't understand why the echo I posted didn't indicate the problem.

The concatenation that grallis suggests is personal preference. If you structure it the way that you have, you should surround array references with braces:
VALUES('{$_POST[id]}','{$_POST[Portfolio]}',...

Phobia1

7:53 pm on Oct 1, 2008 (gmt 0)

10+ Year Member



HI and thanks to all that try to clarify my thinking:-)
I have tried all of the below and get the same answer from all.

$sqlquery = "INSERT INTO
Garant
SET
id = '".$_POST['id']."',
Portfolio = '".$_POST['Portfolio']."',
Town = '".$_POST[Town]."',
Location = '".$_POST['Location']."';

$sqlquery = "INSERT INTO
Garant
SET
id = '{$_POST['id']}',
Portfolio = '{$_POST['Portfolio']}',
Town = '{$_POST[Town])',
Location = '{$_POST['Location']}';

$sqlquery = "INSERT INTO Garant (id, Town, Portfolio, Price, Location, Size, Condition,bedrooms)
VALUES('$_POST['id']','$_POST['Town']','$_POST['Portfolio']','$_POST['price']','$_POST['Location']','$_POST['Size']','$_POST['Condiion']','$_POST['bedrooms']')";
...........
Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home/public_html/submittedinfo.php on line 55

Regards
Fred

StoutFiles

9:05 pm on Oct 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



<tr><td align="right"><font SIZE="+0" face="verdana">Portfolio <input type="text" Portfolio="name" id="Portfolio"></td></tr>

Switch this to name="Portfolio".

cameraman

9:24 pm on Oct 1, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



$sqlquery = "INSERT INTO
Garant
SET
id = '".$_POST['id']."',
Portfolio = '".$_POST['Portfolio']."',
Town = '".$_POST[Town]."',
Location = '".$_POST['Location']."'"; // Missing closing quote

$sqlquery = "INSERT INTO
Garant
SET
id = '{$_POST['id']}',
Portfolio = '{$_POST['Portfolio']}',
Town = '{$_POST[Town]}',
Location = '{$_POST['Location']}'";// You had a close parenthesis instead of a close brace on town and missing closing quote

$sqlquery = "INSERT INTO Garant (id, Town, Portfolio, Price, Location, Size, Condition,bedrooms)
VALUES('{$_POST['id']}','{$_POST['Town']}','{$_POST['Portfolio']}', '{$_POST['price']}','{$_POST['Location']}','{$_POST['Size']}', '{$_POST['Condiion']}','{$_POST['bedrooms']}')";

Phobia1

6:04 pm on Oct 2, 2008 (gmt 0)

10+ Year Member



Hi Guys
Agian many thanks for the replies. However I still can't get data into the MYSQL database. The submittedinfo.php now reports that data has been entered so from the form to submittedinfo.php
the data is correct but nothing goes actually into Garant.
Greetings from Bulgaria

StoutFiles

6:17 pm on Oct 2, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Have you tried breaking down the script some?

Test small parts of the script so you can pinpoint errors. Trying the whole script at once and having an error to find is much harder than just testing small parts at a time.

Phobia1

6:49 pm on Oct 2, 2008 (gmt 0)

10+ Year Member



Hi, yes. I have taken it line by line. Can enter data manually on the server but well, thats not the required answer. The actual connection script I have been using for some time and I can access the database from my site. As it stands I just can't see why it desn't work but then again, what do i know. A, new to PHP/Mysql but have for the time being only simple need of the power. Best F

[edited by: coopster at 7:03 pm (utc) on Oct. 2, 2008]
[edit reason] no urls please TOS [webmasterworld.com] [/edit]

Phobia1

7:14 pm on Oct 2, 2008 (gmt 0)

10+ Year Member



eeeeeeeeeeeeeeeeeeeeeeeeeeeeeeeee
Talk about being stupid........... id should be ID arrrrrrrrrrrrrrhgggggggggggggghh

$sqlquery = "INSERT INTO Garant (ID, Town, Portfolio, Price, Location, Size, Condition,bedrooms)
VALUES('{$_POST['ID']}','{$_POST['Town']}','{$_POST['Portfolio']}', '{$_POST['price']}','{$_POST['Location']}','{$_POST['Size']}', '{$_POST['Condiion']}','{$_POST['bedrooms']}')";

Many thanks for all your patient help
Regards
F