Jump to content

Help using mySQL UPDATE


hosker

Recommended Posts

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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)

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.