Problem getting array values to update in table

The error output just shows "Failed to run query: (0) " I’m afraid, so it’s as if it can’t find an error…

Very strange- the values all seem to arrive ok, but then the UPDATE doesn’t accept them…

interestingly it seems to be getting the values twice- I edited the table so that there are now only two service records for that car. When I tried updating the values again:

Failed to run query: (0) 
array(2) { 
[6613]=> string(8) "12.04.71" 
[6614]=> string(8) "23.05.85" } 
array(2) { 
[6613]=> string(6) "16,897" 
[6614]=> string(6) "34,908" } 
Failed to run query: (0) 
array(2) { 
[6613]=> string(8) "12.04.71" 
[6614]=> string(8) "23.05.85" } 
array(2) { 
[6613]=> string(6) "16,897" 
[6614]=> string(6) "34,908" } 

So what you’ve got at the moment is this?

$stmt = $mysqli->prepare('UPDATE servicehistorytable SET servicedate = ?, odometer = ?, serviceagent = ?, servicedesc = ? WHERE `id` = ?');
echo "Failed to run query: (" . $mysqli->errno . ") " . $mysqli->error;

it’s odd that you don’t get an error msg… can you replace the second line with:

var_dump($stmt);

and see what you get?

That produces some interesting output…

object(mysqli_stmt)#34 (9) { ["affected_rows"]=> int(0) ["insert_id"]=> int(0) ["num_rows"]=> int(0) ["param_count"]=> int(5) ["field_count"]=> int(0) ["errno"]=> int(0) ["error"]=> string(0) "" ["sqlstate"]=> string(5) "00000" ["id"]=> int(6) } object(mysqli_stmt)#37 (9) { ["affected_rows"]=> int(0) ["insert_id"]=> int(0) ["num_rows"]=> int(0) ["param_count"]=> int(5) ["field_count"]=> int(0) ["errno"]=> int(0) ["error"]=> string(0) "" ["sqlstate"]=> string(5) "00000" ["id"]=> int(7) } 

As I read this, though, you need to have field descriptors for each of the fields that you intend to replace, using ‘s’ and ‘i’ to indicate string or integer respectively. So don’t you need to replace “ssi” with “ssssi” because you’re supplying four strings and an integer? I have no idea if it would cause the error you’re getting, though.

ETA - is the column name “servicedesc” correct in the update query? You seem to get the data out of a column called “servicehistory”.

1 Like

That being the case, I can’t see why you were getting the original error message… $stmt looks to be an object of the correct type.

I have spotted another small problem, although it won’t make any difference to the error. On this line:

$stmt->bind_param("ssi", $servicedate[$rowId], $serviceodometer[$rowId], $serviceagent[$rowId], $servicedesc[$rowId], $rowId);

the first argument is a string which tells bind_param the data types of the variables you want to bind: s for string, i for integer etc… so that argument actually needs to be “sissi”.

Edit: droopsnoot beat me to it :wink:
Edit2: and he’s right about the data type string… I forgot that your odometer readings have commas in them

1 Like

Thanks guys, well the comment about $servicedesc was correct, but I saw that afterwards and changed it to the correct name, same result though.

So I’ve now changed the bind_param to ssssi which is correct (they’re all actually strings aside from the ID)… and it actually seems to be updating- so maybe it was that the bind_param wasn’t seeing a variable type for all the values (is was ssi so now only 3 data types set). ???

Anyway, that seems to be ok now… bit more testing needed. Thanks again!

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.