Home > Article > Backend Development > Second-hand recycling website uses multiple payment method options developed in PHP
Second-hand recycling website uses multiple payment method options developed by PHP
With the development of social economy and the improvement of consumption levels, the second-hand recycling market is gradually emerging. In order to facilitate users to conduct transactions, second-hand recycling websites need to provide a variety of payment method options. This article will introduce how to use PHP to develop a second-hand recycling website and realize the selection of multiple payment methods.
First, we need to create a database to store the user's transaction information. Suppose we have created a database named "transactions" with the following fields: id
, user_id
, item_id
, payment_method
, amount
, status
. Among them, user_id
is the user ID, item_id
is the product ID, payment_method
is the payment method, amount
is the transaction amount, status
is the transaction status.
Next, we use PHP code to connect to the database and implement the payment method selection. An example is as follows:
<?php // 连接数据库 $servername = "localhost"; $username = "root"; $password = "123456"; $dbname = "transactions"; $conn = new mysqli($servername, $username, $password, $dbname); if ($conn->connect_error) { die("连接数据库失败: " . $conn->connect_error); } // 获取用户选择的支付方式 $paymentMethod = $_POST['payment_method']; // 插入交易信息到数据库 $sql = "INSERT INTO transactions (user_id, item_id, payment_method, amount, status) VALUES ('1', '1', '$paymentMethod', '100', '待支付')"; if ($conn->query($sql) === true) { echo "交易成功"; } else { echo "交易失败:" . $conn->error; } $conn->close(); ?>
On a web page, we can use a form to display multiple payment methods for users to choose from. An example is as follows:
<form action="payment.php" method="post"> <input type="radio" name="payment_method" value="支付宝"> 支付宝 <input type="radio" name="payment_method" value="微信支付"> 微信支付 <input type="radio" name="payment_method" value="银行转账"> 银行转账 <input type="submit" value="确认付款"> </form>
The above code implements the function of users selecting payment methods on the web page and submitting transaction information to the database.
Next, we can display the user's transaction records by querying the database. An example is as follows:
<?php // 连接数据库 $servername = "localhost"; $username = "root"; $password = "123456"; $dbname = "transactions"; $conn = new mysqli($servername, $username, $password, $dbname); if ($conn->connect_error) { die("连接数据库失败: " . $conn->connect_error); } // 查询用户的交易记录 $sql = "SELECT * FROM transactions"; $result = $conn->query($sql); if ($result->num_rows > 0) { // 输出数据 while($row = $result->fetch_assoc()) { echo "交易ID:" . $row["id"]. " - 用户ID:" . $row["user_id"]. " - 支付方式:" . $row["payment_method"]. " - 交易金额:" . $row["amount"]. "<br>"; } } else { echo "没有交易记录。"; } $conn->close(); ?>
The above code implements the function of querying the user's transaction records from the database and displaying them on the web page.
Through the above sample code, we can see that it is not difficult to use PHP to develop multiple payment method options in a second-hand recycling website. Developers only need to connect to the database, obtain the payment method selected by the user, and insert transaction information into the database. At the same time, we can also display users' transaction records by querying the database to facilitate users to query and manage their own transaction information. In this way, users can easily choose their preferred payment method and understand their transaction records when using second-hand recycling websites, which improves the convenience and reliability of second-hand recycling transactions.
The above is the detailed content of Second-hand recycling website uses multiple payment method options developed in PHP. For more information, please follow other related articles on the PHP Chinese website!