Hi, please advise how to resolve. I want to print the receiptno on a receipt and it prints
"receiptno". I don't know how to define "receiptno" to use the value in the table.
I'm working with mysqli and having trouble with this code:

$receiptno='receiptno';  

$sql = "UPDATE numbers SET receiptno = receiptno+1 WHERE id=1";  // ************ updates
if ($conn->query($sql) !== true) { echo "Failed to update receiptno"; }
else { echo " "; }
echo "receipt # is " .$receiptno . "<br>";

I am no expert on either PHP or MySQL, but I am fairly certain that that won't work, at least as you describe it. You would have to do a separate query to retrieve the value of receiptno for id = 1 first, then increment that value, then use the UPDATE query to set the new value - all while under a single transaction to prevent any hidden race conditions.

However, it also leads to the question of why you are updating the value of receiptno for an existing entry, or perhaps more cogently, why there is a separate receiptno (which is presumably an identifying value) and id (which is presumably a unique, non-data-driven primary key). If receiptno is UNIQUE, then having both is redundant, meaning that the table itself is poorly normalized.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.