-1

Trying to insert this array facing

Array to string conversion error

but data is inserting in DB

$connect = new PDO("mysql:host=localhost; dbname=dentist", "root", "");
if (isset($_POST['problem'])) { 
$problem = $_POST['problem'];
$tooth = $_POST['tooth'];
$intervention = $_POST['intervention'];

For loop using to insert array

for ($i = 0; $i < count($problem); $i++) {
$query = "INSERT INTO tbl_finds (problem, tooth, intervention) 
VALUES ($problem[$i], $tooth[$i], $intervention[$i])";
$stmt = $connect->prepare($query);
$stmt->execute(array($_POST['problem'], $_POST["tooth"], $_POST["intervention"]));}}
Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98

1 Answers1

0

You are assigning values directly to the query as well as trying to bind them in next line using execute().Don't do that.

Do like below:

$query = "INSERT INTO tbl_finds (problem, tooth, intervention) VALUES (?, ?, ?)";
$stmt = $connect->prepare($query);
foreach($_POST['problem'] as $key=>$value){
    $stmt->execute(array($value, $_POST["tooth"][$key], $_POST["intervention"][$key]));
}

Complete code will be:

$connect = new PDO("mysql:host=localhost; dbname=dentist", "root", "");
if (isset($_POST['problem'])) { 
    $problem = $_POST['problem'];
    $tooth = $_POST['tooth'];
    $intervention = $_POST['intervention'];

    $query = "INSERT INTO tbl_finds (problem, tooth, intervention) VALUES (?, ?, ?)";
    $stmt = $connect->prepare($query);
    foreach($_POST['problem'] as $key=>$value){
        $stmt->execute(array($value, $_POST["tooth"][$key], $_POST["intervention"][$key]));
    }
}

Note:- $_POST['problem'], $_POST["tooth"], $_POST["intervention"] all three are array so when you directly use them to bind as values to the query, you got that error.

Alive to die - Anant
  • 70,531
  • 10
  • 51
  • 98