-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreate_auth.php
More file actions
61 lines (57 loc) · 2 KB
/
Copy pathcreate_auth.php
File metadata and controls
61 lines (57 loc) · 2 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
<!--
Jimmy Silva, 4/4/2024, IT202-006, Web Project Phase 4 jns@njit.edu
-->
<?php
$toyCode = filter_input(INPUT_POST, 'toyCode', FILTER_DEFAULT);
$toyName = filter_input(INPUT_POST, 'toyName', FILTER_DEFAULT);
$description = filter_input(INPUT_POST, 'description', FILTER_DEFAULT);
$price = filter_input(INPUT_POST, 'price', FILTER_VALIDATE_FLOAT);
$toyCategoryID = filter_input(INPUT_POST, 'toyCategory',FILTER_DEFAULT);
$sale = 1;
$error_m = '';
// validating
if ($price === FALSE) {
$error_m .= 'Price must be a valid number. <br>';
} else if ( $price > 1000 ){
$error_m .= 'Price of the toy must be a less then 1000$. <br>';
} else if ($price <= 0){
$error_m .= 'Price must be greater than zero. <br>';
}
if ($toyName == NULL || $description == NULL) {
$error_m .= "Invalid product data. Check all fields and try again.";
}
if($error_m != '') {
include('create.php');
exit();
}
// apply formatting
$price = number_format($price, 2);
require_once('database_njit.php');
$db = getDB();
//check if toyCode is unique, sql query
$query = 'INSERT INTO toy
(toyCategoryID, toyCode, toyName, onSale, description, price, dateCreated)
VALUES
(:toy_category_id, :toy_code, :toy_name,:toy_sale,:description,:price,NOW())';
$statement = $db->prepare($query);
$statement->bindValue(':toy_category_id', $toyCategoryID);
//line that potentially can cause error.
$statement->bindValue(':toy_code', $toyCode);
//
$statement->bindValue(':toy_name', $toyName);
$statement->bindValue(':toy_sale', $sale);
$statement->bindValue(':description', $description);
$statement->bindValue(':price', $price);
//$statement->bindValue(':date_created', 'NOW()');
try{
$success = $statement->execute();
//echo '<p>you are connected to the njit database!</p>';
} catch(PDOException $ex) {
$error_m .= 'duplicate entry of toy code, create a different code.';
include('create.php');
exit();
}
$statement->closeCursor();
include('shop_database.php')
//echo print_r($toyCode);
?>