Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 11 of 11
  1. #1
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts

    PHP/MYSQL update function?

    Hello!

    I'm trying to do an update to a mysql record.
    but seem to be getting some errors,

    my code:
    PHP Code:

    $update 
    $_REQUEST['update'];
    $content $_REQUEST['content'];
    $section $_REQUEST['section'];

    mysql_connect($DBhost,$DBuser,$DBpass) or die("Unable to connect to database");
    @
    mysql_select_db("$DBName") or die("Unable to select database $DBName");
    $sqlquery "UPDATE content SET cData='$content' WHERE id='$update'";
    mysql_query($query);

      
    header("Location: admin.php?edit=$section");

    mysql_close(); 
    errors:

    Notice: Undefined variable: query in C:\Program Files\Apache Group\Apache2\htdocs\national\admin\func.php on line 14

    line 14 is: mysql_query($query);

    Warning: Cannot modify header information - headers already sent by (output started at C:\Program Files\Apache Group\Apache2\htdocs\national\admin\func.php:14) in C:\Program Files\Apache Group\Apache2\htdocs\national\admin\func.php on line 16

    line 16: header("Location: admin.php?edit=$section");

    why am i getting this header error? this is the only code in the script right now, so i'm a bit confused. also why is my update statement not working?

    I thought it would be the same as the INSERT statement, cept use UPDATE insted??

    thanks in advance for your time!
    -Ken

  • #2
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    ok, i've got the UPDATE working,
    just needed to change the mysql_query($sqlquery); to reflect query statement. but just wondering why i get this header error so much?

    thanks again for your time!
    -Ken

  • #3
    Regular Coder
    Join Date
    Jun 2003
    Location
    Australia
    Posts
    528
    Thanks
    8
    Thanked 8 Times in 8 Posts
    header: outset already sent:

    this means you have HTML inputted somewhere prior to the "location" line. This is usually cause by a blank line.

  • #4
    Regular Coder
    Join Date
    Jun 2005
    Posts
    804
    Thanks
    0
    Thanked 0 Times in 0 Posts
    When you're using a relocation header, nothing can have been sent to the browser before that header function is called. In your original code, the undefined variable error message is being sent to the browser, so your header() call is getting hosed.

  • #5
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    yah, still getting this error:

    Warning: Cannot modify header information - headers already sent by (output started at index.php:6) in index.php on line 124,

    so how can i do a redirect if data has already been sent to the browser?

    -SiX

  • #6
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    hmm, ok i'm using this JavaScript to do it,, not sure that this is the best way though, but it works.

    <script language="Javascript" type="text/javascript">
    <!-- Hide script
    //<![CDATA[
    window.location.href="?home"
    //]]> End script hiding -->
    </script>


    -Ken

  • #7
    Regular Coder
    Join Date
    Feb 2005
    Location
    West Midlands, UK
    Posts
    623
    Thanks
    0
    Thanked 0 Times in 0 Posts
    You should take a look at PHP output buffering. It's a very useful tool which will basically allow you to capture all browser output in a buffer variable. You can then decide if any headers need to be sent and finally echo your buffer variable as the last thing you do in the script. That will ensure that no content is sent before headers are sent Definitely much better than javascript, which won't work on everyone's browsers.

  • #8
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    kool, that sounds interesting, i'll look into it.

    thanks!
    -Ken

  • #9
    Senior Coder NancyJ's Avatar
    Join Date
    Feb 2005
    Location
    Bradford, UK
    Posts
    3,172
    Thanks
    19
    Thanked 65 Times in 64 Posts
    ...you have several blank lines in your code... also afaik the mysqlclose() wont execute cos its after the redirect.
    If you're redirecting to another page then nothing should be sent to the browser since you're not displaying any of it

  • #10
    Regular Coder
    Join Date
    Jun 2005
    Posts
    804
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Actually, the redirect won't stop execution of the original script, so the mysql_close() should be fine. You could actually run the header() call very first thing and quietly run the UPDATE afterward, but you'd lose any error handling. Your die() would still kill the script, but the error message wouldn't be sent (though you could always set up an administrative email notification/log file to capture them).

  • #11
    Senior Coder angst's Avatar
    Join Date
    Apr 2004
    Location
    Toronto, Ontario
    Posts
    2,114
    Thanks
    15
    Thanked 122 Times in 122 Posts
    kool!
    thanks for all the ideas guys!

    I'm picking up php pretty fast because of this great forum!


    -Ken


  •  

    Posting Permissions

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