Evaluated variables in PHP


 
Thread Tools Search this Thread
Top Forums Programming Evaluated variables in PHP
# 1  
Old 11-13-2010
Evaluated variables in PHP

I'm putting my club's membership database online.
I have a Member table in the database with fields such as FirstName, LastName etc. I have a web form with matching input variables Member_FirstName, Member_LastName that's going to be used for inserting a new member into the database, so I'll be populating an SQL statement such as
Code:
insert Member
(FirstName, LastName .... )
values
("$_POST['Member_FirstName']", "$_POST['Member_LastName']" ...)

My plan was to have a set of intermediate variables
Code:
$Member_FirstName = $_POST['Member_FirstName'];
$Member_LastName = $_POST['Member_LastName'];

so that I could have a pre built SQL statement that would simply substitute the intermediate variables.

OK - that's the background...now the question is, how do I generate the intermediate variables in a loop of column names.
So in pseudo code...
Code:
for each $col (select * from Member)
    $var = "Member_" . $col;                 ($var="MemberFirstName")
    ${$var} = $_POST[$var];                  Create the intermediate variable
                                                This is the problem line!

I've been trying to use 'eval()' but can't get the syntax correct, so any views on correct syntax or a better/alternative way to do this appreciated.

Jerry
# 2  
Old 11-13-2010
eval is evil. You'd spend more code on trying to prevent security problems than anything else, and even then there'd probably be (more than) a few left.

Check the PHP Manual for Variable variables.
This User Gave Thanks to pludi For This Post:
# 3  
Old 11-13-2010
Brilliant - thanks pludi. I thought I'd tried this previously and couldn't get it working, so eval was a clutch at straws. The description and example on the link were invaluable.

Jerry
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

PHP: declared variables, strlen vs isset

greetings, pretty new to php and i think i might be missing some fundamental limitation of isset. i have two php scripts below that are executed by crond, one using --host X and one that does not. and below that are three different attempts at generating a command line that will be executed. the... (8 Replies)
Discussion started by: crimso
8 Replies

2. Shell Programming and Scripting

Passing awk variables to bash variables

Trying to do so echo "111:222:333" |awk -F: '{system("export TESTO=" $2)}'But it doesn't work (2 Replies)
Discussion started by: urello
2 Replies

3. Shell Programming and Scripting

Running a script with multiple variables like 25 variables.

Hi All, i have a requirement where i have to run a script with at least 25 arguements and position of arguements can also change. the unapropriate way is like below. can we achieve this in more good and precise way?? #!/bin/ksh ##script is sample.ksh age=$1 gender=$2 class=$3 . . .... (3 Replies)
Discussion started by: Lakshman_Gupta
3 Replies

4. Shell Programming and Scripting

[PHP] Need help with making variables Global

I have made a script that requires another php script for functions. I need a way so that the required script can read and write the main script's variables. Best Regards, John Wei ---------- Post updated at 08:54 AM ---------- Previous update was at 08:40 AM ---------- Sorry Guys, EDIT: my... (1 Reply)
Discussion started by: johntzwei
1 Replies

5. Shell Programming and Scripting

Passing variables from bash to php-cli

Hello everyone, I've been looking how to pass variables between bash and php-cli in 1 file. So far i got this: #!/bin/bash echo "This is bash" php << EOF <?php echo "This is php\n"; ?> EOF I would now like to be able to pass a variable declared in the bash to the php. I already... (0 Replies)
Discussion started by: robbee
0 Replies

6. Shell Programming and Scripting

Help with passing XML variables to MySQL DB via PHP

Hi everybody, I need the help of the Unix community once again :) I have some code which queries an XML feed and displays the results for me. I would like to enter the XML output in to my database, but I am having trouble passing the variables while INSERTing. Here is my code that I need to... (0 Replies)
Discussion started by: o0110o
0 Replies

7. Shell Programming and Scripting

Variable is not evaluated in awk statement

Dear All, I have one problem in my script, awk statement as 1. it is not evaluate the second variable $stake but the first one $channel is being done. 2.I want to assign the whole awk statement to a variable actual_code which is not being executed in my script. #!/usr/bin/sh echo "Enter... (3 Replies)
Discussion started by: chinmayadalai
3 Replies

8. Programming

How to convert byteArray variables to HexaString variables for Linux?

Hello everybody, I am having problem in converting byte array variables to Hexa String variables for Linux. I have done, converting byte array variables to Hexa String variables for Windows but same function doesn't work for linux. Is there any difference in OS ? The code for Windows is given... (2 Replies)
Discussion started by: ritesh_163
2 Replies

9. UNIX for Dummies Questions & Answers

Assigning evaluated expression to a variable

Hello, Could you please let me know what is the problem here.. 28:var1="SERVER_$j" 29:eval $var1=`grep "^DBSERVER=" "$i" |cut -d"=" -f2` i get this error: syntax error at line 29 : `|' unexpected Thanks for your quick response..This is urgent..pls.. Regards Kaushik (5 Replies)
Discussion started by: kaushikraman
5 Replies

10. Shell Programming and Scripting

how do I make dynamic parameter names? Or get the value of a parameter evaluated twi

Say I write something like the following: var1=1 var2=2 for int in 1 2 do echo "\$var$int" done I want the output to be: 1 2 Instead I get something like: $var1 $var2 (2 Replies)
Discussion started by: Awanka
2 Replies
Login or Register to Ask a Question