sql - mysql error in WHERE clause -


i'm sorry ask such basic question, cant life of me spot error here far can see correct. yet error, perhaps need pair of fresh eyes have look

you have error in sql syntax; check manual corresponds mysql server version right syntax use near 'where event_id = '1243'' @ line 1 in insert expiredevents (event_id,sport_type,tournament,round,team1,team2,venue,event_date) values ('1243','rugby','super15','3','waratahs','sharks','allianz stadium','') event_id = '1243'

 $sql="insert expiredevents   (event_id,sport_type,tournament,round,team1,team2,venue,event_date)   values ('$id','$sport','$trnmnt','$rnd','$t1','$t2','$ven','$edate')   event_id = '$id'" 

there no where clause in correct syntax of insert statement.

depending on want achieve, choose 1 of following.

insert new row, don't bother if 1 having same event_id exists

insert expiredevents     (event_id, sport_type, tournament, round, team1, team2, venue, event_date)  values     ('$id', '$sport', '$trnmnt', '$rnd', '$t1', '$t2', '$ven', '$edate')  

if event_id unique index of table expiredevents, query fails if record having event_id = '$id' exists.

assuming event_id pk of table, keep reading.

insert new row if not exists

insert ignore expiredevents     (event_id, sport_type, tournament, round, team1, team2, venue, event_date)  values     ('$id', '$sport', '$trnmnt', '$rnd', '$t1', '$t2', '$ven', '$edate')  

the ignore keyword turns errors warnings , query completes not insert row if 1 having event_id = '$id' exists.

inserts row if not exist or update existing one, if exists

insert expiredevents     (event_id, sport_type, tournament, round, team1, team2, venue, event_date)  values     ('$id', '$sport', '$trnmnt', '$rnd', '$t1', '$t2', '$ven', '$edate')  on duplicate key update     sport_type=values(sport_type), round=round+1, event_date=now() 

if row not exist, query insert using values values clause. if row exists uses on duplicate key update know how update it. there 3 fields modified in query:

  • sport_type=values(sport_type) - value of column sport_type updated using value provided in query column sport_type (values(sport_type), '$sport');
  • round=values(round)+1 - value of column round updated using current value plus 1 (round+1); value provided in values clause not used;
  • event_date=now() - value of column event_date modified using value returned function now(); both old value , 1 provided in values clause of query ignored.

this example, put there whatever expressions need update existing row.

completely replace existing row new one

replace expiredevents     (event_id, sport_type, tournament, round, team1, team2, venue, event_date)  values     ('$id', '$sport', '$trnmnt', '$rnd', '$t1', '$t2', '$ven', '$edate')  

the replace statement mysql extension sql standard. first deletes row having event_id = '$id' (if any) inserts new row. functionally equivalent delete expiredevents event_id = '$id' followed first query exposed above in answer.


Comments

Popular posts from this blog

c++ - Difference between pre and post decrement in recursive function argument -

php - Nothing but 'run(); ' when browsing to my local project, how do I fix this? -

php - How can I echo out this array? -