Inserting the actual string $variable into the db

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

Moderator: General Moderators

fariquzeli
Forum Contributor
Posts: 144
Joined: Mon Jun 24, 2002 9:16 am
Location: Chicago
Contact:

Inserting the actual string $variable into the db

Post 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.
chris12295
Forum Contributor
Posts: 113
Joined: Sun Jun 09, 2002 10:28 pm
Location: USA
Contact:

Post by chris12295 »

Are you using mysql?
fariquzeli
Forum Contributor
Posts: 144
Joined: Mon Jun 24, 2002 9:16 am
Location: Chicago
Contact:

Post by fariquzeli »

yes.
chris12295
Forum Contributor
Posts: 113
Joined: Sun Jun 09, 2002 10:28 pm
Location: USA
Contact:

Post 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.
fariquzeli
Forum Contributor
Posts: 144
Joined: Mon Jun 24, 2002 9:16 am
Location: Chicago
Contact:

Post 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?
User avatar
hob_goblin
Forum Regular
Posts: 978
Joined: Sun Apr 28, 2002 9:53 pm
Contact:

Post 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
chris12295
Forum Contributor
Posts: 113
Joined: Sun Jun 09, 2002 10:28 pm
Location: USA
Contact:

Post by chris12295 »

The values can be any variable be it a FORM POST or GET variable or a variable declared in your script.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
fariquzeli
Forum Contributor
Posts: 144
Joined: Mon Jun 24, 2002 9:16 am
Location: Chicago
Contact:

Post 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
chris12295
Forum Contributor
Posts: 113
Joined: Sun Jun 09, 2002 10:28 pm
Location: USA
Contact:

Post 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.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post 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
fariquzeli
Forum Contributor
Posts: 144
Joined: Mon Jun 24, 2002 9:16 am
Location: Chicago
Contact:

Post 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.
calebsg
Forum Commoner
Posts: 28
Joined: Tue Jun 18, 2002 10:41 am

Post by calebsg »

maybe I'm missing something but i thought in php you can't have a variable that starts with a number?

Caleb
fariquzeli
Forum Contributor
Posts: 144
Joined: Mon Jun 24, 2002 9:16 am
Location: Chicago
Contact:

Post by fariquzeli »

i believe that may be right.
User avatar
mikeq
Forum Regular
Posts: 512
Joined: Fri May 03, 2002 3:33 am
Location: Edinburgh, Scotland

Post 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."
Post Reply