Jump to content

Help with PDO using more than two $vars


xryan

Recommended Posts

I've been messing with this over a week and it's driving me nuts.. I'm still very new at php! It's entering it into mysql four timeS, the number of variables I have. I'm guessing the problem is in the "foreach," I've tried it about 30 different ways with no luck.

 

 

include "database.php";

$cleaned_file = array("1","2","3","4");

 

$sql = "INSERT INTO test (ONE, TWO, THREE, FOUR)

VALUES (?,?,?,?)";

$stmt = $db->prepare($sql) or die ("ERROR: " . implode(":",

$db->errorInfo()));

 

$stmt->bindParam (1, $ONE);

$stmt->bindParam (2, $TWO);

$stmt->bindParam (3, $THREE);

$stmt->bindParam (4, $FOUR);

 

foreach($cleaned_file as $enter_data) {

$stmt->execute($cleaned_file)  or die ("ERROR: " .

implode(":", $stmt->errorInfo()));

}

echo "Record(s) successfully added.";

 

Link to comment
https://forums.phpfreaks.com/topic/243046-help-with-pdo-using-more-than-two-vars/
Share on other sites

Try:

include "database.php";
$cleaned_file = array("1","2","3","4");

$sql = "INSERT INTO test (ONE, TWO, THREE, FOUR)
VALUES (?,?,?,?)";
$stmt = $db->prepare($sql) or die ("ERROR: " . implode(":",
$db->errorInfo()));

$stmt->bindParam (1, $cleaned_file[0]);
$stmt->bindParam (2, $cleaned_file[1]);
$stmt->bindParam (3, $cleaned_file[2]);
$stmt->bindParam (4, $cleaned_file[3]);

$stmt->execute($cleaned_file)   or die ("ERROR: " .
   implode(":", $stmt->errorInfo()));
  
echo "Record(s) successfully added.";

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.