PHP vs MySQL vs Forms. HELP ME!!!

Questions about the MySQL, PostgreSQL, and most other databases, as well as using it with PHP can be asked here.

Moderator: General Moderators

Post Reply
User avatar
moniarde
Forum Newbie
Posts: 20
Joined: Thu Feb 06, 2003 12:45 am
Location: Perth, Western Australia

PHP vs MySQL vs Forms. HELP ME!!!

Post by moniarde »

I don't know if I'm crazy 8O , but the tutorials I read make what I need to do so much simpler than the way I am doing it. I am trying to post information from a form back into my mysql db - theoretically pretty simple. Here's what I have so far:

Code: Select all

$sql = "INSERT INTO test SET " .
	"test_id='4', " .
	"test1='{$_POSTї"form_test1"]}', " .
	"test2='{$_POSTї"form_test2"]}'";
$result = mysql_query($sql);
:where "form_test1 & 2" are text inputs in the form. This does work, but that's not the point.

Everytime I want to refer to a value posted from the form, I have to use $_POST. But all the tutorials I read tell me I can refer to input as if it were already a variable. Why can't I? Is it something to do with the fact that I am using PHP4, and the tutorials are written for PHP3?
Caroline
Forum Commoner
Posts: 25
Joined: Thu Jan 30, 2003 1:38 pm

Hrmmm

Post by Caroline »

In PHP4, form input name DO be a variable and you can insert it right away

like this

Code: Select all

mysql_query("INSERT INTO test WHERE test_id = 4 VALUES('form_test1','form_test2')");
Check with your host

Or move your site here
Rob the R
Forum Contributor
Posts: 128
Joined: Wed Nov 06, 2002 2:25 pm
Location: Houston

Post by Rob the R »

This is actually dependent on the register_globals directive being set in your PHP environment. It is generally more secure to keep register_globals off so that you have to refer to the POST, GET, and COOKIE variables from within the array, like you have observed.

If it's really important for you to be able to refer to the variable without using the $_POST array, you'll have to turn the register_globals directive on.
User avatar
moniarde
Forum Newbie
Posts: 20
Joined: Thu Feb 06, 2003 12:45 am
Location: Perth, Western Australia

PHP4 vs 3

Post by moniarde »

So is this problem just a matter of the tutorials being written for PHP3, while I'm using PHP4?

Is there another way to globablly state that all form information gathered should automatically be placed into it's own variable according to the name of the form input ie, send all the information to an array in one relatively small piece of code?
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

The tutorials are written for the fact that before PHP version 4.2, register_globals was on by default - now it is deprecated and in some future release will not exist any more and is currently off by default.

To get all of the information from the $_POST array to be placed in variables of the same name as the form inputs they came from you can simply use extract():

Code: Select all

extract($_POST);
and then $_POST['formvar1'] can be refered to as $formvar1.

One thing to note - you really should do some validation of the form data - e.g. make sure what should be numbers really are - before putting data into the database otherwise you could have problems with malicious and/or stupid users messing up your data.

Mac
Post Reply