0

I've got two queries which are, in the end, nearly exactly the same. But for some reason the first one works flawlessly and the second one doesn't add anything to the database nor gives back any error.

The one that works looks like this:

public function insertPits($pitstop, $RaceID, $managerId, $seasonNumber, $raceNumber) {

$sqlpit = "insert into pitstop (RaceID,managerid,season,race,PitStopNo,LapNo, Reason,TyreWear,FuelLeft,FuelLoad,PitTime) values "; 
    foreach ($pitstop as $pitstop){ 
      $sqlpit .= "($RaceID,$managerId,$seasonNumber,$raceNumber,$pitstop[0],$pitstop[1],'$pitstop[2]',$pitstop[3],$pitstop[4],$pitstop[5],$pitstop[6]),"; 
    } 
$sqlpit =rtrim($sqlpit,","); 
mysqli_query($GLOBALS["___mysqli_ston"], $sqlpit);
 }

The one not working as it should:

public function insertStints($stints, $RaceID, $managerId, $seasonNumber, $raceNumber) {

$sqlstint = "insert into stint (RaceID,managerid,season,race,StintNo,Tyre) values "; 
    foreach ($stints as $stints) {
      $sqlstint .= "($RaceID,$managerId,$seasonNumber,$raceNumber,$stints[0],$stints[7]),"; 
    } 
$sqlstint =rtrim($sqlstint,","); 
mysqli_query($GLOBALS["___mysqli_ston"], $sqlstint);  
 }

$pitstop and $stints are both the exact same array, just different names. The pitstop database is filled correctly, the stints database stays empty without error.

The $sqlstint shows this as echo, which is right.. but it doesn't add anything:

insert into stint (RaceID,managerid,season,race,StintNo,Tyre) 
values (40978,65,60,3,1,Extra Soft),
       (40978,65,60,3,2,Extra Soft),
       (40978,65,60,3,3,Extra Soft)

Does anyone have a clue how to fix this, or how to do it differently (maybe combine them into one function.. but to be honest.. I have no clue how to do that either). Hoping for your feedback!

Charles Bretana
  • 143,358
  • 22
  • 150
  • 216
kevibu
  • 15
  • 6
  • 4
    Learn to use parameters. Don't munge query strings by putting values directly into them. – Gordon Linoff Aug 05 '17 at 13:18
  • 3
    first time I see a `foreach (X as X` Following your edit quotes are missing around "Extra Soft" –  Aug 05 '17 at 13:21
  • You need quotes around '$stints[7]'. But even if it works with them you should still listen to the previous two comments. – jh1711 Aug 05 '17 at 13:25
  • I know it's far from perfect coding, real coders will prolly call it horrible or worse.. but it's a private project, more a hobby really.. I'll look into the feedback. The quotes actually done the job, stupid that I haven't seen it.. thanks for that help ;-) – kevibu Aug 05 '17 at 13:30

0 Answers0