Skip to content

Commit e275503

Browse files
committed
vue php
0 parents  commit e275503

30 files changed

+26062
-0
lines changed

README.md

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
Basically it's a simple CRUD project, for Learning Vue.js.
2+
3+
# Created with
4+
Vue.js, PHP, Axios, Bootstrap
5+
6+
# How to setup
7+
8+
1. Create a database
9+
2. import the users.sql file
10+
3. change database connection from `<project_path>/api/v1.php` on line number #7
11+
4. change ULR from `<project_path>/js/app.js` on line number #21, #34, #49, #64 like `http://localhost/<project_path>/api/v1.php?action=<action_leave_as_default>`
12+
13+
Ping me on facebook, if you need any information: [Rudra Sarkar](https://www.facebook.com/R.sark4r)

api/v1.php

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
<?php
2+
3+
// Content Type JSON
4+
header("Content-type: application/json");
5+
6+
// Database connection
7+
$conn = new mysqli("localhost", "root", "", "phpvue");
8+
if ($conn->connect_error) {
9+
die("Database connection failed!");
10+
}
11+
$res = array('error' => false);
12+
13+
14+
// Read data from database
15+
$action = 'read';
16+
17+
if (isset($_GET['action'])) {
18+
$action = $_GET['action'];
19+
}
20+
21+
if ($action == 'read') {
22+
$result = $conn->query("SELECT * FROM `users`");
23+
$users = array();
24+
25+
while ($row = $result->fetch_assoc()) {
26+
array_push($users, $row);
27+
}
28+
$res['users'] = $users;
29+
}
30+
31+
32+
// Insert data into database
33+
if ($action == 'create') {
34+
$username = $_POST['username'];
35+
$email = $_POST['email'];
36+
$mobile = $_POST['mobile'];
37+
38+
$result = $conn->query("INSERT INTO `users` (`username`, `email`, `mobile`) VALUES('$username', '$email', '$mobile')");
39+
40+
if ($result) {
41+
$res['message'] = "User added successfully";
42+
} else {
43+
$res['error'] = true;
44+
$res['message'] = "User insert failed!";
45+
}
46+
}
47+
48+
49+
// Update data
50+
51+
if ($action == 'update') {
52+
$id = $_POST['id'];
53+
$username = $_POST['username'];
54+
$email = $_POST['email'];
55+
$mobile = $_POST['mobile'];
56+
57+
58+
$result = $conn->query("UPDATE `users` SET `username`='$username', `email`='$email', `mobile`='$mobile' WHERE `id`='$id'");
59+
60+
if ($result) {
61+
$res['message'] = "User updated successfully";
62+
} else {
63+
$res['error'] = true;
64+
$res['message'] = "User update failed!";
65+
}
66+
}
67+
68+
69+
// Delete data
70+
71+
if ($action == 'delete') {
72+
$id = $_POST['id'];
73+
$username = $_POST['username'];
74+
$email = $_POST['email'];
75+
$mobile = $_POST['mobile'];
76+
77+
$result = $conn->query("DELETE FROM `users` WHERE `id`='$id'");
78+
79+
if ($result) {
80+
$res['message'] = "User delete success";
81+
} else {
82+
$res['error'] = true;
83+
$res['message'] = "User delete failed!";
84+
}
85+
}
86+
87+
88+
// Close database connection
89+
$conn->close();
90+
91+
// print json encoded data
92+
echo json_encode($res);
93+
die();
94+
95+
?>

0 commit comments

Comments
 (0)