Repository navigation
Expand file tree
/
Copy pathprocess_post.php
More file actions
55 lines (44 loc) · 1.95 KB
/
Copy pathprocess_post.php
File metadata and controls
55 lines (44 loc) · 1.95 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
<?php
session_start();
if (isset($_POST['submit'])) {
// Database connection setup (replace with your credentials)
$db_host = 'localhost';
$db_username = 'root';
$db_password = '';
$db_name = 'saplingo';
$conn = mysqli_connect($db_host, $db_username, $db_password, $db_name);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
// Get user input
$item_name = mysqli_real_escape_string($conn, $_POST['item_name']); // Sanitize input
$description = mysqli_real_escape_string($conn, $_POST['description']);
$unit_price = floatval($_POST['unit_price']); // Ensure it's a float
$quantity = intval($_POST['quantity']); // Ensure it's an integer
$uid = $_SESSION['uid']; // Assuming you have a 'uid' key in your session.
// Check for file upload
$target_dir = "uploads/";
$target_file = $target_dir . basename($_FILES["Simage"]["name"]);
if (move_uploaded_file($_FILES["Simage"]["tmp_name"], $target_file)) {
// Insert product data into the database using prepared statement
$sql = "INSERT INTO sales_items (Simage, item_name, quantity, description, unit_price, uid) VALUES (?, ?, ?, ?, ?, ?)";
$stmt = mysqli_prepare($conn, $sql);
if ($stmt) {
mysqli_stmt_bind_param($stmt, "ssssdi", $target_file, $item_name, $quantity, $description, $unit_price, $uid);
if (mysqli_stmt_execute($stmt)) {
// Add a header to redirect to shop.php after successful posting
header("Location: shop.php?product_posted=1");
exit(); // Ensure that no further code is executed after the redirect
} else {
echo "Error: " . mysqli_stmt_error($stmt);
}
mysqli_stmt_close($stmt);
} else {
echo "Error preparing statement: " . mysqli_error($conn);
}
} else {
echo "Error uploading image.";
}
mysqli_close($conn);
}
?>