The previous section explained how to save files to a local directory through PHP code
In this section we explain how to upload files to the database.
First we need to create a test database:
<?php // 创建连接 $conn = new mysqli("localhost", "uesename", "password"); // 检测连接 if ($conn->connect_error) { die("连接失败: " . $conn->connect_error);} // 创建数据库 $sql = "CREATE DATABASE test"; if ($conn->query($sql) === TRUE) { echo "数据库创建成功"; } else { echo "Error creating database: " . $conn->error; } $conn->close(); ?>
Then we need to create a database img table, containing three content ids, title titles, and pic image addresses.
Set the following fields:
id: It is unique, of type int, and select the primary key.
title: Title, type is varchar, length is 100.
pic: Picture storage address, type is varchar, length is 100.
<?php // 创建连接 $conn = new mysqli("localhost", "uesename", "password","test"); // 检测连接 if ($conn->connect_error) { die("连接失败: " . $conn->connect_error); } // 使用 sql 创建数据表 $sql = "CREATE TABLE img ( id INT(10) UNSIGNED AUTO_INCREMENT PRIMARY KEY, title VARCHAR(100) NOT NULL, pic VARCHAR(200) NOT NULL,)ENGINE=InnoDB DEFAULT CHARSET=utf8 "; if ($conn->query($sql) === TRUE) { echo "Table MyGuests created successfully"; } else { echo "创建数据表错误: " . $conn->error; } $conn->close(); ?>
Of course, you can also directly create databases and tables through phpMyAdmin.
For details, please refer to our PHP development user login module tutorial on creating databases and tables.
Next Section