jeudi 5 février 2015

Send data from input field to sqlite database

I would like to have an input field that sends data to my array '$data' which then is sent to my sqlite database. Took some previous advice but in the end i had trouble implementing it, i would just like the data that has been inputted to send to my array and sqlite database. Here is my code so far:


index.php:



<!DOCTYPE html>
<html>
<head>
<title>Data Input</title>
<link href="css/style.css" type="text/css" rel="stylesheet" />
</head>
<body>

<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>

</body>


conn.php



<?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();
}
?>

Aucun commentaire:

Enregistrer un commentaire