<!DOCTYPE html>
<html>
<head>
<style>
label{display:inline-blockwidth:100pxmargin-bottom:10px}
</style>
<title>Add students</title>
</head>
<body>
<!-- 数据库用mysqli 面向过程调用方法-->
<form method="post" action="write2db.php">
<!--数据库用mysqli 面向过程调用方法
<form method="post" action="write2db_sqlio.php">
-->
<!--数据库用PDO调用方法
<form method="post" action="write2db_pdo.php">
-->
<label>First Name</label>
<input type="text" name="first_name" />
<br />
<label>Last Name</label>
<input type="text" name="last_name" />
<br />
<label>department</label>
<input type="text" name="department" />
<br />
<label>Email</label>
<input type="text" name="email" />
<br />
<input type="submit" value="Add students">
</form>
</body>
</html>
------------------------------
<?php//拷贝命名为write2db.php,数据库用mysqli 面向过程调用方法
//print_r($_POST)
// create a variable
$first_name=$_POST['first_name']
$last_name=$_POST['last_name']
$department=$_POST['department']
$email=$_POST['email']
//调试用
echo "Your input: "
echo $first_name
echo '<br />'
echo $last_name
echo '<br />'
echo $department
echo '<br />'
echo $email
echo '<br />'
$servername = "localhost"
//Your database username and password
//$username = "username"
//$password = "password"
$username = "tester"
$password = "testerPassword"
//your database name
$dbname = "test"
$tablename ="student"
// Create connection
$connect = mysqli_connect($servername, $username, $password, $dbname)
if (!$connect) {
die("Connection failed: " . mysqli_connect_error())
}
//Execute the query
$sql="INSERT INTO $tablename (first_name,last_name,department,email)
VALUES('$first_name','$last_name','$department','$email')"
if (mysqli_query($connect, $sql)) {
echo "Hooray! New record is inserted to database successfully. Please check database."
} else {
echo "Error: " . $sql . "<br />" . mysqli_error($connect)
}
mysqli_close($connect)
?> <?php
//拷贝命名为write2db_sqlio.php,数据库用mysqli 面向对象调用方法
//print_r($_POST)
// create a variable
$first_name=$_POST['first_name']
$last_name=$_POST['last_name']
$department=$_POST['department']
$email=$_POST['email']
//调试用
echo "Your input: "
echo $first_name
echo '<br />'
echo $last_name
echo '<br />'
echo $department
echo '<br />'
echo $email
echo '<br />'
$servername = "localhost"
//Your database username and password
//$username = "username"
//$password = "password"
$username = "tester"
$password = "testerPassword"
//database name
$dbname = "test"
$tablename ="student"
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname)
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error)
}
$sql="INSERT INTO $tablename (first_name,last_name,department,email)
VALUES('$first_name','$last_name','$department','$email')"
if ($conn->query($sql) === TRUE) {
echo "New record created successfully"
} else {
echo "Error: " . $sql . "<br>" . $conn->error
}
$conn->close()
?> <?php
//拷贝为文件write2db_pdo.php,数据库用PDO调用方法
//print_r($_POST)
a variable
$first_name=$_POST['first_name']
$last_name=$_POST['last_name']
$department=$_POST['department']
$email=$_POST['email']
//调试用
echo "Your input: "
echo $first_name
echo '<br />'
echo $last_name
echo '<br />'
echo $department
echo '<br />'
echo $email
echo '<br />'
$servername = "localhost"
//Your database username and password
//$username = "username"
//$password = "password"
$username = "tester"
$password = "testerPassword"
//your database name
$dbname = "test"
$tablename ="student"
// Create connection
try {
$conn = new PDO("mysql:host=$servernamedbname=$dbname", $username, $password)
// set the PDO error mode to exception
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION)
$sql="INSERT INTO $tablename (first_name,last_name,department,email)
VALUES('$first_name','$last_name','$department','$email')"
// use exec()
$conn->exec($sql)
echo "New record created successfully"
}
catch(PDOException $e)
{
echo $sql . "<br>" . $e->getMessage()
}
$conn = null
?> --创建数据库test, 将此文件存为test.sql 导入数据库,或者手动创建表结构
-- phpMyAdmin SQL Dump
-- version 4.7.4
-- https://www.phpmyadmin.net/
--
-- Host: 127.0.0.1:3306
-- Generation Time: Mar 12, 2018 at 04:04 AM
-- Server version: 5.7.19
-- PHP Version: 7.1.9
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"
SET AUTOCOMMIT = 0
START TRANSACTION
SET time_zone = "+00:00"
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */
/*!40101 SET NAMES utf8mb4 */
--
-- Database: `test`
--
-- --------------------------------------------------------
--
-- Table structure for table `student`
--
DROP TABLE IF EXISTS `student`
CREATE TABLE IF NOT EXISTS `student` (
`id` tinyint(3) UNSIGNED NOT NULL AUTO_INCREMENT,
`first_name` varchar(20) NOT NULL,
`last_name` varchar(20) NOT NULL,
`department` varchar(50) NOT NULL,
`email` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8
--
-- Dumping data for table `student`
--
INSERT INTO `student` (`id`, `first_name`, `last_name`, `department`, `email`) VALUES
(1, 'first1', 'last1', 'cs', '1985@qq.com')
COMMIT
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */
在 MySQL 中,可以使用 CREATE TABLE 语句创建表。其语法格式为:其中,[表定义选项]的格式为:CREATE TABLE 语句的主要语法及使用说明如下:CREATE TABLE:用于创建给定名称的表,必须拥有表CREATE的权限。<表名>:指定要创建表的名称,在 CREATE TABLE 之后给出,必须符合标识符命名规则。表名称被指定为 db_name.tbl_name,以便在特定的数据库中创建表。无论是否有当前数据库,都可以通过这种方式创建。在当前数据库中创建表时,可以省略 db-name。如果使用加引号的识别名,则应对数据库和表名称分别加引号。例如,'mydb'.'mytbl' 是合法的,但 'mydb.mytbl' 不合法。<表定义选项>:表创建定义,由列名(col_name)、列的定义(column_definition)以及可能的空值说明、完整性约束或表索引组成。size:规定表中列的最大长度。默认的情况是,表被创建到当前的数据库中。若表已存在、没有当前数据库或者数据库不存在,则会出现错误。提示:使用 CREATE TABLE 创建表时,必须指定以下信息:要创建的表的名称不区分大小写,不能使用SQL语言中的关键字,如DROP、ALTER、INSERT等。数据表中每个列(字段)的名称和数据类型,如果创建多个列,要用逗号隔开。示例:选择创建表的数据库 test_db创建 tb_emp1 数据表使用 SHOW TABLES 语句查看数据表是否创建成功欢迎分享,转载请注明来源:内存溢出
评论列表(0条)