Posting to a database using mysqli

I am learning php and trying to do the following work:

<?php
require_once("db_connect.php");

    // TODO - Check that connection was successful.

    $dname = $_POST["dname"];
    $daddress = $_POST["daddress"];


    $stmt = $mysqli->prepare("INSERT INTO test (dname, daddress) VALUES (?, ?)");

    // TODO check that $stmt creation succeeded

    // "s" means the database expects a string
    $stmt->bind_param("s", $dname, $daddress);

    $stmt->execute();

    $stmt->close();

    $mysqli->close();
?>

It works with only one bind_param, but not 2. If $ daddress has been removed from the code, it sends messages. The form contains 26 posts in the database. I am doing this with 2 at the moment to keep it minimal.

When submitting a form, the following error appears.

Warning: mysqli_stmt :: bind_param () [mysqli-stmt.bind-param]: The number of elements in the type definition line does not match the number of binding variables in / home / mymotorsportco / public _html / entry / actions / entry. php on line 15

+4
source share
3 answers

PHP:

, ,

i - integer

d - double

s - string

b - ​​

, . , ,

$stmt->bind_param("ss", $dname, $daddress);
+4

, . :

 $stmt->bind_param("ss", $dname, $daddress);

, , . , , int:

 $stmt->bind_param("si", $dname, $daddress);

, . !

+3

You have 2 rows not 1.

$stmt->bind_param("ss", $dname, $daddress);
+2
source

Source: https://habr.com/ru/post/1627528/


All Articles