Jump to content

Assign Variables - List HELP!!


biff423

Recommended Posts

Info collected from form using hidden fields with values of 1 for 'yes' and 0 for 'no':

$angus=$_POST['angus'];

$brangus=$_POST['brangus'];

$charolais=$_POST['charolais'];

 

$data="$angus$brangus$charolais";      so $data should equal 100

 

include("addtolistx2.php");

 

 

_____________________________________

Code in include addtolistx2.php:

list($angus,$brangus,$charolais) = $data

 

$sql = mysql_query("INSERT INTO list (angus,brangus,charolais) VALUES ('$angus','$brangus','$charolais')")  or die (mysql_error());

 

so $angus should be 1, $brangus should be 0 and $charolais should be 0 - and those values should be entered into the proper columns in database

BUT - the values are not showing up in the database.

 

Please help - I believe the problem is with unpacking the list and assigning variables but I am not sure how to correct.

 

Thank you Marci - please be kind - sorta a newbie :shrug:

Link to comment
https://forums.phpfreaks.com/topic/175223-assign-variables-list-help/
Share on other sites

There is no need to "pack" and "unpack" your variables when using an include.... especially when you're unpacking them incorrectly

 

$data is being defined as a string, so you'd need to convert it to an array before using list.

list($angus,$brangus,$charolais) = str_split($data);

 

Hi biff423,

 

Also, as you are entering POSTed data directly into a MySQL query you need to do some validation/sanitisation to avoid possible MySQL Injection attacks.

 

Very basic protection would be to use mysql_real_escape_string(), i.e.:

 

$angus=mysql_real_escape_string($_POST['angus']);
$brangus=mysql_real_escape_string($_POST['brangus']);
$charolais=mysql_real_escape_string($_POST['charolais']);

 

Make sure you have a database connection before doing this otherwise it won't work.  Have a read of Daniel's excellent PHP security tutorial here for further information http://www.phpfreaks.com/tutorial/php-security

 

Hope this helps.

Thank you to all - total whoops on my part - I used the code from another form and didn't really think it through - I took out teh $data in the first file and the list () = $data in the second file and it is all working fine! Thanks for your help!

 

Marci

 

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.