hosker Posted December 28, 2011 Share Posted December 28, 2011 I have the following code that when used seperately work. How would I write an if..else statement that will make them work together? The following code give me an Parse Error, syntax error unexpected T_string. The following if my code I have tried, if (SELECT tournament FROM weekly_picks WHERE tournament = '$tournament') mysql_query("UPDATE weekly_picks SET player = '$golfer' WHERE tournament = '$tournament' AND user = '$usr'"); else mysql_query("INSERT INTO weekly_picks (t_id, tournament, user, player, backup, timestamp) VALUES ('$t_id', '$tournament', '$usr', '$golfer', '$backup', '$time')",$link) or die('Error, insert query failed'); I also know that values are being stored in the variables. Quote Link to comment https://forums.phpfreaks.com/topic/253929-help-using-mysql-update/ Share on other sites More sharing options...
charles07 Posted December 28, 2011 Share Posted December 28, 2011 hosker could you explain what your requirement is clearly? Quote Link to comment https://forums.phpfreaks.com/topic/253929-help-using-mysql-update/#findComment-1301767 Share on other sites More sharing options...
hosker Posted December 28, 2011 Author Share Posted December 28, 2011 I have a form that gets submitted weekly and posts data into my DB for a players golf pick for the tournament that weekend. If the user changes their mind and wants to submit a different pick, I only want one instance of the tournament, user, and pick in the database, not multiple. I want to be able to have check to see if the tournament and user already exist in the database, and if they do, execute somecode, if not execute othercode. Quote Link to comment https://forums.phpfreaks.com/topic/253929-help-using-mysql-update/#findComment-1301768 Share on other sites More sharing options...
peter_anderson Posted December 28, 2011 Share Posted December 28, 2011 This line is incorrect: if (SELECT tournament FROM weekly_picks WHERE tournament = '$tournament') You need to execute the query and get the number of results: $q = "SELECT tournament FROM weekly_picks WHERE tournament = '$tournament'"; // execute the query $q = $sql->query($q); // use num_rows if($q->num_rows > 0){ // put the rest here You will need to change the query & num_rows to fit in with how you do database queries (eg mysql_query) Quote Link to comment https://forums.phpfreaks.com/topic/253929-help-using-mysql-update/#findComment-1301880 Share on other sites More sharing options...
hosker Posted December 28, 2011 Author Share Posted December 28, 2011 I have been trying to figure this out myself, but I am having no luck. Here is the following code that I have written: if (mysql_query("SELECT * FROM weekly_picks WHERE tournament = '$tournament' AND usr = '$usr'")) mysql_query("UPDATE weekly_picks SET player = '$golfer' WHERE tournament = '$tournament' AND user = '$usr'"); else mysql_query("INSERT INTO weekly_picks (t_id, tournament, user, player, backup, timestamp) VALUES ('$t_id', '$tournament', '$usr', '$golfer', '$backup', '$time')",$link) or die('Error, insert query failed'); This code will run, but all it does is add a new row into my database instead of updating what is already there when needed. Any help would be much appreciated. Quote Link to comment https://forums.phpfreaks.com/topic/253929-help-using-mysql-update/#findComment-1301881 Share on other sites More sharing options...
peter_anderson Posted December 28, 2011 Share Posted December 28, 2011 Your if statement will run only if the query succeeds. You're not checking if there are any results. Quote Link to comment https://forums.phpfreaks.com/topic/253929-help-using-mysql-update/#findComment-1301897 Share on other sites More sharing options...
hosker Posted December 28, 2011 Author Share Posted December 28, 2011 How would I check for results? Quote Link to comment https://forums.phpfreaks.com/topic/253929-help-using-mysql-update/#findComment-1301898 Share on other sites More sharing options...
peter_anderson Posted December 28, 2011 Share Posted December 28, 2011 $q = mysql_query("SELECT * FROM weekly_picks WHERE tournament = '$tournament' AND usr = '$usr'"); $num = mysql_num_rows($q); $num will hold how many results tehre are Quote Link to comment https://forums.phpfreaks.com/topic/253929-help-using-mysql-update/#findComment-1301913 Share on other sites More sharing options...
hosker Posted December 28, 2011 Author Share Posted December 28, 2011 So my if statement condition is: If ($num > 0) Correct? Quote Link to comment https://forums.phpfreaks.com/topic/253929-help-using-mysql-update/#findComment-1301914 Share on other sites More sharing options...
hosker Posted December 28, 2011 Author Share Posted December 28, 2011 I just put the code in and this was what I received: Warning: mysql_num_rows(): supplied argument is not a valid MySQL result resource Thoughts? Quote Link to comment https://forums.phpfreaks.com/topic/253929-help-using-mysql-update/#findComment-1301918 Share on other sites More sharing options...
Pikachu2000 Posted December 28, 2011 Share Posted December 28, 2011 Then the query is failing. mysql_error should tell you why it's failing. Quote Link to comment https://forums.phpfreaks.com/topic/253929-help-using-mysql-update/#findComment-1301933 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.