SitePoint Sponsor

User Tag List

Results 1 to 7 of 7
  1. #1
    SitePoint Enthusiast kerim's Avatar
    Join Date
    Mar 2001
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    How to increase the value of a field by one?

    I am writing a script to keep track of downloads, and I am having lots of difficulty writing the code to increase the field "num" (for number of downloads) by one each time there is a download.

    Right now I have the script:

    $num++

    $sql = mysql_query (UPDATE $table WHERE id='$id' SET num='$num');

    But it insists on updating every row in the table, not just the row I want to update!!! If I try anything else, I either get a syntax error, or no result at all . . .

    Maybe if I saw some samples of how other people do this in their scripts I could figure it out.

    Thanks!

    kerim

  2. #2
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Firstly you have the structure of your sql statement mixed up a little and this is why it is not performing as you wish. The sql you provided should be written like:

    UPDATE $table SET num='$num' WHERE id='$id'

    However, there is a simpler way of incrementing the value of a field in the database:

    UPDATE $table SET num = num + 1 WHERE id='$id'

  3. #3
    SitePoint Enthusiast kerim's Avatar
    Join Date
    Mar 2001
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks. I tested the syntax right in MySQL manager and it works fine, but for some reason it is not working in the script. Below is th e whole script, do you see anything obviously wrong? Thanks.

    kerim

    <?PHP

    // This script is an adaptation of Download Manger
    // which can be seen at (www.tunga.com/PHP)

    include("downlogin.php");

    $filelist = mysql_query(
    "SELECT * FROM files WHERE id='$id'");

    while ($row = mysql_fetch_array($filelist)) {
    $id = $row["id"];
    $filename = $row["filename"];
    $num = $row["num"];

    // add 1 to number of downloads

    $sql = mysql_query("UPDATE files SET num = num + 1 WHERE id='$id' or*DIE(mysql_error())");

    // sends them the PDF file

    $file_location = "****deleted for security****";


    $file_path = $file_location . $filename;
    if (detect_browser($HTTP_USER_AGENT) == "ie")
    {
    Header("Content-type: application/force-download");
    }else{
    Header("Content-Type: application/octet-stream");
    }
    Header("Content-Length: ".filesize($file_path));
    Header("Content-Disposition: attachment; filename=$filename");
    readfile($file_path);
    }
    // this is the function called to determine the browser

    function detect_browser($variable)
    {
    if(eregi("(msie) ([0-9]{1,2}.[0-9]{1,3})", $variable))
    {
    $content = "ie";
    }else{
    $content = "nn";
    }
    return $content;
    }


    ?>

  4. #4
    SitePoint Addict Chris Roane's Avatar
    Join Date
    Jul 1999
    Location
    Helena, MT
    Posts
    287
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:

    $sql = mysql_query("UPDATE files SET num = '$num + 1' WHERE id='$id' or DIE(mysql_error())");

    Technically, it shouldn't make a difference, but you never know.

  5. #5
    SitePoint Enthusiast kerim's Avatar
    Join Date
    Mar 2001
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Still no luck.

    Thanks. I tried the different syntax. I even found another syntax error (one of the spaces was not a real space character), but no luck. It still is not updating the database. Like I said, the file downloads fine, although annoyingly the acrobat 5 plugin manages to grab the file before download and so it appears in the browser. When this happens, it also avoids naming the file correctly - giving it the name of the PHP script instead. This is a real bummer, since this script was written partically to avoid that problem by pulling the filename from the SQL database.

    kerim

  6. #6
    ********* Callithumpian silver trophy freakysid's Avatar
    Join Date
    Jun 2000
    Location
    Sydney, Australia
    Posts
    3,798
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Code:
    $sql = mysql_query("UPDATE files SET num = num + 1 WHERE id='$id' or*DIE(mysql_error())");
    should be
    Code:
    $sql = mysql_query("UPDATE files SET num = num + 1 WHERE id='$id'") or*DIE(mysql_error());

  7. #7
    SitePoint Enthusiast kerim's Avatar
    Join Date
    Mar 2001
    Posts
    49
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    It WORKS!



    Thank you all so much for helping a poor novice coder who has no idea! I've been working on this script for *months* and it finally works! (Except for the Acrobat issue I mentioned.) Maybe someday there will be a tool for the MacOS that actually checks my syntax for me!

    Cheers.

    kerim


Bookmarks

Posting Permissions

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