Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- $db_host = "YOUR_DB_HOST";
- $db_name = "YOUR_DB_NAME";
- $db_user = "YOUR_DB_USER";
- $db_pass = "YOUR_DB_PASS";
- function sql($dbh,$s,$v=array()){ // USE PDO TO QUERY MYSQL DB
- $stmt = $dbh->prepare($s); // prepare statement
- if (!$stmt) die("Database error [prepare]"); // report errors
- foreach($v as $key => $val) $bind = $stmt->bindValue($key,strval($val)); // bind statement
- $exec = $stmt->execute(); // execute statement
- if (!$exec) die("Database error [execute]"); // report errors
- return $stmt; // return results
- }
- $pdo = new PDO("mysql:host=$db_host;dbname=$db_name",$db_user,$db_pass); // open PDO connection to MySQL
- if (!$pdo) die("Database error [connect]"); // report errors
- if(isset($_POST["chosen"])) $chosen=$_POST["chosen"]; // get the post element
- if(isset($chosen)){ // if any items are selected {
- foreach($chosen as $key => $val){ // for each item {
- $q = "insert into tbl_orders (item_code) values (:b_code)"; // insert SQL with placeholder
- $s = sql($pdo,$q,array(":b_code"=>$val)); // run the query with bind variable
- } // }
- echo "Inserted the following product codes: ".implode(",",$chosen); // show which were inserted
- } // }
- ?>
- <!DOCTYPE html>
- <html>
- <body>
- <form action="#" method="post">
- <table>
- <tr><th>Code</th><th>Name</th><th>Description</th></tr>
- <?php
- $q = "select item_code, item_name, item_desc from tbl_products order by item_code"; // select SQL
- $s = sql($pdo,$q); // run the query
- while($r=$s->fetch(PDO::FETCH_ASSOC)) { // for each result {
- echo "<tr>"; // create row
- echo "<td><input type='checkbox' name='chosen[]' value='".$r["item_code"]."'/></td>"; // show checkbox
- echo "<td>".$r["item_code"]."</td>"; // show code
- echo "<td>".$r["item_name"]."</td>"; // show name
- echo "<td>".$r["item_desc"]."</td>"; // show desc
- echo "</tr>"; // end row
- } // }
- ?>
- </table>
- <input type="submit" value="Go" />
- </form>
- </body>
- <?php
- $pdo = null; // close PDO connection
- ?>
Add Comment
Please, Sign In to add comment