Jump to content

Which is the best method?


dmccabe

Recommended Posts

When inserting values in to a database from a POST form is it better to set the POST results to variables or to just use the $_POST['blah'] direct in the SQL statement?

 

ie:

$name = $_POST['name'];
$tel = $_POST['tel'];
$insert = "INSERT INTO `tbl_purchaseorders` (`name`, `tel`) VALUES (`$name`, `$tel`)";
[code]

or

[code]

$insert = "INSERT INTO `tbl_purchaseorders` (`name`,`tel`) VALUES (`$_POST['name']`,`$_POST['tel']`)";

 

?[/code][/code]

Link to comment
https://forums.phpfreaks.com/topic/98345-which-is-the-best-method/
Share on other sites

BlueSky is correct about sanitizing te user-inputted variables.

 

As far as your question goes.... assigning the $_POST array to variables is neater, but less efficient. It really doesn't cause a huge performance hit, unless the code is getting executed hundreds of times a second (in a loop, or extremely traffic-heavy environment).

 

Do whatever best suits your needs. It makes for prettier code, but I wouldn't suggest doing it in a loop or traffic-heavy environment.

Thanks again, so I how would I use the mysql_real_escape_string() in this instance?

 

Like this:

$insert = "INSERT INTO `tbl_purchaseorders` (`name`,`tel`) VALUES ("mysql_real_escape_string($_POST['name']),mysql_real_escape_string($_POST['tel'])")";

 

like that?

 


$name = mysql_real_escape_string($_POST['name']);
$tel = mysql_real_escape_string($_POST['tel']);

$insert = "INSERT INTO `tbl_purchaseorders` (`name`,`tel`) VALUES ('$name', '$tel')"

 

or alternately

 


$insert = "INSERT INTO `tbl_purchaseorders` (`name`,`tel`) VALUES ('". mysql_real_escape_string($_POST['name']) ."', '
". mysql_real_escape_string($_POST['tel']) ."')"

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.