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); ?>
I think you should make a variable before query as below First set $receiptno=0; (Fetch value for this field if NULL or "" then set to 0 or set value in database). $receiptno_count = $receiptno + 1; And then add it to query $sql = "UPDATE control SET receiptno = '$receiptno_count' where id='$id'"; After this fetch data and check ...... i think database will update successfully.
didn't work but this did but didn't display the receiptno value <?php $link = mysqli_connect("localhost", "root", "", "homedb"); // Check connection if($link === false){ die("ERROR: Could not connect. " . mysqli_connect_error()); } $id = 'id'; $receiptno=0; /* Perform a query, check for error */ $sql = "UPDATE control SET receiptno = receiptno + 1 where id=1"; echo "receipt# $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); ?>
You are just update database and echo $receiptno value .... So this value will be same as you are submitting by form for update for example if your value is 2 then in database update value is 2+1 = 3 but because you are trying to display posting value and so echo value will be 2. To display correct value ... after update fetch value again and echo them. After udpate query add one more query $query = mysqli_query("SELECT receiptno from Table WHERE where id=1"); $row = mysqli_fetch_array($query); echo $row["receiptno"]; Then you will see updated value. Hope this will work.
<?php $link = mysqli_connect("localhost", "root", "", "homedb"); // Check connection if($link === false){ die("ERROR: Could not connect. " . mysqli_connect_error()); } $id = 'id'; $receiptno=0; /* Perform a query, check for error */ $sql = "UPDATE crttbl SET receiptno = receiptno + 1 where id=1"; $query = mysqli_query("SELECT receiptno from crttbl WHERE where id=1"); $row = mysqli_fetch_array($query); echo $row["receiptno"]; echo "receipt# $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); ?> receipt# 0updated Warning: mysqli_query() expects at least 2 parameters, 1 given in C:\xampp\htdocs\kirkplace\receiptno.php on line 30 Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in C:\xampp\htdocs\kirkplace\receiptno.php on line 31 Notice: Trying to access array offset on value of type null in C:\xampp\htdocs\kirkplace\receiptno.php on line 32 receipt# 0updated
this is complete code. On commented line 43 value displayed is "0". On commented lines 57-61 value is not displayed. On commented line 77 value displayed is "0". <!DOCTYPE html><html> <head> <title>record payment</title> <html><head> <script type="text/javascript"> var monthNames = [ "January","February","March","April","May","June","July", "August","September","October","November","December" ]; var today = new Date(); var date = monthNames[today.getMonth()] + " - " + today.getDate() + " - " + today.getFullYear(); </script> </head> <body><center> Date:<script type="text/javascript">document.write(date);</script><p> <img src="apt-pic.jpg" alt="apartment" height=100 width=300><p> <?php $link = mysqli_connect("localhost", "root", "", "homedb"); // Check connection if($link === false){ die("ERROR: Could not connect. " . mysqli_connect_error()); } $unit=$_POST['unit']; $amtpaid=$_POST['amtpaid']; $paidday=$_POST['paidday']; $id = ''; $amtpaid = 0; $amtdue = 0; $dueday = ''; $prevbal = 0; $latechg = 0; $paidday = ''; $bizname = ''; $bizstreet = ''; $bizcity = ''; $bizzip = ''; $bizemail = ''; $receiptno = 0; $owed = $amtdue - $amtpaid; /* Perform a query, check for error */ $sql = "UPDATE crttbl SET receiptno = receiptno + 1 where id=1"; echo "receipt# $receiptno"; // line 43 if(mysqli_query($link, $sql)){ echo "updated"; } else { echo "ERROR: Could not able to execute $sql. " . mysqli_error($link); } /* Perform a query, check for error */ $sql = "UPDATE paytbl SET $amtpaid = '$amtpaid', dueday = DATE_ADD(dueday, INTERVAL 1 MONTH), prevbal = '$prevbal', latechg = '$latechg', paidday = '$paidday' WHERE id = '$id'"; if(mysqli_query($link, $sql)){ echo ""; } else { echo "ERROR: Could not able to execute $sql. " . mysqli_error($link); } ?> <?php echo $bizname;echo "<br />"; ?> // line 57 <?php echo $bizstreet;echo "<br />"; ?> <?php echo $bizcity;echo "<br />"; ?> <?php echo $bizzip;echo "<br />"; ?><br> <?php echo $bizemail;echo "<br />"; ?> // line 61 For:<SELECT name="options"> <option value="#990033" style="background-color: Violet;">Rent payment</option> <option value="#003300" style="background-color: Aquamarine;">Background Check</option> <option value="#6600cc" style="background-color: Pink;">Security Deposit Payment</option> <option value="#003300" style="background-color: Aquamarine;">Damages Payment</option> <option value="#990033" style="background-color: Violet;">Late Charges Payment</option> <option value="#003300" style="background-color: Aquamarine;">Court Costs Payment</option> <option value="#6600cc" style="background-color: Pink;">NSF Payment</option> <option value="#990033" style="background-color: Violet;"> </option> </SELECT><p> Tenant paying is: <?php echo $_POST["unit"]; ?> - Amount paid is: <?php echo $_POST["amtpaid"]; ?> - Balance due is:<?php echo $owed; ?><br> // line 77 <b><input type="text" size="35" maxlength="35" name=frm.Name" value="sign" STYLE="color: #000000; font-weight: bold; background-color: #ffccff;" onFocus="this.value=''"><br> <h3>We Thank You</h3> </center></body></html> <?php // Close connection mysqli_close($link); ?> ------------------------------ errors: receipt# 0updatedERROR: Could not able to execute UPDATE paytbl SET 0 = '0', dueday = DATE_ADD(dueday, INTERVAL 1 MONTH), prevbal = '0', latechg = '0', paidday = '' WHERE id = ''. You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near '0 = '0', dueday = DATE_ADD(dueday, INTERVAL 1 MONTH), prevbal = '0', latech...' at line 2
In your form where is receiptno and why you are using $id = 'id' while in update query you are using id = 1 ..... Is id column primary in database?? I think for update in form you have to use hidden value for id (Which row you want to update) and receiptno also. In your given form i am not able to see these two values.
I don't want to look up the latest receipt # and use on form. The receiptno updates just doesn't echo the value. There is 1 record in crttbl table. The code for the form is: <!DOCTYPE html><html lang="en"> <head> </head> <body bgcolor=ccffff"><center> <font size=+2><b> record a payment</font><br> <form action="payment.php" method="post"> <label for="unit">Unit</label> <input type="text" name="unit" id="unit" <label for="amtpaid">Amt paid:</label> <input type="text" name="amtpaid" id="amtpaid"> <label for="paidday">date paid yyyy-mm-dd:</label> <input type="text" name="paidday" id="paidday"><p> <input type="submit" name="submit" value="make a payment"> </form></body></html>