BelowZero Posted February 26, 2011 Share Posted February 26, 2011 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); Quote Link to comment https://forums.phpfreaks.com/topic/228960-problems-with-ifelse/ Share on other sites More sharing options...
jcbones Posted February 26, 2011 Share Posted February 26, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/228960-problems-with-ifelse/#findComment-1180149 Share on other sites More sharing options...
BelowZero Posted February 26, 2011 Author Share Posted February 26, 2011 week_id and game_id are not primary keys, since I'll have several matching weeks and several matching games to manipulate. I have another field (counter) as a primary, auto increment key. Quote Link to comment https://forums.phpfreaks.com/topic/228960-problems-with-ifelse/#findComment-1180152 Share on other sites More sharing options...
jcbones Posted February 26, 2011 Share Posted February 26, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/228960-problems-with-ifelse/#findComment-1180162 Share on other sites More sharing options...
BelowZero Posted February 26, 2011 Author Share Posted February 26, 2011 yep, that did it. Thanks a lot! Quote Link to comment https://forums.phpfreaks.com/topic/228960-problems-with-ifelse/#findComment-1180169 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.