High Coder High Coder - 4 months ago 11
PHP Question

How to data capture with PDO?

How to data is captured for form (select/option) with PDO?

HTML form:

<form method="post">
<select name="studentactivity">
<option value="Basketball">Basketball</option>
<option value="Football">Football</option>
</select>
</form>


SQL table:

CREATE TABLE `dataset` (
`id` int(11) NOT NULL AUTO_INCREMENT PRIMARY KEY,
`studentactivity` varchar(225) COLLATE utf8_turkish_ci NOT NULL
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_turkish_ci;

Answer
    $db = new PDO('mysql:host=localhost;dbname=yourdb;charset=utf8mb4', 'user', 'password');

    $stmt = $db->prepare("UPDATE dataset SET studentactivity=:studentActivity WHERE id=:id");

    $stmt->bindParam(':studentActivity', $_POST['studentactivity']);

    $stmt->bindParam(':id', $_POST['id']);

    $stmt->execute();
Comments