Master Guide on Mysqli prepare errors-php Tutorial

Source: Internet
Author: User
Please advise me if I haven't written a program about the Mysqli prepare error for a long time. due to the many advantages of the mysqli class database upgraded by php5.0, I want to use the mysqli method to operate the database, after checking materials and reading manuals on the Internet, browsing and deleting can be done normally, but the insertion function always prompts an error: Fatal error: Call to a member function prepare () on null in D: \ xampps \ htdocs \ txl \ addbook. php on line 20.
I am dying. can't I solve this problem? do I have to give up Mysqli? Please give me some advice on where my code is wrong? Or a server problem? (The server I use is the Xampps1.9.7 Integrated Environment)

If ($ _ POST ['contacts'] = "true ")
{
$ Stmt = $ mysqli-> prepare ("insert into contactsuser (bookUname, bookDep, bookDuty, bookphone, bookTel, bookEmail, bookGid) VALUES (?, ?, ?, ?, ?, ?, ?) ");
$ Stmt-> bind_param ('sssd', $ bookUname, $ bookDep, $ bookDuty, $ bookphone, $ bookTel, $ bookEmail, $ bookGid );

$ BookUname = $ _ POST ['bookuname'];
$ BookDep = $ _ POST ['bookdep'];
$ BookDuty = $ _ POST ['bookduty'];
$ Bookphone = $ _ POST ['bookphone'];
$ BookTel = $ _ POST ['booktel'];
$ BookEmail = $ _ POST ['bookemail'];
$ BookGid = $ _ POST ['bookgid'];

/* Execute prepared statement */
$ Stmt-> execute ();
Printf ("% d Row inserted. \ n", $ stmt-> affected_rows );
/* Close statement and connection */
$ Stmt-> close ();
Exit;
}


Reply to discussion (solution)

$ Mysqli is not a mysqli object

Try the process-oriented method:

$stmt = mysqli_prepare($link, "INSERT INTO contactsuser(bookUname, bookDep, bookDuty,bookphone,bookTel,bookEmail,bookGid) VALUES (?, ?, ?, ?, ?, ?, ?)")


I did not forget to include the database link file in the program just now, but there are still errors after the inclusion, including the change to the process-oriented style:
Warning: mysqli_stmt_bind_param (): Number of elements in type definition string doesn't match number of bind variables in D: \ xampps \ htdocs \ txl \ addbook. php on line 24

The procedure is as follows:
$ Link = mysqli_connect ('localhost', 'root', 'root', 'ssssbook ') or die ('unale to connection ');
$ Stmt = mysqli_prepare ($ link, "insert into contactsuser (bookUname, bookDep, bookDuty, bookphone, bookTel, bookEmail, bookGid) VALUES (?, ?, ?, ?, ?, ?, ?) ");
Mysqli_stmt_bind_param ($ stmt, 'sssd', $ bookUname, $ bookDep, $ bookDuty, $ bookphone, $ bookTel, $ bookEmail, $ bookGid );
$ BookUname = $ _ POST ['bookuname'];
$ BookDep = $ _ POST ['bookdep'];
$ BookDuty = $ _ POST ['bookduty'];
$ Bookphone = $ _ POST ['bookphone'];
$ BookTel = $ _ POST ['booktel'];
$ BookEmail = $ _ POST ['bookemail'];
$ BookGid = 1;
Mysqli_stmt_execute ($ stmt );
Mysqli_stmt_close ($ stmt );

I also checked the field type of the database. except that the primary key bookid is the automatically added int type, the bookGid is the int type, and the other is the varchar type.

VALUES (?, ?, ?, ?, ?, ?, ?)
$ Stmt, 'sssd', $ bookUname, $ bookDep, $ bookDuty, $ bookphone, $ bookTel, $ bookEmail, $ bookGid
The seven question marks correspond to eight parameters ..... Add a question mark and try again.

mysqli_stmt_bind_param($stmt,'ssssssd', $bookUname, $bookDep, $bookDuty, $bookphone, $bookTel, $bookEmail, $bookGid);

Http://php.net/manual/zh/mysqli-stmt.bind-param.php

The number of variables and length of string types must match the parameters in the statement.

The upstairs is correct. it should be ssssssd.

Great! Thank you for your help! In particular, the question "ssssssd" mentioned by saint_leer and misakaqunianx... has been mentioned, and I have never understood what this character string means!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.