-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate.php
108 lines (99 loc) · 3.78 KB
/
create.php
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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
// Include config file
require_once "config.php";
// Define variables and initialize with empty values
$name = $amount = $price = "";
$name_err = $amount_err = $price_err = "";
// Processing form data when form is submitted
if($_SERVER["REQUEST_METHOD"] == "POST"){
// Validate name
$input_name = trim($_POST["name"]);
if(empty($input_name)){
$name_err = "Please enter a name.";
} elseif(!filter_var($input_name, FILTER_VALIDATE_REGEXP, array("options"=>array("regexp"=>"/^[a-zA-Z\s]+$/")))){
$name_err = "Please enter a valid name.";
} else{
$name = $input_name;
}
// Validate address
$input_amount = trim($_POST["amount"]);
if(empty($input_amount)){
$amount_err = "Please enter an amount.";
} else{
$amount = $input_amount;
}
// Validate salary
$input_price = trim($_POST["price"]);
if(empty($input_price)){
$price_err = "Please enter the price of product.";
} else{
$price = $input_price;
}
// Check input errors before inserting in database
if(empty($name_err) && empty($amount_err) && empty($price_err)){
// Prepare an insert statement
$sql = "INSERT INTO customers (name, amount, price) VALUES ('$name', '$amount', '$price')";
if($stmt = $pdo->prepare($sql)){
// Attempt to execute the prepared statement
if($stmt->execute()){
// Records created successfully. Redirect to landing page
header("location: index.php");
exit();
} else{
echo "Something went wrong. Please try again later.";
}
}
// Close statement
unset($stmt);
}
// Close connection
unset($pdo);
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Create Record</title>
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.css">
<style type="text/css">
.wrapper{
width: 500px;
margin: 0 auto;
}
</style>
</head>
<body>
<div class="wrapper">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<div class="page-header">
<h2>Create Record</h2>
</div>
<p>Please fill this form and submit to add products record to the database.</p>
<form action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]); ?>" method="post">
<div class="form-group <?php echo (!empty($name_err)) ? 'has-error' : ''; ?>">
<label>Name</label>
<input type="text" name="name" class="form-control" value="<?php echo $name; ?>">
<span class="help-block"><?php echo $name_err;?></span>
</div>
<div class="form-group <?php echo (!empty($amount_err)) ? 'has-error' : ''; ?>">
<label>Amount</label>
<textarea name="amount" class="form-control"><?php echo $amount; ?></textarea>
<span class="help-block"><?php echo $amount_err;?></span>
</div>
<div class="form-group <?php echo (!empty($price_err)) ? 'has-error' : ''; ?>">
<label>Price($)</label>
<input type="text" name="price" class="form-control" value="<?php echo $price; ?>">
<span class="help-block"><?php echo $price_err;?></span>
</div>
<input type="submit" class="btn btn-primary" value="Submit">
<a href="index.php" class="btn btn-default">Cancel</a>
</form>
</div>
</div>
</div>
</div>
</body>
</html>