if(isset($_POST["btn_submit"])){
if(empty($_POST['username'])){
echo "你没有输入用户名 "
exit(0)
}
if (empty($_POST['password'])){
echo "你没有输入密码:"
exit(0)
}
echo"你的 ".$_POST['user_name'凳胡]."
"渗粗键
echo"你的".$_POST['password']."
"
echo"你的 ".$_POST['age']."
"
if (!empty($_POST['languages'])){
echo "你选择的为:"
foreach ($_POST['languages'] as $lang){
echo $lang. " "
}
echo "你的自我介绍: ".n12br($_POST['intro'])."
"丛巧
echo "网页隐藏值": ".$_POST['from']."
"
}
?>
请把代码里面的中文标点换成英文的就可以了
<!--表单文件,拷入index.php--><!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 */
网页:<body>
<form action="Connection.php"method="post">
<table bgcolor="#eeeeee" >
<tr>
<td colspan="2" align="center"><b>情况登记</b>
<tr>
<td>姓名:<td><input size="20" type="text" name="name">
<tr>
<td>密码:<td><input size="20" type="password" name="password>
<tr>
<td>性别:<td><input type="radio"拿衫 name="stuSex" value="0">男<input type="radio" name="stuSex" value="1">女
<消腔腔tr>
<td><td><input type="submit" value="提交"></a><input type="reset"value="重写">
</table>
</form>
</body>
数据库表:
CREATE DATABASE `student`
CREATE TABLE `student` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`name` varchar(50) NOT NULL,
`password` varchar(50) NOT NULL,
`stusex` int(1) NOT NULL,
PRIMARY KEY (`id`)
)
PHP Connection.php代码:
<?php
$db = mysql_connect('localhost', 'root', '123456')//root是数据库账号 123456 是数据库密码
if (!$db) {
die('无法连接: ' . mysql_error())
}
mysql_select_db('student', $db) or die ('无法使用数据库: ' . mysql_error())
$name = trim(strip_tags($_POST['name']))
$password = md5(trim($_POST['password'圆陆]))
$stusex = intval($_POST['stusex'])
$sql = "insert into student (`name`,`password`,`stusex`) values ('$name','$password','$stusex')"
mysql_query($sql) or die("无法保存学生数据: " . mysql_error())
die("保存成功")
?>
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)