Home  >  Article  >  Backend Development  >  How to develop a simple customer relationship management system using PHP

How to develop a simple customer relationship management system using PHP

WBOY
WBOYOriginal
2023-09-28 18:07:411773browse

How to develop a simple customer relationship management system using PHP

How to use PHP to develop a simple customer relationship management system

With the development of the Internet and the expansion of enterprise scale, customer relationship management systems (CRM) have become increasingly important in enterprise management. are becoming increasingly important. It can help companies better manage customer information, track sales opportunities, improve customer satisfaction, etc. This article will introduce how to use PHP to develop a simple customer relationship management system to help companies better manage customer relationships.

1. Set up a development environment

First, we need to set up a PHP development environment. It is recommended to use integrated development environments such as XAMPP or WAMP, which can provide an integrated Apache server, MySQL database and PHP environment.
After the installation is complete, start the Apache and MySQL services, and enter localhost in the browser to confirm whether the installation is successful.

2. Create a database

Next, we need to create a database to store customer information. Create a database named "crm" using phpMyAdmin or the MySQL command line.
The following is the SQL code to create the "customers" table:

CREATE TABLE customers (
id INT(11) NOT NULL AUTO_INCREMENT,
name VARCHAR(100) NOT NULL,
email VARCHAR(100) NOT NULL,
phone VARCHAR(15) NOT NULL,
address VARCHAR(255) NOT NULL,
PRIMARY KEY (id)
);

三, Create PHP script

  1. Create an "index.php" file to display the customer list and action buttons. The code is as follows:
<?php
// 数据库连接信息
$hostname = "localhost";
$username = "root";
$password = "";
$dbname = "crm";

// 连接数据库
$conn = mysqli_connect($hostname, $username, $password, $dbname);

// 检查连接是否成功
if (!$conn) {
  die("连接数据库失败: " . mysqli_connect_error());
}

// 查询所有客户信息
$sql = "SELECT * FROM customers";
$result = mysqli_query($conn, $sql);

// 显示客户信息
echo "<h1>客户列表</h1>";
echo "<table>";
echo "<tr><th>姓名</th><th>邮箱</th><th>电话</th><th>地址</th><th>操作</th></tr>";
while ($row = mysqli_fetch_assoc($result)) {
  echo "<tr>";
  echo "<td>".$row['name']."</td>";
  echo "<td>".$row['email']."</td>";
  echo "<td>".$row['phone']."</td>";
  echo "<td>".$row['address']."</td>";
  echo "<td><a href='edit.php?id=".$row['id']."'>编辑</a> <a href='delete.php?id=".$row['id']."'>删除</a></td>";
  echo "</tr>";
}
echo "</table>";

// 关闭数据库连接
mysqli_close($conn);
?>
  1. Create an "add.php" file to add customer information. The code is as follows:
<?php
// 数据库连接信息
$hostname = "localhost";
$username = "root";
$password = "";
$dbname = "crm";

// 连接数据库
$conn = mysqli_connect($hostname, $username, $password, $dbname);

// 检查连接是否成功
if (!$conn) {
  die("连接数据库失败: " . mysqli_connect_error());
}

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  // 获取表单提交的数据
  $name = $_POST['name'];
  $email = $_POST['email'];
  $phone = $_POST['phone'];
  $address = $_POST['address'];

  // 插入客户信息
  $sql = "INSERT INTO customers (name, email, phone, address) VALUES ('$name', '$email', '$phone', '$address')";
  
  if (mysqli_query($conn, $sql)) {
    echo "添加客户成功";
  } else {
    echo "添加客户失败: " . mysqli_error($conn);
  }
}

// 关闭数据库连接
mysqli_close($conn);
?>

<h1>添加客户</h1>
<form action="add.php" method="post">
  <label for="name">姓名:</label>
  <input type="text" name="name" required><br>

  <label for="email">邮箱:</label>
  <input type="email" name="email" required><br>

  <label for="phone">电话:</label>
  <input type="text" name="phone" required><br>

  <label for="address">地址:</label>
  <textarea name="address" required></textarea><br>

  <input type="submit" value="添加客户">
</form>
  1. Create an "edit.php" file for editing customer information. The code is as follows:
<?php
// 数据库连接信息
$hostname = "localhost";
$username = "root";
$password = "";
$dbname = "crm";

// 连接数据库
$conn = mysqli_connect($hostname, $username, $password, $dbname);

// 检查连接是否成功
if (!$conn) {
  die("连接数据库失败: " . mysqli_connect_error());
}

if ($_SERVER["REQUEST_METHOD"] == "POST") {
  // 获取表单提交的数据
  $id = $_POST['id'];
  $name = $_POST['name'];
  $email = $_POST['email'];
  $phone = $_POST['phone'];
  $address = $_POST['address'];

  // 更新客户信息
  $sql = "UPDATE customers SET name='$name', email='$email', phone='$phone', address='$address' WHERE id=$id";
  
  if (mysqli_query($conn, $sql)) {
    echo "编辑客户成功";
  } else {
    echo "编辑客户失败: " . mysqli_error($conn);
  }
} else {
  // 获取要编辑的客户信息
  $id = $_GET['id'];
  $sql = "SELECT * FROM customers WHERE id=$id";
  $result = mysqli_query($conn, $sql);
  $customer = mysqli_fetch_assoc($result);
}

// 关闭数据库连接
mysqli_close($conn);
?>

<h1>编辑客户</h1>
<form action="edit.php" method="post">
  <input type="hidden" name="id" value="<?php echo $customer['id']; ?>">

  <label for="name">姓名:</label>
  <input type="text" name="name" value="<?php echo $customer['name']; ?>" required><br>

  <label for="email">邮箱:</label>
  <input type="email" name="email" value="<?php echo $customer['email']; ?>" required><br>

  <label for="phone">电话:</label>
  <input type="text" name="phone" value="<?php echo $customer['phone']; ?>" required><br>

  <label for="address">地址:</label>
  <textarea name="address" required><?php echo $customer['address']; ?></textarea><br>

  <input type="submit" value="保存">
</form>

4. Run the program

Place the file created above in the website root directory of the server, and then access localhost/index.php in the browser, that is You can see the customer list page. Click the "Add Customer" button to jump to the add customer page. After filling in the information, click "Add User" to add a customer.
Click the "Edit" button in the customer list to jump to the editing page. After modifying the customer information, click "Save" to save the changes.

Through the above steps, you have successfully developed a simple customer relationship management system using PHP. You can expand and optimize the functions according to your own needs, such as adding search, sorting and other functions to better manage customer relationships.

The above is the detailed content of How to develop a simple customer relationship management system using PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn