Hi everyone hope you are ok
i have mistake in my php and i cant find it. please help me
I need to create 3 php . create table , insert_pdo ands select data.
That is my html:

`       <form action="" method="get"> <!-- Search box --> <input  type="text" name="search_text" id="search_text" placeholder="Search"/> <!-- Search button --> <input  type="image" name="search_button" id="search_button" src="search-button.png" /> </form><!-- search form end --> <!-- search end --> </ul> </nav><!-- end navigation --> <div class="clearer"></div> <!-- menu end --> </div> <!-- header end --> <div class="body"> <!-- body start --> <br> <br> <br> <!--   Jsfiddle.net. (2017). jQuery check uncheck all checkboxes - JSFiddle.
 [online] Available at: http://jsfiddle.net/NogginBox/ScnQT/1/ [Accessed 13 Mar. 2017].--> <h3 class="sub-h3">Preferences</h3> <form action="insert_pdo.php" method="POST" name="my-form" id="my-form" class="my-form"> <!-- heading 4 size -comunications --> <fieldset class="fieldset-sub"> <h4 class="sub-h4">Newsletter</h4> <!-- label and checkbox subscribe --> <p><label class="label-sub">Please enter your email to subscribe</label><input type="text" class="email" id="email" name="email" placeholder="NAME@EXAMPLE.COM" style="width:150px; margin-left:50px" > </p> <br><br><br> <!-- heading 4 size -comunications --> <h4>Communications</h4><br> <!-- select all checkbox --> <label><input type="checkbox" id="select-all" class="select-all" name="checkbox[]" value="select_all"/> Select all</label><br><br><br> <p><label class="label-sub">Email me when items in my wishlist drop in price</label><input type="checkbox" id="checkbox_subscribe" name="checkbox[]" value="checkbox_subscribe1" style="margin-left:290px;" /> </p> <br><br><br> <p><label class="label-sub"> Email me about the items in my wishlist</label><input type="checkbox"id="checkbox-subscribe" name="checkbox[]" value="checkbox_subscribe2" style="margin-left:340px;"/></p> <br><br><br> <p><label class="label-sub">Email me if I have items in my basket, and not completed checkout</label><input type="checkbox" id="checkbox[]" name="checkbox" value="checkbox_subscribe3" style="margin-left:194px;" /></p> <br><br> <p><strong><label class="label-sub">What kind of books you would like to subscribe</label></strong></p><br><br> <ul class="checkbox-grid"> <li><input type="checkbox" name="checkbox[]" value="art" /><label for="checkbox">Art</label> <li><input type="checkbox" name="checkbox[]" value="architecture" /><label for="checkbox">Architecture</label> <li><input type="checkbox" name="checkbox[]" value="biography" /><label for="checkbox">Biography</label> <li><input type="checkbox" name="checkbox[]" value="businessFinance" /><label for="checkbox">Business & Finance</label> <li><input type="checkbox" name="checkbox[]"  value="scienceFiction" /><label for="checkbox">Science fiction</label> <li><input type="checkbox" name="checkbox[]"  value="drama"/><label for="checkbox">Drama</label> <li><input type="checkbox" name="checkbox[]"  value="romance" /><label for="checkbox">Romance</label> <li><input type="checkbox" name="checkbox[]"  value="mystery" /><label for="checkbox">Mystery</label> <li><input type="checkbox" name="checkbox[]"  value="horror" /><label for="checkbox">Horror</label> <li><input type="checkbox" name="checkbox[]"  value="history" /><label for="checkbox">History</label> <li><input type="checkbox" name="checkbox[]"  value="dictionaries" /><label for="checkbox">Dictionaries</label> <li><input type="checkbox" name="checkbox[]"  value="fantasy" /><label for="checkbox">Fantasy</label> <li><input type="checkbox" name="checkbox[]"  value="journals" /><label for="checkbox">Journals</label> <li><input type="checkbox" name="checkbox[]"  value="children" /><label for="checkbox">Children's</label> <li><input type="checkbox" name="checkbox[]"  value="science" /><label for="checkbox">Science</label> <li><input type="checkbox" name="checkbox[]"  value="poetry" /><label for="checkbox">Poetry</label> </ul> <br><br><br> <!-- save preferences button --> <button class="submit" id="submit"  style=" width:150px; margin-left:1000px;">Safe preferences</button> <br><br><br>`

Create table:

<?php

include "connect_pdo.php";

$sql = "CREATE TABLE IF NOT EXISTS preferences (id INT NOT NULL AUTO_INCREMENT PRIMARY KEY, email VARCHAR(200) NOT NULL,
 checkbox VARCHAR(200) NOT NULL)";

$sq = $pdo->query($sql);

if  ($sq) {
    echo 'created';
}
?>

Insert_pdo

<?php
$email = $_POST['email'];
$checkbox = $_POST['checkbox'];

// You have to loop through the array of checked box values ...
$check="";
foreach($check as $entry){
$check .= $entry.",";
}

try {
include "connect_pdo.php";

$query = $pdo->prepare('INSERT INTO preferences (email, checkbox) VALUES(:email,:check)');

header("Location: preferences.html");
}
catch(PDOException $e) {
echo 'Error: ' .$e->getMessage();
}
?>

select_data php

<?php

include "connect_pdo.php";

$query = $pdo->prepare("SELECT * FROM preferences");
$query->execute();
$query->setFetchMode(PDO::FETCH_NUM);
echo"<table border=\"1\" width\100\">";
while($r = $query->fetch()){

  $test1 = $r[1];
  $test2 = $r[2];
  $test3 = $r[3];

echo"<tr><td>$test1</td><td>$test2</td><td>$test3</td><</tr>";

}
echo"</table>";
?>

Dani AI

Generated

Observed problems and a compact repair plan that builds on 's request for real error output and 's suggestions.

Primary issues found in the thread:

  • The form has inconsistent names/IDs for checkboxes (some use checkbox[], one uses checkbox) so POST data may be missing or not an array.
  • The insert script loops the wrong variable (the loop uses the string variable instead of the incoming array), never executes the prepared statement, and calls header() before inserting.
  • The select script mixes up fetch indexes and has malformed echo/HTML (numeric fetch indexes start at 0; table has only id,email,checkbox).
  • PDO error mode is not set, so SQL/PDO errors can be silent.

Concrete checklist to fix the page

  • Make every preference checkbox use the same name with brackets: name="checkbox[]". Fix any inputs where name is missing brackets.
  • In insert_pdo: check $_SERVER['REQUEST_METHOD'] === 'POST', validate the email (for example with filter_var), and handle the checkbox when it is absent.
  • Join checkbox values with implode() (or store JSON) and then prepare + execute the INSERT. Do not call header() until after execute() and follow it with exit;.
  • Enable PDO exceptions during development: set PDO::ATTR_ERRMODE to PDO::ERRMODE_EXCEPTION so errors are visible.
  • In select_data: use PDO::FETCH_ASSOC (or remember that FETCH_NUM starts at 0) and escape output with htmlspecialchars().

Example patterns (short, focused):

if (!empty($_POST['checkbox']) && is_array($_POST['checkbox'])) {
    $checkboxString = implode(',', array_map('trim', $_POST['checkbox']));
} else {
    $checkboxString = '';
}

$stmt = $pdo->prepare('INSERT INTO preferences (email, checkbox) VALUES (:email, :checkbox)');
$stmt->execute(['email' => $email, 'checkbox' => $checkboxString]);
header('Location: preferences.html');
exit;

Debugging tips

  • Add var_dump($_POST) temporarily to confirm what the form actually sent.
  • Turn on PDO exceptions and check webserver/PHP error logs.
  • Consider storing preferences in a normalized table or JSON instead of a single CSV string if multiple selections must be queried later.

These steps resolve the common mistakes noted by and address 's point about posting actual errors; they also correct the variable/name mismatches observed in 's snippets.

Recommended Answers

All 3 Replies

There are several mistakes before we even get to your question.

  1. You posted the title in ALL CAPS WHICH IS KIND OF RUDE (I corrected that)
  2. You posted a mess of unformatted html and expected us to wade through it
  3. You didn't give any indication as to what error you are seeing

Hi sorry abouty the tittle.
Sorry how i posted the html and php i am trying to learn it
i think the error is in create_table.php or insert_pdo.php

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.