Page 1 of 2

Inserting the actual string $variable into the db

Posted: Tue Jul 09, 2002 12:13 pm
by fariquzeli
I have some php that is supposed to add in numbers into a db that were added into a form field on a previous page. The problem is that the form is entering the actual variable names as strings into the db

ex. The field's name is $1_spring, rather than entering the number 7 that was put into that field, it puts in $1_spring. here is the code:

їcode]$sql2="INSERT INTO curriculum SET
polishing_philosophy = '$polishing_philosophy',
teaching_year = '$teaching_year',
teaching_year_semester = '$teaching_year_semester',
polishing_procedure = '$polishing_procedure',
ergonomics_taught= '$ergonomics_taught',
seperate_course = '$seperate_course',
hours_received = '$hours_received',
teaching_importance = '$teaching_importance',
prepared_module = '$prepared_module',
1_fall = '$1_fall',
2_fall = '$2_fall',
3_fall = '$3_fall',
4_fall = '$4_fall',
1_fall2 = '$1_fall2',
2_fall2 = '$2_fall2',
3_fall2 = '$3_fall2',
4_fall2 = '$4_fall2',
1_spring = '$1_spring',
2_spring = '$2_spring',
3_spring = '$3_spring',
4_spring = '$4_spring',
1_spring2 = '$1_spring2',
2_spring2 = '$2_spring2',
3_spring2 = '$3_spring2',
4_spring2 = '$4_spring2',
1_summer = '$1_summer',
2_summer = '$2_summer',
3_summer = '$3_summer',
4_summer = '$4_summer',
1_summer2 = '$1_summer2',
2_summer2 = '$2_summer2',
3_summer2 = '$3_summer2',
4_summer2 = '$4_summer2',
teaching_year2 = '$teaching_year2',
teaching_year_semester2 = '$teaching_year_semester2',
hours_ergonomics_module = '$hours_ergonomics_module'";ї/code]

all of the fields with the odd numbers for names are inputting the $string into the db rather than the number, all the normally named fields are entering info fine.

Posted: Tue Jul 09, 2002 12:16 pm
by chris12295
Are you using mysql?

Posted: Tue Jul 09, 2002 12:20 pm
by fariquzeli
yes.

Posted: Tue Jul 09, 2002 12:27 pm
by chris12295
in MYSQL the syntax for inserting that seems to work for me is

Code: Select all

$sql2 = "INSERT into table (field1, filed2, field3) VALUES('$value1', '$value2', '$value3')";
I have never tried it your way, just give my syntax a try and tell me if that works.

Posted: Tue Jul 09, 2002 12:48 pm
by fariquzeli
I'm not exactly sure if that's the right thing. I want the values to be taken from the form on the previous page, not actually set to values that i want.

is that the same syntax for achieving what I want to do as well?

Posted: Tue Jul 09, 2002 12:49 pm
by hob_goblin
ok the way you're doing it is:

$statement = "thing 'thing'";

so if i were to do:

echo $statement;

it would produce: thing 'thing'

and i assume you're trying to do like mysql_query($sql2);
and in PHP a variable will only be treated as a variable in a string if it is enclosed in double quotes.

so try:

Code: Select all

$sql2=""INSERT INTO curriculum SET 
         polishing_philosophy = '$polishing_philosophy', 
         teaching_year = '$teaching_year', 
         teaching_year_semester = '$teaching_year_semester', 
         polishing_procedure = '$polishing_procedure', 
         ergonomics_taught= '$ergonomics_taught', 
         seperate_course = '$seperate_course', 
         hours_received = '$hours_received', 
         teaching_importance = '$teaching_importance', 
         prepared_module = '$prepared_module', 
         1_fall = '$1_fall', 
         2_fall = '$2_fall', 
         3_fall = '$3_fall', 
         4_fall = '$4_fall', 
         1_fall2 = '$1_fall2', 
         2_fall2 = '$2_fall2', 
         3_fall2 = '$3_fall2', 
         4_fall2 = '$4_fall2', 
         1_spring = '$1_spring', 
         2_spring = '$2_spring', 
         3_spring = '$3_spring', 
         4_spring = '$4_spring', 
         1_spring2 = '$1_spring2', 
         2_spring2 = '$2_spring2', 
         3_spring2 = '$3_spring2', 
         4_spring2 = '$4_spring2', 
         1_summer = '$1_summer', 
         2_summer = '$2_summer', 
         3_summer = '$3_summer', 
         4_summer = '$4_summer', 
         1_summer2 = '$1_summer2', 
         2_summer2 = '$2_summer2', 
         3_summer2 = '$3_summer2', 
         4_summer2 = '$4_summer2', 
         teaching_year2 = '$teaching_year2', 
         teaching_year_semester2 = '$teaching_year_semester2', 
         hours_ergonomics_module = '$hours_ergonomics_module'"";
it SHOULD work

Posted: Tue Jul 09, 2002 1:04 pm
by chris12295
The values can be any variable be it a FORM POST or GET variable or a variable declared in your script.

Posted: Tue Jul 09, 2002 1:37 pm
by twigletmac
Have you tried echoing out all or some of the variables at different points before they get to the SQL query? It could be that something you've done earlier on in the code is causing the problem.

Mac

Posted: Tue Jul 09, 2002 2:14 pm
by fariquzeli
The variables before it are being entered in just fine, that's why the way i have it coded out should work. It's just the ones named like 1_fall etc. that aren't working

they didn't work by adding the "/" either

Posted: Tue Jul 09, 2002 2:18 pm
by chris12295
Just the odd variables arent working? thats an interesting problem, try changing all the variables to $GLOBALS['varName'] or removing the quotes from around the variables.

Posted: Tue Jul 09, 2002 2:23 pm
by twigletmac
Have you echoed out the variables that aren't working before putting them into the SQL query? Do you do something different with them in the code before that you don't do with the others?

Mac

Posted: Tue Jul 09, 2002 2:25 pm
by fariquzeli
nope nothing special being done to them, and i've checked the names of them.

i'll echo em out now though to check.

Posted: Tue Jul 09, 2002 3:04 pm
by calebsg
maybe I'm missing something but i thought in php you can't have a variable that starts with a number?

Caleb

Posted: Tue Jul 09, 2002 3:08 pm
by fariquzeli
i believe that may be right.

Posted: Tue Jul 09, 2002 4:29 pm
by mikeq
Yep, quote from PHP4 Bible

"After the initial '$', variable names must be composed of letters (uppercase or lowercase), digits(0-9) and underscore characters ('_'). Furthermore, the first character after the '$' may not be a number."