www.webdeveloper.com
Results 1 to 7 of 7

Thread: [RESOLVED] numeric values not incremented

  1. #1
    Join Date
    Dec 2007
    Posts
    61

    resolved [RESOLVED] numeric values not incremented

    Hey y'all


    This is my first time that i try something with mysql and/or db's btw.


    I created a table called images with only 3 colums named "pid", "filename" and "views" with this:

    PHP Code:
    <?php 
        $connection 
    mysql_connect("host.name"
            
    "dbuser"
            
    "dbpass"); 

        
    mysql_select_db("dbname"$connection); 

    echo 
    "Connected to MySQL<br />";
    mysql_select_db("dbname") or die(mysql_error());
    echo 
    "Connected to Database<br />";


    // Create a MySQL table in the selected database

    mysql_query("CREATE TABLE tablename(
    pid INT NOT NULL AUTO_INCREMENT, PRIMARY KEY(pid), filename VARCHAR(30), views VARCHAR(7)"
    )
     or die(
    mysql_error());  

    echo 
    "Table tablename Created!";

    ?>

    but i think the problem is on this code. The "views" value on the specified row by the pid, is not incremented.

    the image(s) here are called like:

    somesite.com/page.php?imid=X
    (where X is some number)


    PHP Code:
    <html>
    <head>
    <title>some title</title>
    </head>
    <BODY>

    <?php 



    <?php 

    $connection 
    mysql_connect("host.name"
            
    "dbname"
            
    "dbpass"); 

        
    mysql_select_db("dbname"$connection); 





    $pid $_GET['imid'];




    // This is the part that Increments count of views, but is not working. 

    function increment($pid)
    {
       
    // Get data for page title
       
    $increment_query 'SELECT *
                                     FROM pics
                                     WHERE pid = '
    .$pid.'';
       
    $increment_result mysql_query($increment_query); 

       
    $increment_row mysql_fetch_array($increment_result);   

       
    // Get count data
       
    $views $increment_row['views']; 

       
    // Increment $count
       
    $views++;
     
       
    // Put new value into database
       
    $increment_query 'UPDATE tablename 
                                      SET views = '
    .$views.
                                      WHERE pid = '
    .$pid.'';
       
    $increment_result mysql_query($increment_query);






    // Get a specific result from the table 
    $result mysql_query("SELECT * FROM pics
     WHERE pid='
    $pid'") or die(mysql_error());  
    $row mysql_fetch_array$result );





    $openpic $row['filename'];

    echo 
    "<img src='$openpic' alt=''";
    echo 
    "<br><br>";

    ?> 

    </body>
    </html>

    i googled about it and also found

    PHP Code:
    UPDATE MyTable SET MyColumn=MyColumn+1 Where MyID=123 

    and tried this other code for my function increment($pid)


    PHP Code:
    function increment($pid)
    {
       
    // Get data for page title
    $increment_query 'UPDATE tablename SET views = views+1  WHERE pid= '.$pid.'';

    but nothing works. the problem must be easy to correct, but i know very little about this. remember... my 1st time "mysqling"!

    thanks in advance...

  2. #2
    Join Date
    Dec 2007
    Posts
    61

    I'm not sure abut this but, may the error just be on the code used to create the table? Didn't get any error and the code ran ok!

  3. #3
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,316
    Do you actually call the increment() function anywhere in your code?
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  4. #4
    Join Date
    Dec 2007
    Posts
    61
    Quote Originally Posted by NogDog View Post
    Do you actually call the increment() function anywhere in your code?


    I think i do, with this:

    PHP Code:
    function increment($pid
    Is there something else missing on the codeor have to cut the "$pid" from this line?





    the page as i tried first:

    PHP Code:
    <html> 
    <head> 
    <title>some title</title> 
    </head> 
    <BODY> 

    <?php 



    <?php 

    $connection 
    mysql_connect("host.name"
            
    "dbname"
            
    "dbpass"); 

        
    mysql_select_db("dbname"$connection); 





    $pid $_GET['imid']; 




    ///////////////////////////////////////////

    function increment($pid

       
    $increment_query 'SELECT * 
                                     FROM tablename 
                                     WHERE pid = '
    .$pid.''
       
    $increment_result mysql_query($increment_query); 
       
    $increment_row mysql_fetch_array($increment_result);    
     
    // Get count data 
       
    $views $increment_row['views']; 
    // Increment $count 
       
    $views++; 
    // Put new value into database 
       
    $increment_query 'UPDATE tablename 
                                      SET views = '
    .$views.
                                      WHERE pid = '
    .$pid.''
       
    $increment_result mysql_query($increment_query);

    // not sure if this line goes or not, but either way, didn't work  :(
    }
     
    ///////////////////////////////////////////




    // Get a specific result from the table 
    $result mysql_query("SELECT * FROM tablename
    WHERE pid='
    $pid'") or die(mysql_error());   
    $row mysql_fetch_array$result ); 





    $openpic $row['filename']; 

    echo 
    "<img src='$openpic' alt=''"
    echo 
    "<br><br>"

    ?> 

    </body> 
    </html>


    then i tried this way:

    PHP Code:
    <html> 
    <head> 
    <title>some title</title> 
    </head> 
    <BODY> 

    <?php 



    <?php 

    $connection 
    mysql_connect("host.name"
            
    "dbname"
            
    "dbpass"); 

        
    mysql_select_db("dbname"$connection); 





    $pid $_GET['imid']; 




    ///////////////////////////////////////////

    function increment($pid

       
    // Get data for page title 
    $increment_query 'UPDATE tablename SET views = views+1  WHERE pid= '.$pid.'';

    // again, not sure if this line goes or not, but either way, didn't work  :(
     
    ///////////////////////////////////////////




    // Get a specific result from the table 
    $result mysql_query("SELECT * FROM pics 
    WHERE pid='
    $pid'") or die(mysql_error());   
    $row mysql_fetch_array$result ); 





    $openpic $row['filename']; 

    echo 
    "<img src='$openpic' alt=''"
    echo 
    "<br><br>"

    ?> 

    </body> 
    </html>

    image is displayes fine, but nothing else.
    Last edited by knightman; 11-09-2009 at 09:20 PM.

  5. #5
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,316
    That is the start of the function definition. To actually execute it, you need to call the function wherever you want it to execute in the script.
    PHP Code:
    // define a function
    function example($name);
    {
       echo 
    "Hello, $name.";
    }

    // use a function:
    example("World");

    // use it again:
    example("Dave"); 
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  6. #6
    Join Date
    Dec 2007
    Posts
    61
    Thanks NogDog, i'll take note of this, and i'll try it tomorow.

    thanks again... and good night y'all!

    And i'll keep you posted of course!
    Last edited by knightman; 11-09-2009 at 09:33 PM.

  7. #7
    Join Date
    Dec 2007
    Posts
    61

    Thumbs up

    i got it working!




    PHP Code:

    // define a function 
    function increment($pid

    mysql_query("UPDATE pics SET `views` = `views`+'1' 
    WHERE pid = '
    $pid'"); 

    // use a function: 
    increment($pid); 
    thanks!!

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



Recent Articles