PDA

Click to See Complete Forum and Search --> : POST Problem


root2
Apr 9th, 2009, 09:34 PM
Hi guys,

I need some help with the following code. I am sure I have the right variable and checked using


echo "<h1>Post Parameter/s:</h1>";
echo "<pre>";
if($_POST)
print_r($_POST);
else
echo "There are no post parameters.";
echo "</pre>";


so the post comes back as value "transamt"

I wrote the following code to capture the payment info but I can't get it to update MySQL. HELP!!!


$result=mysql_query("SELECT user,money from chatusers WHERE id='$_COOKIE[id]' LIMIT 1");

while($row = mysql_fetch_array($result))

{ $username=$row['user'];$money=$row['money']; }
if (strstr($_POST['transamt'],".") == "")
{
echo $money=$money + $_POST['transamt'].".00";
}
else
{
$money=$money + $_POST['transamt'];
}
$sql="Update chatusers set money=$money where user='$username'";
$res=mysql_query($sql);
mysql_query("insert into payments (ammount, details) values ('".$_POST['transamt']."', '$username')");
echo '<script>window.location="buyminutes.php"</script>';

kows
Apr 10th, 2009, 04:00 AM
this should probably be posted in the PHP forum (http://www.vbforums.com/forumdisplay.php?f=27) and not here.

there are a lot of obvious logical problems with your script ("spaghetti code" -- things that work, but should not be done the way you're doing it); but, none of these should affect your query. the only thing I see wrong with your insert query is that "amount" is misspelled (but it could also be misspelled in your database). you might also want to check and make sure that $_POST['transamt'] is actually a numerical value; you can use is_numeric() for that.

you should try to be more consistent when handling your database, too. your SELECT query selects using an ID, but your UPDATE query updates using the username. then, you store the username (instead of the ID) in the "payments" table. it's definitely not required, but it promotes easier to understand code when debugging.

and finally, you should never use javascript as a redirect method when you can easily do the same thing using a header with PHP:
header("Location: buyminutes.php");
just make sure that you're sending this header before any output is sent to the browser.

Hack
Apr 10th, 2009, 07:27 AM
Moved From The CodeBank

root2
Apr 10th, 2009, 05:12 PM
I appreciate you outlining several observations such as using javascript within the PHP and then you giving me example. I just started coding in PHP so I'm a one month old newbie trying to program in PHP.

I did find the problem it was a mistake in the declaration or obtaining the $_POST. I had this setup as "transamt" and I did not notice the "transamnt". I was missing an "N" right before the T. The developer on the other side decided to abbreviate amount amnt when normally it's amt.

I thank you for outlining these things and will change my code to make it better. Thank you

modpluz
Apr 20th, 2009, 08:07 AM
and finally, you should never use javascript as a redirect method when you can easily do the same thing using a header with PHP:
PHP Code:
header("Location: buyminutes.php");
just make sure that you're sending this header before any output is sent to the browser.


i've always used javascript to redirect(one way or the other i manage to always send output before redirecting).
so, to avoid obvious errors, i always use a self created php function to redirect.

function js_redir($new_page){
echo("<script>\nwindow.location = '$new_page';\n</script>");
}


i know using php to output html can slow down the page, but what can i do?

kows
Apr 20th, 2009, 12:50 PM
that's bad practice; building a website that completely relies on javascript for anything important to the actual function of the website is a bad idea. it's much easier to verify user input before sending any output to the user, and then using PHP to redirect the user when needed.

modpluz
Apr 21st, 2009, 06:36 AM
that's bad practice; building a website that completely relies on javascript for anything important to the actual function of the website is a bad idea. it's much easier to verify user input before sending any output to the user, and then using PHP to redirect the user when needed.

yes i know, but just like i said - i have not found a better way to do this(beside using javascript).

but come to think of it - the support for javascript is increasing accross mordern browsers, but it is client side(that's the one problem i have, depending on the client).

I_Love_My_Vans
Apr 22nd, 2009, 07:20 AM
I would argue the PHP header method is better, using header in PHP does the same job as the JS you are using, and whilst most browsers have JavaScript support some disable it for security reasons, also I am sure using JavaScript to redirect a user will be increasing the number of HTTP requests to the server, which will slow the user down.

To understand why it uses less HTTP request, takre a look at this example. We can assume the user will load the main page, fill in the form and submit it:

JavaScript
accept input -> find error and echos redirect -> page loads -> javascript redirects -> destination page

PHP
accept input -> find error and redirect -> destination page

modpluz
Apr 22nd, 2009, 11:00 AM
i haven't said using js is right, my point is, if somehow my php script manage to echo output before header(); (which by the way will throw up error) - its js to the rescue.

however, i always make sure my js redirect happens at the very top of the page.

i may still be wrong - but willing to be corrected.

techgnome
Apr 22nd, 2009, 12:00 PM
then the lesson to learn is to NOT send any output at all until the redirect... which is the way I tend to operate anyhow... do all the processing and validation up front. If everything is copacetic, redirect to the OK page... if not, redirect to the ERROR page.

-tg

modpluz
Apr 23rd, 2009, 06:58 AM
Alright Sir!
i'll put more effort in not outputing anything before my header().

modpluz
Jun 26th, 2009, 08:33 AM
Just thought i'll put some more here, i recently went through all my work(it wasn't easy though) and i have managed to stop all output before headers.

it was really dumb though when i found out why i was outputting before headers.

Thank you all once again.

techgnome
Jun 26th, 2009, 08:42 AM
mind if I ask what was going on?

-tg

modpluz
Jul 1st, 2009, 12:09 PM
yeah sure, i always have a global file that kinda control what happens in all other files(included).

so, after throwing output from the main file, i then go on to use headers in one of the included file.

i warned you, it was dumb...