0% found this document useful (0 votes)
13 views3 pages

Customer Search Databse in PHP

This PHP document establishes a connection to a MySQL database and provides a web interface for managing customer information. Users can add, delete, search, sort, and display all customers through various forms. The document includes functionality to handle database operations based on user input from the forms.

Uploaded by

bholdi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views3 pages

Customer Search Databse in PHP

This PHP document establishes a connection to a MySQL database and provides a web interface for managing customer information. Users can add, delete, search, sort, and display all customers through various forms. The document includes functionality to handle database operations based on user input from the forms.

Uploaded by

bholdi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1 <?

php
2 $conn = mysqli_connect("localhost","root","","bca");
3 if (!$conn) {
4 die("Connecstion failed: " .mysqli_connect_error());
5 }
6 ?>
7
8 <html>
9 <head>
10 <title>Customer Management</title>
11 </head>
12 <body>
13 <h1>Customer Information Management</h1>
14 <!-- Buttons for actions -->
15 <form method="POST">
16 <input type="submit" name="add_form" value="Add Customer">
17 <input type="submit" name="delete_form" value="Delete Customer">
18 <input type="submit" name="search_form" value="Search Customer">
19 <input type="submit" name="sort_form" value="Sort Customers">
20 <input type="submit" name="display_all" value="Display All Customers">
21 </form>
22
23 <?php
24 // Show Add Customer Form
25 if (isset($_POST['add_form'])) {
26 echo "<h2>Add Customer</h2>";
27 echo '<form method="POST">
28 Customer Id: <input type="text" name="customer_id" required><br<br>
29 Customer Name: <input type="text" name="customer_name" required><br><br>
30 Item Purchased: <input type="text" name="item_purchased" required><br><br>
31 Mobile Number: <input type="text" name="mobile_number" maxlength="10"
pattern="\d{10}" required><br><br>
32 <input type="submit" name="add_customer" value="Add Customer">
33 </form>';
34 }
35
36 // Show Delete Customer Form
37 if (isset($_POST['delete_form'])) {
38 echo "<h2>Delete Customer</h2>";
39 echo '<form method="POST">
40 Customer ID: <input type="text" name="customer_id" required><br>
41 <input type="submit" name="delete_customer" value="Delete Customer">
42 </form>';
43 }
44
45 // Show Search Customer Form
46 if (isset($_POST['search_form'])) {
47 echo "<h2>Search Customer</h2>";
48 echo '<form method="POST">
49 Customer ID: <input type="text" name="customer_id" required><br>
50 <input type="submit" name="search_customer" value="Search Customer">
51 </form>';
52 }
53
54 // Show Display All Customers Table
55 if (isset($_POST['display_all'])) {
56 echo "<h2>All Customers</h2>";
57 echo "<table border='1'>
58 <tr>
59 <th>Customer ID</th>
60 <th>Customer Name</th>
61 <th>Item Purchased</th>
62 <th>Mobile Number</th>
63 </tr>";
64
65 $result = mysqli_query($conn,"SELECT * FROM customers");
66
67 while ($row = mysqli_fetch_assoc($result)) {
68 echo "<tr>
69 <td>{$row['cid']}</td>
70 <td>{$row['cname']}</td>
71 <td>{$row['item']}</td>
72 <td>{$row['mobile']}</td>
73 </tr>";
74 }
75 echo "</table>";
76 }
77
78 // Handle Add Customer
79 if (isset($_POST['add_customer'])) {
80 $customer_id = $_POST['customer_id'];
81 $customer_name = $_POST['customer_name'];
82 $item_purchased = $_POST['item_purchased'];
83 $mobile_number = $_POST['mobile_number'];
84
85 $sql = "INSERT INTO customers (cid, cname, item, mobile)
86 VALUES ('$customer_id', '$customer_name', '$item_purchased', '$mobile_number
')";
87 $result = mysqli_query($conn, $sql);
88 if ($result) {
89 echo "<h3>CUSTOMER ADDED SUCCESSFULLY.</h3>";
90 } else {
91 echo "Error: " ;
92 }
93
94 }
95
96
97 // Handle Delete Customer
98 if (isset($_POST['delete_customer'])) {
99 $customer_id = $_POST['customer_id'];
100 $sql = "DELETE FROM customers WHERE cid = '$customer_id'";
101 $result = mysqli_query($conn, $sql);
102
103 if (mysqli_affected_rows($conn) > 0) {
104 echo "<h3>Customer with id ".$customer_id." removed</h3>";
105 } else {
106 echo "<h3>No record found </h3>";
107 }
108 }
109
110 // Handle Search Customer
111 if (isset($_POST['search_customer'])) {
112 $customer_id = $_POST['customer_id'];
113
114 $sql = "SELECT * FROM customers WHERE cid='$customer_id'";
115 $result = mysqli_query($conn, $sql);
116
117 if (mysqli_num_rows($result) > 0)
118 {
119 while($row = mysqli_fetch_assoc($result))
120 {
121 echo "<strong>Customer Id:</strong>" . $row["cid"]."</br>";
122 echo "<strong>Customer Name:</strong>" . $row["cname"]."</br>";
123 echo "<strong>Purchased Item</h3>:</strong>" . $row["item"]."</br>";

124 echo "<strong>Customer Mobile</h3>:</strong>" . $row["mobile"].


"</br>";
125 echo "</br>";
126 }
127
128 }
129 else
130 {
131 echo "<h3>No Customer found</h3>";
132 }
133 }
134
135 // Handle Sorting by Customer ID
136 if (isset($_POST['sort_form'])) {
137 echo "<h2>All Customers</h2>";
138 echo "<table border='1'>
139 <tr>
140 <th>Customer ID</th>
141 <th>Customer Name</th>
142 <th>Item Purchased</th>
143 <th>Mobile Number</th>
144 </tr>";
145
146 $result = mysqli_query($conn,"SELECT * FROM customers order by cid ASC");
147
148 while ($row = mysqli_fetch_assoc($result)) {
149 echo "<tr>
150 <td>{$row['cid']}</td>
151 <td>{$row['cname']}</td>
152 <td>{$row['item']}</td>
153 <td>{$row['mobile']}</td>
154 </tr>";
155 }
156 echo "</table>";
157
158 }
159 ?>
160
161 </body>
162 </html>
163

You might also like