Well hello, I'm just trying to update (add 1) to the "receiptno" field in "numbers" table and display the new receiptno. At a loss ------------------------------------------------------- the code: <?php //Open a new connection to the MySQL server require_once "getprerentdb.php"; echo "<center>";echo date('m/d/y'); $id="id"; $receiptno = "receiptno +1"; $sql = "UPDATE numbers SET receiptno='$receiptno' Where id='".$_POST['id']."'"; mysqli_query($sql) or die(mysql_error()); echo "Record for receiptno ".$_POST["receiptno"]." has been updated"; echo "receiptno ".$_POST["receiptno"]." "; ?> ----------------------------------------------------------------------------- errors: Notice: Undefined index: id on line 8 Warning: mysqli_query() expects at least 2 parameters, 1 given on line 9 Fatal error: Uncaught Error: Call to undefined function mysql_error() on line 9
Mysqli is expecting a database connection to be passed. $sql = "UPDATE numbers SET receiptno='$receiptno' Where id='".$_POST['id']."'"; PHP: should be using filter_input to ensure no dodgy values are being passed ref: https://www.php.net/manual/en/function.filter-input.php
The "getprerentdb.php" is accessing the database in other programs, hence the table "numbers" and the field "receiptno" is valid why line 8?
I don't use mysqli so had to check the docs you have mysqli_query($sql) or die(mysql_error()); PHP: You need mysqli->query($sql) or die(mysqli_error()); PHP: ref: https://www.php.net/manual/en/mysqli.query.php
@sarahk respose should solve the Fatal Error. For Line 8, you have this $_POST['id']. That undefined Notice means that id does not exist in the $_POST var. To check, print the $_POST variable like this: print_r($_POST)
First error, undefined index is coming because your array $_POST does not has an 'id' field posted. Check your HTML form to see if you have something like this: <input type="hidden" name="id" value="123" /> or <input type="text" name="id" value="123" /> The part 'name="id"' is the important part. Second error is coming because you are not passing database connection link to mysqli_query function. It should be: mysqli_query( $conn, $sql ); $conn should be the connection link generated by your included php file getprerentdb.php The variable could be named something else, check the file to see what they have. The last error is coming because "MySQL" extension is not loaded. Does not needs to be. Change that line to: die(mysqli_error()); You have a missing "I" in that code.
Where's your SQL connection object? The first parameter of the derpy outdated STOP USING IT NOW function based version should be the object you created when you ran mysqli_connect. And no, crapping in filter_input isn't the blasted answer either, they should be using the prepare/execute model! Of course the presence of that disphit <center> tag that hasn't existed in "real" HTML since 1997, lack of htmlspeicalchars, etc, etc, indicates 20 year old code practices. Lemme guess, trying to update a pathetically outdated and broken ****-storm to run on modern PHP engines? Guessing WILDLY (untested, may have typos) <?php //Open a new connection to the MySQL server require_once('getprerentdb.php'); // assuming the SQL connection is called $db echo '<time>', date('m/d/y'), '</time>'; $stmt = $db->prepare(' UPDATE numbers SET receiptno = recieptno + 1 WHERE id = ? '); $stmt->bind_param("i", $_POST['id']); $stmt->execute(); if ($stmt->num_rows) { echo 'Record for receiptno ', htmlspecialchars($_POST["receiptno"]), ' has been updated'; } else { echo 'Invalid ID ', htmlspecialchars($_POST["receiptno"]), ' or other error occurred!'; } Code (markup): Again mysqli does not provide a derpy global connection like the outdated mysql_ trash... and seriously do yourself a favor and ditch the garbage function version of mysqli. Hell, do yourself a REAL favor and switch to PDO.
<?php $host='localhost'; $username='root'; $password=''; $dbname = "prerentdb"; $db=mysqli_connect($host,$username,$password,"$dbname"); if(!$db) { die('Could not Connect MySql Server:' .mysql_error()); } echo '<time>', date('m/d/y'), '</time>'; $stmt = $db->prepare(' UPDATE numbers SET receiptno = recieptno + 1 WHERE id = ? '); $stmt->bind_param("i", $_POST['id']); // Call to a member function bind_param() on bool in $stmt->execute(); if ($stmt->num_rows) { echo 'Record for receiptno ', htmlspecialchars($_POST["receiptno"]), ' has been updated'; } else { echo 'Invalid ID ', htmlspecialchars($_POST["receiptno"]), ' or other error occurred!'; } ?> PHP:
Thanks 4 your generous help, just doing this to retain grey matter. U had "$dbname" & I thought no password is required and ROOT is default. I like the code but 2 get it 2 work. ================================ this is current code: <?php $host='localhost'; $dbname = 'prerentdb'; $db=mysqli_connect($host,'$dbname'); if(!$db) { die('Could not Connect MySql Server:' .mysql_error()); } echo '<time>', date('m/d/y'), '</time>'; $stmt = $db->prepare(' UPDATE numbers SET receiptno = recieptno + 1 WHERE id = ? '); $stmt->bind_param("i", $_POST['id']); // Call to a member function bind_param() on bool in $stmt->execute(); if ($stmt->num_rows) { echo 'Record for receiptno ', htmlspecialchars($_POST["receiptno"]), ' has been updated'; } else { echo 'Invalid ID ', htmlspecialchars($_POST["receiptno"]), ' or other error occurred!'; } ?>
My code does what I want except print the receiptno on the report. I search but don't find example for this. Please ---------------------------------------------- the code: <?php $link = mysqli_connect("localhost", "root", "", "prerentdb"); // Check connection if($link === false){ die("ERROR: Could not connect. " . mysqli_connect_error()); } echo "<center>";echo date('m/d/y'); echo "<br />"; $id='id'; $receiptno = 'receiptno'; echo "$receiptno"; echo "<br />"; // prints "receiptno" $sql = " UPDATE numberstbl SET receiptno = receiptno + 1 where id=$id "; if(mysqli_query($link, $sql)){ echo "receiptno was updated successfully."; } else { echo "ERROR: Could not able to execute $sql. " . mysqli_error($link); } // Close connection mysqli_close($link); ?>
Look closely at these two lines $receiptno = 'receiptno'; echo "$receiptno"; echo "<br />"; // prints "receiptno" PHP: You give the variable $receiptno a string value
Hello, This should fix it: <?php //Open a new connection to the MySQL server require_once "getprerentdb.php"; echo "<center>";echo date('m/d/y'); $id= isset($_POST['id']) ? htmlentities($_POST['id']) : ''; $receiptno = isset($_POST["receiptno"]) ? htmlentities($_POST["receiptno"]) : ''; $sql = "UPDATE numbers SET receiptno = receiptno+1 Where id='".$id."'"; mysqli_query($sql) or die(mysqli_error()); echo "Record for receiptno $receiptno has been updated"; echo "receiptno $receiptno"; ?> PHP: