not updating

propz

New Member
This code is just to update the receiptno by adding 1 and displaying the value of receiptno.
The only prob, it does neither.?? says "updated but NOT

<?php
$link = mysqli_connect("localhost", "root", "", "homedb");
// Check connection
if($link === false){ die("ERROR: Could not connect. " . mysqli_connect_error()); }

$id = 'id';
$receiptno='';

/* Perform a query, check for error */
$sql = "UPDATE control SET
receiptno = '$receiptno' + 1 where id='$id'";
echo $receiptno;
if(mysqli_query($link, $sql)){ echo "updated"; }
else { echo "ERROR: Could not able to execute $sql. " . mysqli_error($link); }
// Close connection
mysqli_close($link);
?>
 

Cromewell

Administrator
Staff member
This means the query executed without error, but as you are seeing, does not mean it worked.

If your code is an exact copy, $receiptno is an empty string and $id=id which makes your query "UPDATE control SET receiptno = '' + 1 where id='id'";

'' + 1 is 1. So if you are expecting 2 but getting 1, that is probably your issue.
Code:
drop table if exists control;
create table control (id integer, receiptno integer);
insert into control (id, receiptno) values (0, 1);
update control set receiptno = '' + 1 where id = 0;
select * from control;

1 row

id | receiptno
---+-----------
0  | 1
 
Last edited:
Top