Page 1 of 1

SQL/PHP Problem.

Posted: Sun Sep 30, 2007 12:16 pm
by Blackmirth
feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hello.
  I have been trying to create a script that, if a correct answer is posted to the page, points are added to a user's profile. Also, it should add a number to a field, and does a strstr on the feild, so that the user cannot just re-post the answer and gain more points.
  I know that this code adds points properly, but the rest I am stuck with.
  Could you check out the syntax for me? And help me to see what the problem is.

Code: Select all

<?php

        if (isset($_POST['answer'])){

            if ($_POST['answer'] == 'answer to question'){
	
			$con = mysql_connect("XXXXXXXXXXXXXXXXX");
					if (!$con){
						die('Could not connect: ' . mysql_error());
  						}


			mysql_select_db("b13_980789_db", $con);

			$username = $_SESSION['username'];

			$isitdone = mysql_query("SELECT * FROM basic WHERE username = '$username'");

				if((strstr($isitdone, '1')) === FALSE){			

					mysql_query("UPDATE `b13_980789_db`.`users` SET `points` = points+3 WHERE `users`.`username` = '$username';

					UPDATE `b13_980789_db`.`users` SET `basic` = basic + '1' WHERE `users`.`username` = '$username'");

					echo "Congratulations.";

					}

				else{

					}

		mysql_close($con);

                    }


        }

?>

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]

Posted: Sun Sep 30, 2007 1:30 pm
by superdezign
I'm not exactly sure what you are asking, or what you are doing, but I do see a few errors and senselessness.

1) Why do you run strstr() on $isitdone? $isitdone is a MySQL resource, not a string.
2) mysql_query() can only run one query at a time.
3) One query says SET `basic` = basic + '1'. All that would do is add a '1' to the end of the string that is in `basic.` Is this intentional?
4) You wouldn't need to specify the database you are selecting from in the UPDATE queries, especially since you have a reference to the connection resource which you could pass to mysql_query().

Posted: Mon Oct 01, 2007 11:42 am
by Blackmirth
I wouldn't call it senslessness, but thanks for your help.
How is it possible, then, to run a strstr() on a MySQL result (the string in 'basic')? (In effect, I would like to make it check whether a value (1) is present in the field, and if it is, do nothing. If it isn't there, add the '1' into the field, so that it won't run that code again for that user.)

So, I should change the double mysql query into two separate mysql_query()s?

I will delete the database specification, then. Thanks.

Posted: Mon Oct 01, 2007 12:37 pm
by RobertGonzalez
You need to read $isitdone into a PHP data value. You can use something like mysql_fetch_array(), mysql_fetch_assoc() or something similar to those. Once you have the result resource in a data set you can access that data like you would any other array and check values within it just the same.

Posted: Mon Oct 01, 2007 12:56 pm
by Blackmirth
Thank you very much Everah, I will read up on it.