Forum Moderators: coopster

Message Too Old, No Replies

Retrieve form data and insert into MySQL database

No new records updated in database

         

webwatch

3:46 pm on Jul 29, 2004 (gmt 0)

10+ Year Member



I am new to PHP and having a difficult time starting off. All I am trying to do is use a form and insert the data from the form to mysql db. The code below shows the data is retreived and displayed properly but does not add any new records to the "Test" table in the database.

<?php
/* Start of PHP3 Script */
/* Data of SQL-server */
$server="server";/* Address of database server*/
$user="user"; /* Database username */
$password="password"; /* Database Password*/
$database="database"; /* name of database */
$table="Test"; /* Name of table, you can select that */

$county=$_POST['county'];
$township=$_POST['township'];

/* Accessing SQL-server */
MYSQL_CONNECT($server, $user, $password) or die ( "<H3>Server unreachable</H3>");
MYSQL_SELECT_DB($database) or die ( "<H3>Database non existent</H3>");

/* Entering the values */

$sql="select * from Test where County = ".$_POST
['county']. " and Township = ".$_POST['township'].
"";
echo $sql;

$query = "insert INTO $table (county, township) VALUES ('$county', '$township')";
mysql_query($query);

?>

RussellC

4:04 pm on Jul 29, 2004 (gmt 0)

10+ Year Member



$sql = "SELECT * FROM Test WHERE County = '$_POST
[county]' AND Township = '$_POST[township]'";

Try that instead, that is one of the problems...you leaving out alot of code..is that on purpose?

After looking at it more, what are you using a SELECT statement? If you only need to INSERT $_POST["county"] and $_POST["township"] into the database forget the SELECT statement and just use

"INSERT INTO Test SET County = '$_POST[county]', Township = '$_POST[township]'"

webwatch

4:28 pm on Jul 29, 2004 (gmt 0)

10+ Year Member



Thanks for answering the question so quickly. I went back and ran the code and it is now working properly. For some reason the db is now updating the records and I did not change any of the code.

In reference to the select statement, I believe you are correct, I will try you suggestion and see if that works as well.

I do have one question, in other forums I have read that you do not have to use the $Post command if the register_globals are turned on, is this correct?

Birdman

4:48 pm on Jul 29, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, that is correct. It does create some security issues though.I would stick to the long syntax ($_POST[]), or turn register_globals off.

If you turn them off, you can then use:

extract($_POST);

..to get them back into their short form and avoid the longer syntax.