1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Warning: mysqli_query() expects at least 2 parameters, 1 given

Discussion in 'PHP' started by pshaw, Apr 1, 2021.

  1. #1
    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
     
    pshaw, Apr 1, 2021 IP
  2. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #2
    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
     
    sarahk, Apr 1, 2021 IP
  3. pshaw

    pshaw Greenhorn

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #3
    The "getprerentdb.php" is accessing the database in other programs, hence the table "numbers" and the field "receiptno" is valid
    why line 8?
     
    pshaw, Apr 1, 2021 IP
  4. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #4
    sarahk, Apr 1, 2021 IP
  5. Efetobor Agbontaen

    Efetobor Agbontaen Active Member

    Messages:
    136
    Likes Received:
    41
    Best Answers:
    5
    Trophy Points:
    85
    #5
    @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)
     
    Efetobor Agbontaen, Apr 2, 2021 IP
  6. JEET

    JEET Notable Member

    Messages:
    3,825
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #6
    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.
     
    JEET, Apr 3, 2021 IP
  7. deathshadow

    deathshadow Acclaimed Member

    Messages:
    9,732
    Likes Received:
    1,998
    Best Answers:
    253
    Trophy Points:
    515
    #7
    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.
     
    deathshadow, Apr 3, 2021 IP
  8. pshaw

    pshaw Greenhorn

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #8
    <?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:
     
    Last edited by a moderator: Apr 4, 2021
    pshaw, Apr 4, 2021 IP
  9. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #9
    Is that code working?
     
    sarahk, Apr 4, 2021 IP
  10. pshaw

    pshaw Greenhorn

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #10
    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!';
    }
    ?>
     
    pshaw, Apr 5, 2021 IP
  11. pshaw

    pshaw Greenhorn

    Messages:
    90
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #11
    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);
    ?>
     
    pshaw, Apr 11, 2021 IP
  12. sarahk

    sarahk iTamer Staff

    Messages:
    28,500
    Likes Received:
    4,460
    Best Answers:
    123
    Trophy Points:
    665
    #12
    Look closely at these two lines
    $receiptno = 'receiptno';
    echo "$receiptno"; echo "<br />"; // prints "receiptno"
    PHP:
    You give the variable $receiptno a string value
     
    sarahk, Apr 11, 2021 IP
  13. Vincent94

    Vincent94 Peon

    Messages:
    2
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    1
    #13
    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:
     
    Vincent94, Dec 11, 2021 IP