-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathcreate.php
More file actions
77 lines (63 loc) · 1.83 KB
/
create.php
File metadata and controls
77 lines (63 loc) · 1.83 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
<!DOCTYPE HTML>
<html>
<head>
<title>PDO Create New Record</title>
</head>
<body>
<!-- just a header -->
<h1>PDO: Create a Record</h1>
<?php
if($_POST){
// include database connection
include 'database.php';
try{
// insert query
$query = "INSERT INTO products SET name=:name, description=:description, price=:price, created=:created";
// prepare query for execution
$stmt = $con->prepare($query);
// bind the parameters
$stmt->bindParam(':name', $_POST['name']);
$stmt->bindParam(':description', $_POST['description']);
$stmt->bindParam(':price', $_POST['price']);
// specify when this record was inserted to the database
$created=date('Y-m-d H:i:s');
$stmt->bindParam(':created', $created);
// Execute the query
if($stmt->execute()){
echo "<div>Record was saved.</div>";
}else{
die('Unable to save record.');
}
}
// show error
catch(PDOException $exception){
die('ERROR: ' . $exception->getMessage());
}
}
?>
<!-- html form here where the product information will be entered -->
<form action='create.php' method='post'>
<table border='0'>
<tr>
<td>Name</td>
<td><input type='text' name='name' /></td>
</tr>
<tr>
<td>Description</td>
<td><textarea name='description'></textarea></td>
</tr>
<tr>
<td>Price</td>
<td><input type='text' name='price' /></td>
</tr>
<tr>
<td></td>
<td>
<input type='submit' value='Save' />
<a href='read.php'>Back to read records</a>
</td>
</tr>
</table>
</form>
</body>
</html>