widget Posted April 15, 2007 Share Posted April 15, 2007 Hi, I am running a virtual pet style site. Reason: Learning php and what better way to do it Anyway when a user buys a book to read to its pet, its intelligence rating goes up. What Im trying to do is make it so that if they have read the book already, they cannot read it again. I've managed to create a new database table and have the information writing to it but I cant seem to get the rest of it working. Heres the code so far. if ($find_item2[item_type] == "intel_inc") { $getread = fetch("SELECT * FROM read2 WHERE pet_id = '$use_on' AND item_id = '$find_item2[id]' AND game = '$game'"); if (!$getread); { mysql_query("INSERT INTO read2 (item_id,pet_id,game) VALUES ('$find_item2[id]','$use_on','$game')"); $newLevel = $getPetInfo[intelligence]+$find_item2[magic_num]; mysql_query("UPDATE user_pets2 SET intelligence=$newLevel WHERE id = '$use_on' AND game = '$game'"); $effect = "<p>$getPetInfo[name] really liked that book!</p>"; } if ($getread); { $effect = "<b>Sorry, $getPetInfo[name] has read this book already</b>"; } } If i try an if then an else I get errors. I really need some help here. If you know a great tutorial or can help me with the code please post. Quote Link to comment Share on other sites More sharing options...
kenrbnsn Posted April 15, 2007 Share Posted April 15, 2007 You shouldn't have semi-colons after the if/else clause: <?php if ($find_item2[item_type] == "intel_inc") { $q = "SELECT * FROM read2 WHERE pet_id = '$use_on' AND item_id = '" . $find_item2['id'] . "' AND game = '$game'"; $rs = mysql_query($q) or die("Problem with the query <pre>$q</pre><br>" . mysql_error()); $getread = (mysql_num_rows > 0)?true:false; if (!$getread) { $newLevel = $getPetInfo[intelligence]+$find_item2[magic_num]; $q = "INSERT INTO read2 (item_id,pet_id,game, intelligence) VALUES ('$find_item2[id]','$use_on','$game','$newLevel')"; $rs = mysql_query($q) or die("Problem with the query <pre>$q</pre><br>" . mysql_error()); $effect = "<p>$getPetInfo[name] really liked that book!</p>"; } else $effect = "<b>Sorry, $getPetInfo[name] has read this book already</b>"; } ?> I also cleaned up your code a little... Ken Quote Link to comment Share on other sites More sharing options...
widget Posted April 15, 2007 Author Share Posted April 15, 2007 Thank you Ken, that looks fab and I'll try it now. I have although managed to get it working by using this code (yours looks much better) if ($find_item2[item_type] == "intel_inc") { $getread = fetch("SELECT item_id FROM read2 WHERE pet_id = '$use_on'"); if ($getread[item_id] == "$find_item2[id]") { $effect = "<b>Sorry, $getPetInfo[name] has read this book already</b>"; } elseif ($getread[item_id] != "$find_item2[id]") { mysql_query("INSERT INTO read2 (item_id,pet_id,game) VALUES ('$find_item2[id]','$use_on','$game')"); $newLevel = $getPetInfo[intelligence]+$find_item2[magic_num]; mysql_query("UPDATE user_pets2 SET intelligence=$newLevel WHERE id = '$use_on' AND game = '$game'"); $effect = "<p>$getPetInfo[name] really liked that book!</p>"; } } [code] [/code] Quote Link to comment Share on other sites More sharing options...
widget Posted April 19, 2007 Author Share Posted April 19, 2007 Ok it was working and now for some reason its not!!!! I havent changed anything, this is so frustrating. Quote Link to comment Share on other sites More sharing options...
AndyB Posted April 19, 2007 Share Posted April 19, 2007 Ok it was working and now for some reason its not!!!! I havent changed anything, this is so frustrating. We both know that isn't possible. Something is different!! Post the code that isn't working. Explain what it's not doing that it did before and vice versa. Be certain that you uploaded any edited versions to the same folder/same server, etc. Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.