Skip to content

Commit 9d2c37b

Browse files
committed
dynamic inserts into the tables
1 parent b746389 commit 9d2c37b

File tree

3 files changed

+28
-1
lines changed

3 files changed

+28
-1
lines changed

controllers/addname.php

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,9 @@
11
<?php
22

3-
var_dump($_POST);
3+
4+
$app['database']->insert('userlists',[
5+
'name' => $_POST['name'],
6+
'address' => $_POST['address']
7+
]);
8+
9+
header('Location: /');

core/database/QueryBuilder.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,5 +21,25 @@ public function selectAllRecords($table)
2121
//var_dump($statement->fetchAll(PDO::FETCH_CLASS, 'className'));
2222
}
2323

24+
/**
25+
* dynamic inserts into the tables
26+
*/
27+
public function insert($table, array $data)
28+
{
29+
$sql = sprintf('insert into %s (%s) values (%s)',
30+
$table,
31+
implode(', ', array_keys($data)),
32+
':'.implode(', :', array_keys($data))
33+
);
34+
try {
35+
36+
$statement = $this->pdo->prepare($sql);
37+
$statement->execute($data);
38+
39+
} catch(Exception $e) {
40+
die('Something went wrong!');
41+
}
42+
}
43+
2444

2545
}

views/index.tpl.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
<form method="post" action="addname">
1414
<input type="text" name="name">
15+
<input type="text" name="address">
1516
<button type="submit">Submit</button>
1617

1718
</form>

0 commit comments

Comments
 (0)