Commit a5f25d09 authored by Alexander Butenko's avatar Alexander Butenko

New sample CRUD index.php and some readme updates

parent 733bcdaf
<?php <?php
require_once('MysqliDb.php'); require_once('MysqliDb.php');
error_reporting(E_ALL);
$action = 'adddb';
$data = array();
$db = new MysqliDb('localhost', 'root', 'root', 'db'); function printUsers () {
global $db;
$users = $db->get ("users");
if ($db->count == 0) {
echo "<td align=center colspan=4>No users found</td>";
return;
}
foreach ($users as $u) {
echo "<tr>
<td>{$u['id']}</td>
<td>{$u['login']}</td>
<td>{$u['firstName']} {$u['lastName']}</td>
<td>
<a href='index.php?action=rm&id={$u['id']}'>rm</a> ::
<a href='index.php?action=mod&id={$u['id']}'>ed</a>
</td>
</tr>";
}
}
function action_adddb () {
global $db;
$data = Array(
'login' => $_POST['login'],
'customerId' => 1,
'firstName' => $_POST['firstName'],
'lastName' => $_POST['lastName'],
'password' => $db->func('SHA1(?)',Array ($_POST['password'] . 'salt123')),
'createdAt' => $db->now(),
'expires' => $db->now('+1Y')
);
$id = $db->insert ('users', $data);
header ("Location: index.php");
exit;
}
function action_moddb () {
global $db;
$data = Array(
'login' => $_POST['login'],
'customerId' => 1,
'firstName' => $_POST['firstName'],
'lastName' => $_POST['lastName'],
);
$id = (int)$_POST['id'];
$db->where ("customerId",1);
$db->where ("id", $id);
$db->update ('users', $data);
header ("Location: index.php");
exit;
}
function action_rm () {
global $db;
$id = (int)$_GET['id'];
$db->where ("customerId",1);
$db->where ("id", $id);
$db->delete ('users');
header ("Location: index.php");
exit;
}
function action_mod () {
global $db;
global $data;
global $action;
$action = 'moddb';
$id = (int)$_GET['id'];
$db->where ("id", $id);
$data = $db->getOne ("users");
}
$db = new Mysqlidb ('localhost', 'root', '', 'testdb');
if ($_GET) {
$f = "action_".$_GET['action'];
if (function_exists ($f)) {
$f();
}
}
?> ?>
<!DOCTYPE html> <!DOCTYPE html>
...@@ -9,19 +94,32 @@ $db = new MysqliDb('localhost', 'root', 'root', 'db'); ...@@ -9,19 +94,32 @@ $db = new MysqliDb('localhost', 'root', 'root', 'db');
<html lang="en"> <html lang="en">
<head> <head>
<meta charset="utf-8"> <meta charset="utf-8">
<title>untitled</title> <title>Users</title>
</head> </head>
<body> <body>
<?php <center>
$insertData = array( <h3>Users:</h3>
'title' => 'Inserted title', <table width='50%'>
'body' => 'Inserted body' <tr bgcolor='#cccccc'>
); <th>ID</th>
<th>Login</th>
$results = $db->insert('posts', $insertData); <th>Name</th>
print_r($results); <th>Action</th>
?> </tr>
<?php printUsers();?>
</table>
<hr width=50%>
<form action='index.php?action=<?php echo $action?>' method=post>
<input type=hidden name='id' value='<?php echo $data['id']?>'>
<input type=text name='login' required placeholder='Login' value='<?php echo $data['login']?>'>
<input type=text name='firstName' required placeholder='First Name' value='<?php echo $data['firstName']?>'>
<input type=text name='lastName' required placeholder='Last Name' value='<?php echo $data['lastName']?>'>
<input type=password name='password' placeholder='Password'>
<input type=submit value='New User'></td>
<form>
</table>
</center>
</body> </body>
</html> </html>
...@@ -19,7 +19,7 @@ $data = Array ("login" => "admin", ...@@ -19,7 +19,7 @@ $data = Array ("login" => "admin",
"firstName" => "John", "firstName" => "John",
"lastName" => 'Doe' "lastName" => 'Doe'
) )
$id = $db->insert('users', $data) $id = $db->insert('users', $data);
if($id) if($id)
echo 'user was created. Id='.$id; echo 'user was created. Id='.$id;
``` ```
...@@ -39,7 +39,7 @@ $data = Array( ...@@ -39,7 +39,7 @@ $data = Array(
// Supported intervals [s]econd, [m]inute, [h]hour, [d]day, [M]onth, [Y]ear // Supported intervals [s]econd, [m]inute, [h]hour, [d]day, [M]onth, [Y]ear
); );
$id = $db->insert('users', $data) $id = $db->insert('users', $data);
if($id) if($id)
echo 'user was created. Id='.$id; echo 'user was created. Id='.$id;
``` ```
...@@ -91,7 +91,7 @@ echo $user['id']; ...@@ -91,7 +91,7 @@ echo $user['id'];
### Delete Query ### Delete Query
```php ```php
$db->where('id', 1); $db->where('id', 1);
if($db->delete('posts')) echo 'successfully deleted'; if($db->delete('users')) echo 'successfully deleted';
``` ```
### Generic Query Method ### Generic Query Method
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment