PDA

View Full Version : incorporating variables into SQL


Gary23
10-23-2006, 09:37 PM
Hi,

If a user has input a bunch of data into a form on my site, how can I insert this into a MYSQL table I have set up?

I know about INSERT INTO statements etc, but the problem is that I can't quite work out how to drop variables into this statement. Obviously once the page is generated, any pre-defined SQL queries within that page are static.

Do I pass the variables in a URL to another page which then generates the INSERT INTO statement and executes it?

Thanks,
Gary

Gary23
10-25-2006, 10:57 PM
Can none of you brainiacs help me? This must be a first!

rafiki
10-25-2006, 11:41 PM
sorry just read your post and "couldnt drop variables into this statement" not very clear
the variables being set by a user on your website?, on the same page as the SQL statement? explain a little clearer

Gary23
10-31-2006, 08:48 PM
Ok, the user has input some data into a number of text fields on my page. I now want to drop the values of these fields into an SQL statement:

$query4="
INSERT INTO gigs (venue_id, gig_date, specials_id, tbc)
VALUES ()
";


And use the statement to update my database.


Any clearer?

shanstafari
10-31-2006, 08:56 PM
You can just write in the $_GET[value1], $_GET[value2], etc. variables from the form right into your query. Be sure not to put single quotes in the GET array's key - so you wouldn't write $_GET['value1'] - that will break your query.

For example:
$query4="
INSERT INTO gigs (venue_id, gig_date, specials_id, tbc)
VALUES ('$_GET[venue_id]', '$_GET[gig_date]', '$_GET[specials_id]', '$_GET[tbc]')
";

This is, of course, if the names of your form's inputs are venue_id, gig_date, specials_id, and tbc. (If the form method is post, replace GET with POST). Is that what you mean?

littlejones
11-01-2006, 07:52 AM
shanstafari is right, as long as they have quotation marks around the variable name you can just write the name as you would normally.

So for instance this would work.



$orderBy = 'firstName';

$query = "SELECT * FROM people ORDER BY '$orderBy'";
$result = mysql_query($query);



providing of course 'firstName' is a field in your database.