SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Jul 2003
    Location
    San Diego
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    logic error I think

    I have this processing page to either add or update a fishing report depending if there is a record in the database:

    PHP Code:

        
    include("../inc/stuff.php");

        
    // error list array
        
    $errorList = array();
        
    $count 0;

        
    // validate form fields
        
    if (!$lake_id) { $errorList[$count] = "Invalid entry: Lake Name"$count++; }    
        if (!
    $auth_id) { $errorList[$count] = "Invalid entry: Author Id"$count++; }    
        if (!
    $fish_report) { $errorList[$count] = "Invalid entry: Fish report"$count++; }    

        if (
    sizeof($errorList) == 0)
        {
            
    // open DB connection
            
    $connection mysql_connect($host$user$pass) or die ("Unable to connect!");

            
    // select DB
            
    mysql_select_db($db) or die ("Unable to select database!");

    if(
    checkdate($month$day$year)) 
        {
         
    $date_for_query $year.'-'.$month.'-'.$day;
        }
        else
            {  
            die(
    "Invalid Date Choice, please <a href='add_freshwater_report.php'>go back</a> and pick a date"); 
            } 
    Everything works fine until this point:

    PHP Code:
            
    $query 
    "SELECT lake_id, report_id FROM freshwater_report WHERE lake_id = '$lake_id' AND report_id = report_id";
    $result mysql_query($query) or die ("Error in query: $query. " mysql_error());
        if(!
    $result){
            
    // execute insert query
            
    $query "INSERT INTO freshwater_report(lake_id, author_id, date_fished, fish_report) VALUES ('$lake_id', '$auth_id', '$date_for_query', '$fish_report')";
            
    $result mysql_query($query) or die ("Error in query: $query. " mysql_error());
            
    // if the query was successful print:
            
    echo "The Fishing report was added successfully. <a href='index.php'>Go back to the main menu</a>.";
            } 
    If there is no record in the DB the portion above gets skipped and my update string is echoed to the brower: The Fishing report was updated successfully. Go back to the main menu

    PHP Code:
        else{
            
    $query "UPDATE freshwater_report SET lake_id = '$lake_id', author_id = '$auth_id', date_fished = '$date_for_query', fish_report = '$fish_report'
                      WHERE lake_id = '
    $lake_id' AND report_id = report_id";
            
    $result mysql_query($query) or die ("Error in query: $query. " mysql_error());
            echo 
    "The Fishing report was updated successfully. <a href='index.php'>Go back to the main menu</a>.";
            }
            
    mysql_close($connection);
        }
        else
        {
            
    // if there were errors found print them out
            
    echo "The following errors were encountered: <br>";
            echo 
    "<ul>";
            for (
    $x=0$x<sizeof($errorList); $x++)
            {
                echo 
    "<li>$errorList[$x]";
            }
            echo 
    "</ul>";
        } 

    BUT if there is a record in the database the update works. I've checked both the sql statments and they work the problem is in the logic I think.

    Thanks for any help -E

  2. #2
    SitePoint Evangelist
    Join Date
    Jul 2001
    Location
    Michigan, USA
    Posts
    414
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The problem is that $result still contains something when nothing is actual found.

    I remember having a similar problem when I was doing this. An easy work around would be to throw it into an array and then check to see if an actual array value was populated (like $row[lake_id] existed instead of $result).

    I'll be eagerly awaiting a better answer though!

  3. #3
    SitePoint Member
    Join Date
    Jul 2003
    Location
    San Diego
    Posts
    18
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Andy Tomaka
    The problem is that $result still contains something when nothing is actual found.

    I remember having a similar problem when I was doing this. An easy work around would be to throw it into an array and then check to see if an actual array value was populated (like $row[lake_id] existed instead of $result).

    I'll be eagerly awaiting a better answer though!

    I found it: the if statement should have been

    PHP Code:
    if (mysql_num_rows($result) == 0
    instead of
    PHP Code:
    if(!$result
    This really helped to figgure it out:
    PHP Code:
    $query "UPDATE freshwater_report SET lake_id = '$lake_id', author_id = '$auth_id', date_fished = '$date_for_query', fish_report = '$fish_report
                  WHERE lake_id = '
    $lake_id'"
    echo 
    '<!-- 'htmlentities($query), '-->'// output the query as html-comment only visible in the client's source view 
    $result mysql_query($query) or die ("Error in query: $query. " mysql_error()); 
    echo 
    "The Fishing report was updated successfully."mysql_affected_rows()," records have been updated <a href='index.php'>Go back to the main menu</a>."
    Thanks a bunch for your help -E

  4. #4
    SitePoint Evangelist
    Join Date
    Jul 2001
    Location
    Michigan, USA
    Posts
    414
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ah. I never really though to use the number of rows feature to check it. Thanks for pointing that out!


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
  •