Home  >  Article  >  Backend Development  >  How to add php array to database

How to add php array to database

WBOY
WBOYOriginal
2023-05-23 12:45:38695browse

In the PHP programming language, adding an array to the database is a very common operation. In a real application, you may need to extract data from an array and insert it into a database for subsequent querying or analysis. In this post, we will discuss how to add an array to a database using PHP. First, we'll explore how to pass arrays to PHP scripts, and how to connect to the database. Next, we will discuss how to insert an array into a MySQL database using PHP code.

1. Pass the array to the PHP script

In PHP programming, you can pass the array to the PHP script by using an HTML form. In the form, the name of the parameter must be the same as the name used in the subsequent PHP code. For example, if an input box named "data" is used in the form, use $data in the PHP code to access the data. The following is a basic HTML form that will use the POST method to pass data to a PHP script:

<form action="add_to_database.php" method="post">
  数据: <input type="text" name="data"><br>
  <input type="submit" value="提交">
</form>

2. Connect to the database

In PHP, use the mysqli_connect() function to connect to MySQL database. The following is an example of using variables for username, password, and database name:

$servername = "localhost";
$username = "root";
$password = "";
$dbname = "myDatabase";

// 创建连接
$conn = mysqli_connect($servername, $username, $password, $dbname);

// 检测连接
if (!$conn) {
    die("连接失败: " . mysqli_connect_error());
}
echo "连接成功";

3. Insert the array into the MySQL database

Use the following code to insert the array into the MySQL database:

// 将数据从数组中提取到变量中
$data = $_POST['data'];

// 将数据插入到数据库中
$sql = "INSERT INTO myTable (data) VALUES ('".$data."')";
if (mysqli_query($conn, $sql)) {
  echo "数据插入成功";
} else {
  echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}

In this example, we first extract the data we want to save from the POST request. Then we insert the data into our myTable table. Note that we use string concatenation to insert variables into the SQL query.

Summary

In PHP programming, adding an array to the database is a basic operation. We can use HTML forms to pass data to a PHP script and then insert this data in a MySQL database. We can easily add data to a MySQL data table by connecting to the database and using the mysqli_query() function. This helps us collect and store data in web applications for deeper analysis and processing.

The above is the detailed content of How to add php array to database. 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