Hi i am trying to insert pdo multiple checkbox value in one column into database and display it to the user, but i am getting this error
Error: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined.I cant see the error and dont know how to fix it. :(
That is my code :
I have 3 PHP files create table, insert and select
<?php
include "connect_pdo.php";
$sql = "CREATE TABLE IF NOT EXISTS subscriptions (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, name VARCHAR(200), email VARCHAR(200),
subscribe VARCHAR(200))";
$sq = $pdo->query($sql);
if ($sq) {
echo "Table created successfully";
}
?>
<?php
$name=$_POST['name'];
$email=$_POST['email'];
//Counting number of checked checkboxes
$checked_count = count($_POST['check_list']);
$checkbox1=$_POST['check_list'];
$chk="";
echo "You have inserted following ".$checked_count." option(s): <br/>";
echo"</br>";
//Loop to store and display values of individually checked checkbox
foreach($_POST['check_list'] as $selected) {
echo "<p>".$selected ."</p>";
}
foreach($checkbox1 as $chk1)
{
$chk = $chk1.",";
}
try {
include "connect_pdo.php";
$query = $pdo->prepare('INSERT INTO subscriptions (name, email, subscribe) VALUES(:name, :email, :chk)');
$query->execute(array(':name'=> $name,':email'=> $email, ':subscribe'=> $chk));
header("Location: preferences.html");
}
catch(PDOException $e) {
echo 'Error: ' .$e->getMessage();
}
?>
<?php
include "connect_pdo.php";
$query = $pdo->prepare("SELECT * FROM subscriptions");
$query->execute();
$query->setFetchMode(PDO::FETCH_NUM);
echo"<table border=\"1\" width\150\">";
echo '<tr>
<th>Name</th>
<th>Email</th>
<th>Subscriptions</th>
<th>Edit</th>
<th>Delete</th>
</tr>';
while($r = $query->fetch()){
$test1 = $r[1];
$test2 = $r[2];
$test3 = $r[3];
echo "<tr>";
echo '<td>' . $r['1'] . '</td>';
echo '<td>' . $r['2'] . '</td>';
echo '<td>' . $r['3'] . '</td>';
echo '<td><a href="edit.php?id=' . $row['id'] . '">Edit</a></td>';
echo '<td><a href="delete.php?id=' . $row['id'] . '">Delete</a></td>';
echo "</tr>";
}
?>