Question 1: PHP gets the connection fields and writes them to the database?
To get the connection fields in PHP and write to the database, you need to perform the following steps:
$servername = "your_server_name"; $username = "your_username"; $password = "your_password"; $database = "your_database"; $conn = new mysqli($servername, $username, $password, $database); // 检查连接是否成功 if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); }
// 假设表单中有名为 'input_field' 的字段 $inputField = $_POST['input_field'];
$sql = "INSERT INTO your_table_name (column_name) VALUES ('$inputField')";
if ($conn->query($sql) === TRUE) { echo "数据插入成功"; } else { echo "Error: " . $sql . "<br>" . $conn->error; }
$conn->close();
Question 2: How to write a PHP program in PHP MySQL to store data in the database?
To write a PHP program to store data in a MySQL database, you can refer to the following steps:Establish a database connection: Use PHP's mysqli or PDO to establish a connection with the MySQL database, follow the same steps as above.
Get input data: Use **$_POST or $_GET** to get the input data passed by the form or other means.
$inputField1 = $_POST['input_field1']; $inputField2 = $_POST['input_field2']; // 可以根据实际情况获取更多字段
$sql = "INSERT INTO your_table_name (column_name1, column_name2) VALUES ('$inputField1', '$inputField2')"; // 可以根据实际情况插入更多字段
if ($conn->query($sql) === TRUE) { echo "数据插入成功"; } else { echo "Error: " . $sql . "<br>" . $conn->error; }
$conn->close();
Summary:
To obtain the connection field in PHP and write it to the database, you need to establish a database connection, obtain input data, write SQL statements, and execute SQL statement and closing the database connection and other key steps. These steps must be taken care of to ensure the program runs properly.
The above is the detailed content of Get fields from connection and write them to database using PHP. For more information, please follow other related articles on the PHP Chinese website!