Simple file upload to MySql database developed in PHP (1)
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 // 創(chuàng)建連接 $conn = new mysqli("localhost", "uesename", "password"); // 檢測連接 if ($conn->connect_error) { die("連接失敗: " . $conn->connect_error);} // 創(chuàng)建數(shù)據(jù)庫 $sql = "CREATE DATABASE test"; if ($conn->query($sql) === TRUE) { echo "數(shù)據(jù)庫創(chuàng)建成功"; } 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 // 創(chuàng)建連接 $conn = new mysqli("localhost", "uesename", "password","test"); // 檢測連接 if ($conn->connect_error) { die("連接失敗: " . $conn->connect_error); } // 使用 sql 創(chuàng)建數(shù)據(jù)表 $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 "創(chuàng)建數(shù)據(jù)表錯誤: " . $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.