0

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>";
}
?>

Edited by SARDEN

Attachments Screen_Shot_2017-04-05_at_23_16_17.png 129.29 KB
4
Contributors
9
Replies
47
Views
4 Months
Discussion Span
Last Post by diafol
0

Hi,

read the error carefully, it says:

Error: SQLSTATE[HY093]: Invalid parameter number: parameter was not defined.

And look at your prepared statement:

$query = $pdo->prepare('INSERT INTO subscriptions (name, email, subscribe) VALUES(:name, :email, :chk)');

And the execution:

$query->execute(array(':name'=> $name,':email'=> $email, ':subscribe'=> $chk));

As you see you have defined :chk in the prepared statement, and :subscribe in the array that is executed. Fix it and you will solve this error.

0

What's with the array declaration on line 35? Do you need that? Also, knowing where the error is occuring would be helpful.

0

hi i changed like this but now is not displaing to the user which checkboxes he tick and also in select php is i showing just one checkbox checked with ", " but is suppose to show all checked checkboxes with ","

<?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 subscribe (name, email, subscribe) VALUES(:name, :email, :chk)');
$query->execute(array(':name'=> $name,':email'=> $email, ':chk'=> $chk));
header("Location: preferences.html");
}
catch(PDOException $e) {
echo 'Error: ' .$e->getMessage();
}
?>
Attachments Screen_Shot_2017-04-06_at_10_40_57.png 59.99 KB
0
foreach($checkbox1 as $chk1)
{
$chk = $chk1.",";
}

That won't work as you're overwriting $chk with each iteration. So however many you have, you'll end up with the last checkbox only with a trailing comma.

0

If you have a checkbox array:

$checkbox1 = (array) $_POST['check_list'];
$checkstring = implode(",", $checkbox1);
echo $checkstring;

No need to loop.

0

hi thank you so much yes now is working in select is diplaying the checkboxes how i want but now the problem is is not displaying checkbox_count to the user
that part:

$checked_count = count($_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>";
}
2

Sorry keep thinking of other stuff to add...

If you have less than 32 checkboxes and you will never go over this, then you can use the following:

//hardcoding these - but get them from DB table
$options = [1=>"Something", 2=>"Somebody", 4=>"Anybody", 8=>"Nobody", 16=>"Whoever", 32=>"Herself", 64=>"Itself", 126=>"Myself"];
$selected = 103; //(means options 1+2+4+32+64) - comes from stored value in DB for user

$output = '';
foreach($options as $k=>$v){
    $sel = ($k && $selected) ? 'checked' : ''
    $output .= "<label><input type='checkbox' name="check_list[]" value='$k' $sel/> $v</label>\n";
}
//then later on in your html form...

<form ...>
    <?=$output?>
</form>

When you get the form sent - you can retrieve the checked values thus:

$checkboxSum = (isset($_POST['check_list'])) ? array_sum($_POST['check_list']) : 0;

Just a thought. A working demo (of sorts) here: http://demos.diafol.org/bitwise-checkboxes.php - must admit - an old page, when I was younger (my excuse).

Edited by diafol

Votes + Comments
Always great!
0

no its not working like this too :(
do you know any other way

2

Show your code. Your db table schemas. Your form. To say its not working, give up and then ask for a different solution... some people would give up on you, say you.re not working and ask for a different problem to solve.

Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.