0

Reccently I have been attempting to insert an array into a database, I keep getting the error message "Notice: Array to string conversion", I not really sure how to resolve this issue, any advice would be greatly appreciated

<?php 

try{
    $db = new PDO("mysql:host=localhost;dbname=test", 'root', '');
    $db ->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}  catch(PDOException $e){
    echo $e->getMessage();
    die();
}


if ($_SERVER["REQUEST_METHOD"] == "POST"){


    $sort  = $_POST['sort'];
    $count = $_POST["count"];
    $error = $_POST["error"];

    $audit = array( ':sort' => $sort,
        ':count' => $count,
        ':error' => $error
    );


    foreach($audit as $completeAudit => $display) {
        //print_r($display);    

        $sql = implode("INSERT INTO `audits` (`sort`, `count`, `error`, `timeentered`) VALUES ('$sort','$count','$error', NOW())");
    }

    $query = $db->prepare($sql);

    $query->execute(array(
        ':sort' => $sort,
        ':count' => $count,
        ':error' => $error
    ));
}

EDIT

$db = new PDO("mysql:host=localhost;dbname=test", 'root', '');
$db ->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

    if ($_SERVER["REQUEST_METHOD"] == "POST"){
        $sql = "INSERT INTO `audits` (`sort`, `count`, `error`, `timeentered`) VALUES (?,?,?, NOW())";
        $stmt = $db->prepare($sql);
        $query->execute(array($_POST['sort'], $_POST["count"], $_POST["error"]));
    }

This is how it looks now, I deleted everything and used code supplied below

7
  • Paste the error message Commented Mar 14, 2016 at 7:30
  • 4
    why are you using implode in query ?? Commented Mar 14, 2016 at 7:31
  • yes implode is the problem here , remove it Commented Mar 14, 2016 at 7:32
  • Notice: Array to string conversion in C:\Program Files (x86)\EasyPHP-DevServer-14.1VC11\data\localweb\my portable files\test\input.php on line 38 Commented Mar 14, 2016 at 7:32
  • I tried without implode as well, same result. Implode was just a shot in the dark Commented Mar 14, 2016 at 7:33

2 Answers 2

1

The problem is probably with the implode() call. It requires an array as parameter but you're passing a string.

However, you're overriding the $sql variable in every iteration inside the loop so I'm not sure what it's supposed to do.

Last thing, your code is subject to SQL inejctions so have a look at using prepared statements.

Sign up to request clarification or add additional context in comments.

Comments

0

this error has nothing to do with PDO - it's just basic PHP syntax.

However, your PDO is wrong as well. Here is the proper code:

$db = new PDO("mysql:host=localhost;dbname=test", 'root', '');
$db ->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

if ($_SERVER["REQUEST_METHOD"] == "POST"){
    $sql = "INSERT INTO `audits` (`sort`, `count`, `error`, `timeentered`) VALUES (?,?,?, NOW())");
    $stmt = $db->prepare($sql);
    $stmt->execute(array($_POST['sort'], $_POST["count"], $_POST["error"]));
}

1 Comment

I deleted everything and used your code, currently getting error messages, Notice: Undefined variable: query Fatal error: Call to a member function execute()

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.