Richard Kurth
Richard Kurth

Reputation: 77

The script will not save the data to mysql

I am pulling my hair out about this script below. No mater what I do it will not save the data sent to it. I have set the script of many different ways and also tested it like I show below.

I have set the part that sends it to the script like this $pid = 6 $emailmsg = 1 etc.... and it worked. This is what is not making any sense to me. I have used this same script but with different var at least a dozen times in this program with no problem.

This is what the data looks like that I am sending to the script

print_r($_POST);

[emailmsg] => 1 [emailrt] => 2 [pid] => 6 [$emrtid] => 48 [$emmsid] => 46 

This is one of the script that will not send the data to the database

$stmt = $db->prepare("UPDATE options SET pid = ?,emailmsg = ? WHERE id = ?");
echo $stmt->execute(array($_POST['pid'],$_POST['emailmsg'],$_POST['emmsid']));


$stmt = $db->prepare("UPDATE options SET pid = ?, emailrt = ? WHERE id = ?");
$stmt->execute(array($_POST['pid'],$_POST['emailrt'],$_POST['emrtid']));

I also tried it like this

$sql = "UPDATE options SET 
         pid = ?,
         emailmsg = ? 
         WHERE id = ?";
$stmt = $db->prepare($sql);
$stmt->bindValue('1', $_POST['pid'], PDO::PARAM_INT);
$stmt->bindValue('2', $_POST['emailmsg'], PDO::PARAM_STR);
$stmt->bindValue('3', $_POST['emmsid'], PDO::PARAM_INT);
$stmt->execute();

$sql = "UPDATE options SET 
    pid = ?,
    emailrt = ? 
    WHERE id = ?";
$stmt = $db->prepare($sql);
$stmt->bindValue('1', $_POST['pid'], PDO::PARAM_INT);
$stmt->bindValue('2', $_POST['emailrt'], PDO::PARAM_STR);
$stmt->bindValue('3', $_POST['emrtid'], PDO::PARAM_INT);
$stmt->execute();

Upvotes: 1

Views: 82

Answers (2)

jeroen
jeroen

Reputation: 91734

If you literally copied your print_r output to your question, the problem is that your WHERE condition is never met so no row is ever updated.

This:

echo $stmt->execute(array($_POST['pid'],$_POST['emailmsg'],$_POST['emmsid']));

should be:

echo $stmt->execute(array($_POST['pid'],$_POST['emailmsg'],$_POST['$emmsid']));
//                                                                 ^ that's what it looks like in your print_r

The same applies to $_POST['emrtid'] in your second query: $_POST['$emrtid'].

Upvotes: 1

meda
meda

Reputation: 45490

Try like using named parameter:

$stmt = $db->prepare("UPDATE options SET pid = :pid, emailrt = :emailrt WHERE id = :emrtid");
$stmt->execute(array('pid'=>$_POST['pid'],
                     'emailrt'=>$_POST['emailrt'],
                     'emrtid'=>$_POST['emrtid']
));

Upvotes: 0

Related Questions