Results 1 to 14 of 14

Thread: POST Problem

  1. #1

    Thread Starter
    Addicted Member
    Join Date
    Oct 2001
    Location
    Miami, Florida
    Posts
    161

    POST Problem

    Hi guys,

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

    Code:
    	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!!!

    Code:
    $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>';

  2. #2
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: POST Problem

    this should probably be posted in the PHP forum 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:
    PHP Code:
    header("Location: buyminutes.php"); 
    just make sure that you're sending this header before any output is sent to the browser.

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: POST Problem

    Moved From The CodeBank

  4. #4

    Thread Starter
    Addicted Member
    Join Date
    Oct 2001
    Location
    Miami, Florida
    Posts
    161

    Resolved [SOLVED] POST Problem

    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

  5. #5
    Fanatic Member modpluz's Avatar
    Join Date
    Sep 2005
    Location
    Lag, NG
    Posts
    633

    Re: POST Problem

    Quote Originally Posted by kows
    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.
    Code:
    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?
    If you want the rabbit to hop, move the carrot - Paul Kellerman(Prison Break)

    onError GoTo http://vbforums.com



    My Bits:
    VB6: Change Column Name in MS ACCESS

  6. #6
    PowerPoster
    Join Date
    Sep 2003
    Location
    Edmonton, AB, Canada
    Posts
    2,629

    Re: POST Problem

    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.

  7. #7
    Fanatic Member modpluz's Avatar
    Join Date
    Sep 2005
    Location
    Lag, NG
    Posts
    633

    Re: POST Problem

    Quote Originally Posted by kows View Post
    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).
    If you want the rabbit to hop, move the carrot - Paul Kellerman(Prison Break)

    onError GoTo http://vbforums.com



    My Bits:
    VB6: Change Column Name in MS ACCESS

  8. #8
    Frenzied Member I_Love_My_Vans's Avatar
    Join Date
    Jan 2005
    Location
    In the PHP compiler
    Posts
    1,275

    Re: POST Problem

    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
    Last edited by I_Love_My_Vans; Apr 22nd, 2009 at 07:24 AM.

  9. #9
    Fanatic Member modpluz's Avatar
    Join Date
    Sep 2005
    Location
    Lag, NG
    Posts
    633

    Re: POST Problem

    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.
    If you want the rabbit to hop, move the carrot - Paul Kellerman(Prison Break)

    onError GoTo http://vbforums.com



    My Bits:
    VB6: Change Column Name in MS ACCESS

  10. #10
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: POST Problem

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  11. #11
    Fanatic Member modpluz's Avatar
    Join Date
    Sep 2005
    Location
    Lag, NG
    Posts
    633

    Re: POST Problem

    Alright Sir!
    i'll put more effort in not outputing anything before my header().
    If you want the rabbit to hop, move the carrot - Paul Kellerman(Prison Break)

    onError GoTo http://vbforums.com



    My Bits:
    VB6: Change Column Name in MS ACCESS

  12. #12
    Fanatic Member modpluz's Avatar
    Join Date
    Sep 2005
    Location
    Lag, NG
    Posts
    633

    Re: POST Problem

    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.
    If you want the rabbit to hop, move the carrot - Paul Kellerman(Prison Break)

    onError GoTo http://vbforums.com



    My Bits:
    VB6: Change Column Name in MS ACCESS

  13. #13
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: POST Problem

    mind if I ask what was going on?

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  14. #14
    Fanatic Member modpluz's Avatar
    Join Date
    Sep 2005
    Location
    Lag, NG
    Posts
    633

    Re: POST Problem

    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...
    If you want the rabbit to hop, move the carrot - Paul Kellerman(Prison Break)

    onError GoTo http://vbforums.com



    My Bits:
    VB6: Change Column Name in MS ACCESS

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width