0

I am very familiar with PHP, but this is my first time using the mysqli library. I am trying to insert a row into the database and it just does not insert. My code look like this (database login details changed for security):

... // $_REQUEST variables processed
$oConn = new mysqli("localhost","user","password","mydatabase") or die("Error " . mysqli_error($oConn));

$rProvider = $oConn->query("select * from providers where id = $iProviderID");
$aProvider = mysqli_fetch_array($rProvider);
// $aProvider has all the information from the provider that I wanted, so my database connection is working

$oConn->query("insert into bookings (provider_id, provider_rates_id, secret, preferred_date, alternate_date, adults, children, transfer_required, firstname, lastname, email, phone, comments) values
    ($iProviderID, $iRatesID, $sSecret, '$sDate', '$sAltDate', $iAdults, $iChildren, $iTransfer, '$sFirstNameSQL', '$sLastNameSQL', '$sEmailSQL', '$sPhoneSQL', '$sCommentsSQL'");

$iBookingID = $oConn->insert_id;

I am getting no error and $iBookingID is 0. The row just doesn't get inserted into the database. I have gone through the PHP manual and similar posts on StackExchange, but have not been able to resolve this issue.

3
  • @Amal Murali, thank you very much. Your suggestion helped me to fix two problems in my query which was causing it to fail. I would think that it would output these errors automatically, but obviously I was wrong. Again, thank you. Commented Nov 13, 2013 at 12:44
  • As per @Amul Murali's comment, it seems that mysqli does not automatically output errors and you must explicitly force it to do so by adding "or (die $mysqli->error);" after your query (where $mysqli is replaced with your object variable. In my case $oConn). Commented Nov 13, 2013 at 12:46
  • Take a look at the " ' " in your insert statement. Shouldn't the fieldnames be quoted, and the values only quoted accoding to type? insert into ('field1','field2',...) values (123, 123, 'secret','2013-01-01', ...) Commented Nov 13, 2013 at 12:47

4 Answers 4

1

typo-u forgot to add single quotes and also a bracket.

$oConn->query("insert into bookings (provider_id, provider_rates_id, secret, preferred_date, alternate_date, adults, children, transfer_required, firstname, lastname, email, phone, comments) values
    ('$iProviderID', '$iRatesID', '$sSecret', '$sDate', '$sAltDate', '$iAdults', '$iChildren', '$iTransfer', '$sFirstNameSQL', '$sLastNameSQL', '$sEmailSQL', '$sPhoneSQL', '$sCommentsSQL')");
Sign up to request clarification or add additional context in comments.

Comments

1
    ... // $_REQUEST variables processed
    $oConn = new mysqli("localhost","user","password","mydatabase") or die("Error " . $oConn->error);

    $oConn->query("insert into bookings (provider_id, provider_rates_id, secret, preferred_date, alternate_date, adults, children, transfer_required, firstname, lastname, email, phone, comments) values
        ('$iProviderID', '$iRatesID', '$sSecret', '$sDate', '$sAltDate', '$iAdults', '$iChildren', '$iTransfer', '$sFirstNameSQL', '$sLastNameSQL', '$sEmailSQL', '$sPhoneSQL', '$sCommentsSQL')");

    $iBookingID = $oConn->insert_id;
    echo $iBookingID;

1 Comment

if you only want to insert data so above query is perfect for you.
0

there's a typo on your code, you're missing to close the ")" of the values part of your query.

The correct, query:

$oConn->query("insert into bookings (provider_id, provider_rates_id, secret, preferred_date, alternate_date, adults, children, transfer_required, firstname, lastname, email, phone, comments) values ($iProviderID, $iRatesID, $sSecret, '$sDate', '$sAltDate', $iAdults, $iChildren, $iTransfer, '$sFirstNameSQL', '$sLastNameSQL', '$sEmailSQL', '$sPhoneSQL', '$sCommentsSQL');");

Comments

0

If this is not a php error, you can try using echo $oConn->error to get mysql query error!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.