I have a form that i would like to be processed and sent to my sqlite database. I was trying to store the data in an array which is then sent to the database but i think i need to use an SQL INSERT INTO statement after my submit, i am just unsure on how to implement this properly and if my code is correct so far. I have two different pages:
index.php:
<div id="wrapper">
<div class="banner1">
<h2>Stock Input</h2>
</div>
<form id="form" method="post">
Name:<br>
<input type="text" name="name[0]"/> <br>
Gender:<br>
<input type="text" name="gender[0]"/> <br>
Age:<br>
<input type="number" name="age[0]" min="1" max="99"/> <br>
<input id="submit" type="submit">
</form>
</div>
<div id="results">
<div class="banner2">
<h2>Results</h2>
</div>
<div class="data">
<?php
include 'conn.php';
unset($_POST['submit']);
$data=$_POST;
foreach ($result as $row) {
echo $row['name'] . " ";
echo $row['gender'] . " " ;
echo $row['age'] . "<br>" . " ";
}
?>
</div>
</div>
conn.php
I used an include in my index.php as it was getting messy, not sure if this is proper use but it did the job fine i think. Anyway here is my page that creates or connects to a sqlite database using PDO and prepares and inserts some test data into my array.
<?php
try {
$dbh = new PDO('sqlite:mydb.sqlite3');
$dbh->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$dbh->exec("CREATE TABLE IF NOT EXISTS test (
name VARCHAR(30),
gender VARCHAR(30),
age INTEGER)"
);
$data = array(
array('name' => 'Daniel', 'gender' => 'Male', 'age' => '21')
);
$insert = "INSERT INTO test (name, gender, age)
VALUES (:name, :gender, :age)";
$stmt = $dbh->prepare($insert);
$stmt->bindParam('name', $name);
$stmt->bindParam('gender', $gender);
$stmt->bindParam('age', $age);
foreach ($data as $m) {
$name = $m['name'];
$gender = $m['gender'];
$age = $m['age'];
$stmt->execute();
}
$result = $dbh->query('SELECT * FROM test');
$dbh = null;
}
catch(PDOException $e) {
echo $e->getMessage();
}
?>
I need the user input data to be submitted from the form to the array and sqlite. Think i'm missing some insert statements, could someone help me and guide me where i'm going wrong.
Aucun commentaire:
Enregistrer un commentaire