Flobin Flobin - 11 days ago 6
MySQL Question

Fields not entered into mysql database in phpmailer script

I am editing a pre-made script for iDEAL payments which is available here. What I’m trying to do is add a few fields to the form and database. The extra fields show up fine in the email template, but they are not entered into the database. The script works with PHPmailer.

I think this is where the problem is:

$stmt = $db->prepare("INSERT INTO tbl_ideal_payments SET
ID = ?, datumtijd = NOW(),
naamfrom = ?, emailfrom = ?,
naamto = ?, emailto = ?,
bedrag = ?, descr = ?,
mailsubject = ?, mailtekst = ?,
ipadres = ?,
heenreis = ?,
terugreis = ?,
postcode = ?,
factuurnummer = ?,
status = 'open'");
//var_dump($stmt);
$stmt->bind_param('sssssdssss', $session, $naamfrom, $emailfrom, $contact, $emailto, $bedrag, $paydecr, $subject, $mailmsg, $heenreis, $terugreis, $postcode, $factuurnummer, $_SERVER['REMOTE_ADDR']);


The fields that I'm trying to add are heenreis, terugreis, postcode, and factuurnummer.

Full code for the config, form, and above script can be found here (seems a bit much to paste in this post).

What am I doing wrong?

Answer

Count the data type parameters 'sssssdssss' and then count the parameters place holders ?, and then count the columns in the SET clause, they should all be the same number.

Also this situation will have generated an error, but you are not testing for errors

While developing, specially if you are developing on a LIVE server where error reporting should be turned off add these lines at the top of the script, to turn on error reporting.

<?php 
ini_set('display_errors', 1); 
ini_set('log_errors',1); 
error_reporting(E_ALL); 
// if you are using the MYSQLI_ database extension add this also
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);


$stmt = $db->prepare("INSERT INTO tbl_ideal_payments SET 
                ID = ?, datumtijd = NOW(), 
                naamfrom = ?, emailfrom = ?, 
                naamto = ?, emailto = ?, 
                bedrag = ?, descr = ?, 
                mailsubject = ?, mailtekst = ?, 
                ipadres = ?, 
                heenreis = ?, 
                terugreis = ?, 
                postcode = ?, 
                factuurnummer = ?, 
                status = 'open'");

// also add error test
if ( !$stmt ) {
    echo $db->error;
    exit;
}

// now add the 4 missing data type parameters
// I have assumed they are all string !!!
// You should check with your schema what datatype they actually are

$stmt->bind_param('sssssdssssssss', $session, 
                                    $naamfrom, 
                                    $emailfrom, 
                                    $contact, 
                                    $emailto, 
                                    $bedrag, 
                                    $paydecr, 
                                    $subject, 
                                    $mailmsg, 
                                    $heenreis, 
                                    $terugreis, 
                                    $postcode, 
                                    $factuurnummer, 
                                    $_SERVER['REMOTE_ADDR']
                );
Comments