Wrox Programmer Forums
Go Back   Wrox Programmer Forums > PHP/MySQL > Beginning PHP
|
Beginning PHP Beginning-level PHP discussions. More advanced coders should post to the Pro PHP forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Beginning PHP section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old January 24th, 2004, 09:31 AM
Registered User
 
Join Date: Jan 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default Need help debugging....

This is part of some code for a page of mine. I can't figure out why it's not evaluating the block that starts else {...


<?php

  if($submit) {

    //check for title uniqueness

    $sql="SELECT title FROM info WHERE title = \"$title\"";
    $result = mysql_query($sql);

    while ($line = mysql_fetch_object($result)) {

      //if title is not unique, ask user to rename and resubmit

      if($title = $line->title) {

?>
.....some html stuff here....

<?php

      }

      else { //this is the part that is not being evaluated

    //insert data into tables

        $sql="INSERT INTO info VALUES (\"$title\",\"$name\",\"$email\",\"$category\",\"$ comments\")";
        $result = mysql_query($sql);


....more sql statements and some print statements...

      }

    }

  }

  else {

    //display blank data entry form

?> //this works just fine

...some html stuff...

<?php
  }
?>

Any thoughts?

Thanks.


 
Old January 26th, 2004, 03:58 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

You're not making any comparisons in the if () conditional -- you're making an assignment.

Quote:
quote:
if ($title = $line->title)
This should probably be:
  if ($title == $line->title)


Take care,

Nik
http://www.bigaction.org/
 
Old January 27th, 2004, 09:14 AM
Registered User
 
Join Date: Jan 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks.

I did make that change and it still bypasses the if...else statments when the user submits the form a second time - so nothing inserts into the database.

Robyn

 
Old January 27th, 2004, 02:45 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Well, that's because your logic is flawed. If the title does NOT exist in the database, you won't have any result rows. Your else block is contained inside the while loop that iterates over the result rows.

When you think about it, it's totally obvious that the else should never execute, because you're not selecting any rows where the titles don't match.

More (unasked-for) comments:

Your query doesn't make sense. You're selecting all the 'title' column values where the column value is some fixed value ($title). As such, the query you wrote doesn't really do anything for you, since you're not really using the data you're selecting for any specific purpose. I mean, why select any data from the database when you already KNOW the data you're extracting?

What you're really trying to do is to determine whether any rows in the database contain a specific value in a specific column. More specifically -- you want the number of rows in the database where a specific value is found.

A better approach would be:

$query = "SELECT count(*) FROM info WHERE title='{$title}'";
$result = mysql_query($query);

if (FALSE === $result)
{
    // error
}
else // query sucessfully executed
{
    $count = mysql_result($result, 0);

    if ($count > 0) // $title exists in the database.
    {
        // ask user to rename and resubmit.
    }
    else // $title does NOT exist in the database.
    {
        INSERT INTO info ...
    }
}


Make sense?

Take care,

Nik
http://www.bigaction.org/
 
Old January 27th, 2004, 03:34 PM
Registered User
 
Join Date: Jan 2004
Posts: 5
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I'm such a dork...lol. Well, logical errors happen to the best of us. I hate it when I make silly mistakes like that...

Actually, the query you're referring to does have a purpose...I just didn't include all of the code. But, I agree that if I wasn't doing anything else with that data then your query checking for a count would suffice.

Thank you. You have ended a good deal of frustration for me.

Robyn
 
Old January 27th, 2004, 03:51 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 836
Thanks: 0
Thanked 0 Times in 0 Posts
Default

No problem. I've been bitten by that stuff more times than I like to admit -- and it's always after I realize the problem that I kick myself. Hard.

Anyway, I'm glad that it's working!

Take care,

Nik
http://www.bigaction.org/





Similar Threads
Thread Thread Starter Forum Replies Last Post
Help Debugging fs22 Javascript 3 May 17th, 2005 10:13 AM
Debugging BSkelding VB.NET 2002/2003 Basics 1 June 16th, 2004 10:32 PM
Debugging Louisa VB.NET 2002/2003 Basics 1 March 18th, 2004 11:55 AM
debugging Adam H-W Classic ASP Basics 1 February 11th, 2004 02:30 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.