|
Subject:
|
Need help debugging....
|
|
Posted By:
|
reg03
|
Post Date:
|
1/24/2004 8:31:24 AM
|
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.
|
|
Reply By:
|
nikolai
|
Reply Date:
|
1/26/2004 2:58:30 PM
|
You're not making any comparisons in the if () conditional -- you're making an assignment.
quote:
if ($title = $line->title)
This should probably be: if ($title == $line->title)
Take care,
Nik http://www.bigaction.org/
|
|
Reply By:
|
reg03
|
Reply Date:
|
1/27/2004 8:14:48 AM
|
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
|
|
Reply By:
|
nikolai
|
Reply Date:
|
1/27/2004 1:45:51 PM
|
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/
|
|
Reply By:
|
reg03
|
Reply Date:
|
1/27/2004 2:34:15 PM
|
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
|
|
Reply By:
|
nikolai
|
Reply Date:
|
1/27/2004 2:51:54 PM
|
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/
|