Jump to content

Recommended Posts

I'm trying to either update my database if some data matches the if statement, or insert the data in a new row if it doesn't match (if possible).

The code will update the database just fine if the data matches, but if not, it won't insert the new data. (When I leave off the if statement it will insert just fine). I fear I'm not using the if...else correctly.

 

Thanks for any help.

 

<?php	

header("Location: admin_schedule.php");

include("opendatabase.php");

$date=("$_POST[gamedate]");
$week=("$_POST[Week]");
$game=("$_POST[Game]");
$hometeam=("$_POST[team_name_1]");
$awayteam=("$_POST[team_name_2]");

$check = mysql_query("SELECT week_id,game_id FROM schedule");
$row[] = mysql_fetch_array($check);

if ($row[week_id]='$week' && $row[game_id]='$game')
{
mysql_query("UPDATE schedule 
SET week_id = '$week', game_id = '$game', date = '$date', H_team  = '$hometeam', A_team = '$awayteam'
WHERE week_id = '$week'
AND game_id = '$game'");
}

else
{
mysql_query("INSERT INTO schedule (week_id,game_id,date,H_team,A_team)
VALUES('$week','$game','$date','$hometeam','$awayteam')");
}

mysql_close($con);

Link to comment
https://forums.phpfreaks.com/topic/228960-problems-with-ifelse/
Share on other sites

Is week_id your primary key?

 

If so, you could just run the query like:

$sql = "INSERT INTO schedule (week_id,game_id,date,H_team,A_team) VALUES('$week','$game','$date','$hometeam','$awayteam')
            ON DUPLICATE KEY UPDATE game_id = VALUES(game_id), date = VALUES(date), H_team = VALUES(H_team), A_team = VALUES(A_team)";
mysql_query($sql) or trigger_error($sql . ' has encountered an error: <br />' . mysql_error());

 

Then you wouldn't need if statements, nor would you need your first query.  This will insert the data unless it encounters a duplicate primary key.  If it encouters the duplicate key, it will update the row with the new data.

 

UN-TESTED1

 

Auto-increments wouldn't work, as it will just be inserted since the value would be incremented.

 

At this point, the easiest thing to do, is change your first query.

 

$check = mysql_query("SELECT week_id,game_id FROM schedule");
//to
$check = mysql_query("SELECT week_id  FROM schedule WHERE week_id = '$week' AND game_id = '$game'");
//next change your if statement to:
if(mysql_num_rows($check) > 0)

 

Should fix you up.

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.