arrrgghhh - php & mysql not playing nice together..!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

User avatar
mydimension
Moderator
Posts: 531
Joined: Tue Apr 23, 2002 6:00 pm
Location: Lowell, MA USA
Contact:

Post by mydimension »

ok, in edit.php change your SQL statement to read:
$result = mysql_query("SELECT * FROM $db_table WHERE id=$_GET[editid]"); do this for forward compatability and security sake.

in update.php change your SQL statement to:
$result = mysql_query("UPDATE $db_table SET loaddate=$_POST[loaddate], origcity=$_POST[origcity], origstate=$_POST[origstate], destcity=$_POST[destcity], deststate=$_POST[deststate] WHERE id=$_POST[ID]");

what i think was happening is that update.php was no longer within the same scope as index.php. since $_POST is super-global and is present everywhere that problem should be solved.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

Just want to add a couple of adjustments to mydimension's code:

Code: Select all

$sql = "SELECT * FROM $db_table WHERE id=".$_GETї'editid'];
$result = mysql_query($sql) or die(mysql_error().'<p>'.$sql.'</p>');
and

Code: Select all

$sql = "UPDATE $db_table SET loaddate='".$_POSTї'loaddate']."', origcity='".$_POSTї'origcity']."', origstate='".$_POSTї'origstate']."', destcity='".$_POSTї'destcity']."', deststate='".$_POSTї'deststate']."' WHERE id=".$_POSTї'ID'];
$result = mysql_query($sql) or die(mysql_error().'<p>'.$sql.'</p>');

The reason I've done this is firstly because you must quote your array element names so $_POST['ID'] not $_POST[ID] because otherwise PHP does extra work looking for a constant called ID before it realises there isn't one and decides you probably meant an element name 'ID'. Secondly keep the SQL statement in a variable of it's own instead of just writing it straight into the mysql_result() function so that you can echo it out to make sure that all the variables are being passed properly. Finally always have some form of error handling for your MySQL commands - using mysql_error() may not be appropriate when you put the site into production but you can easily change the die statements to just read 'or die('There has been an error with the database')'.

Mac
User avatar
DiamondLil
Forum Newbie
Posts: 15
Joined: Fri Oct 04, 2002 11:14 am

Post by DiamondLil »

Good morning.
Just got in and I'm making the changes you suggest. One question, though: is that super-global only available in more recent versions of PHP (My host is running a . release prior to 4.1) ?

-L
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

yes, it was introduced in php 4.1
take a look at http://www.php.net/manual/en/reserved.v ... ables.post
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

For PHP 4.0.6 or lower $_POST can be replaced with $HTTP_POST_VARS (this array will also work in PHP 4.1 up). The main difference between them (aside from one being quicker to type than the other) is that if want to use $HTTP_POST_VARS in a function you need to declare it global but you don't have to do that with $_POST.

Have a read of the manual page volka pointed you towards as it also explains the other pre-defined arrays that are available to you.

Mac
User avatar
DiamondLil
Forum Newbie
Posts: 15
Joined: Fri Oct 04, 2002 11:14 am

Post by DiamondLil »

Thank you twigletmac & mydimension, not just for the proper code but more importantly for the time you took to explain WHY it was the proper code (yes, it's all working now :) .

One last question, though: you mentioned that I had to declare it globally, I didn't do anything over than replace the $_POST w/ the $HTTP_POST_VARS. Why is is working?

volka - I've been, but a lot of the info is still over my head until I start trying to use bits of it. I'll go take another read through, see if more of it starts to sink in now. Thank you.
User avatar
twigletmac
Her Royal Site Adminness
Posts: 5371
Joined: Tue Apr 23, 2002 2:21 am
Location: Essex, UK

Post by twigletmac »

You only have to declare it global in a function for example if you had

Code: Select all

function my_function()
{
    print_r($_POST);
}
If you wanted to change $_POST to $HTTP_POST_VARS you'd have to add another line to your function making $HTTP_POST_VARS global:

Code: Select all

function my_function()
{
    global $HTTP_POST_VARS;
    print_r($HTTP_POST_VARS);
}
It only applies when you are using the arrays in functions that you have written yourself. For more info on user-defined functions:
http://www.php.net/manual/en/functions.php

Mac
Post Reply