This is my index file,
<?php
include 'db.php';
$form = read('form');
//echo '<pre>';
//print_r($forms);
?>
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport"
content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<title></title>
</head>
<body>
<form action="store.php" method="post">
<label>
Username:
<input type="text" placeholder="username" name="username">
</label>
<label>
Password:
<input type="text" placeholder="password" name="password">
</label>
<label>
Email:
<input type="text" placeholder="email" name="email">
</label>
<button type="submit" name="submit">
submit
</button>
</form>
<table>
<?php foreach ($form as $user): ?>
<tr>
<?php foreach ($user as $item) {
echo '<td>' . $item . '</td>';
} ?>
</tr>
<?php endforeach; ?>
</table>
</body>
and this is my db connection file that reads the database:
<?php
$servername = "localhost";
$username = "root";
$password = "mhimlaA#1";
try {
$conn = new PDO("mysql:host=$servername;dbname=mydb", $username, $password);
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
// echo "Connected successfully";
} catch (PDOException $e) {
echo "Connection failed: " . $e->getMessage();
}
function read($where = '')
{
global $conn;
$sql = "SELECT * FROM `form` $where LIMIT 1000;";
$stm = $conn->prepare($sql);
$stm->execute();
return $stm->fetchAll(PDO::FETCH_ASSOC);
}
I have weird problem in here that PhpStorm shows an error on $conn
in the store file. I'm using this file to insert the input text to database:
<?php
print_r($_POST);
unset($_POST['submit']);
include 'db.php';
$form = read('form');
$sql = "INSERT INTO mydb.form(username, password, email) VALUES (:username,:password,:email)";
$stm = $conn->prepare($sql);
$stm->execute($_POST) or die($conn->errorInfo());
header('location: index.php');