I can't understand why mysqli_query
returns true on an operation where one of the fields doesn't exist. I am trying to UPDATE
a field when the field run_id
matches the variable $id
. However, when $id
isn't set, the echo inside the if statement executes. Would really appreciate your thoughts on this phenomenon.
function sumMiles($id,$runnerRow,$weekArray,$cnn){
//Calculate cumulative miles per week for every runner
$weekMiles=0; //total miles per week
foreach($weekArray as $weekday)
{
$weekMiles+=$runnerRow[$weekday];
}
$addMiles = "UPDATE run SET weekly_total = {$weekMiles} WHERE run_id=$id";//add weekly total into the database's run table
if (mysqli_query($cnn,$addMiles)){
echo "Your weekly total has been updated";
return $weekMiles;
}
else {echo "Your weekly total could not be updated";}
}
I am calling this function like so:
$weekMiles = sumMiles($id,$dailyMileage,$weekArray,$cnn);
The fact that the line doens't exist will not generate any exception at the database.
Verify the existence of the line first before run your update
select count(1) from run WHERE run_id=$id
EDIT
You need to check if the Count(1)
is bigger than 0
$query = "SELECT count(1) from run WHERE run_id=$id";
if (mysqli_query($cnn,$query)>0){
$addMiles = "UPDATE run SET weekly_total = {$weekMiles} WHERE run_id=$id";
echo "updated";
}