php表单处理数据代码 php提交表单数据有哪几种方法
php表单写入mysql数据库的代码
!--表单文件,拷入index.php--
创新互联网站建设公司一直秉承“诚信做人,踏实做事”的原则,不欺瞒客户,是我们最起码的底线! 以服务为基础,以质量求生存,以技术求发展,成交一个客户多一个朋友!专注中小微企业官网定制,网站建设、成都网站建设,塑造企业网络形象打造互联网企业效应。
!DOCTYPE html
html
head
style
label{display:inline-block;width:100px;margin-bottom:10px;}
/style
titleAdd 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"
--
labelFirst Name/label
input type="text" name="first_name" /
br /
labelLast Name/label
input type="text" name="last_name" /
br /
labeldepartment/label
input type="text" name="department" /
br /
labelEmail/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=$servername;dbname=$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
--
--
-- 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 */;
高分求php高手写一处理下面表单的代码
表单问题,单选的多个选项应该用同一个name
你原来的
tr
td width="43%"height="25"取车方式:/td
td width="57%"height="25"input name="mendianziqu"type="radio"value="radiobutton"chec
ked/
门店自取
label
input type="radio"name="shangmensongche"value="radiobutton"/
上门送车/label/td
/tr
改成
tr
td width="43%"height="25"取车方式:/td
td width="57%"height="25"input name="quchefangshi"type="radio"value="mendianziqu"chec
ked/
门店自取
label
input type="radio"name="quchefangshi"value="shangmensongche"/
上门送车/label/td
/tr
/**************完整PHP代码如下 car.php***********************/
?php
if ($_POST) {
$link = mysql_connect ( "localhost", "root", "这里是数据库密码" ); //自行修改密码
$sql_creat_db = 'create database IF NOT EXISTS `car`';
mysql_query ( $sql_creat_db );
mysql_query ( "SET NAMES 'GBK'");
mysql_select_db ( "car", $link ) or die ( '连接数据库失败' );
$sql_creat_table = '
create table IF NOT EXISTS `getcar`(
`id` int(11) NOT NULL auto_increment,
`quchedidian` varchar(150) NOT NULL,
`quchefangshi` varchar(50) NOT NULL,
`birthday1` varchar(50) NOT NULL,
`qucheshijian` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM
';
mysql_query ( $sql_creat_table ) or die ( '连接数据库失败' );
if (preg_match ( "/^[\d]+$/", $_POST ['birthday1'] )) {
$birthday = intval ( $_POST ['birthday1'] );
if ($birthday 31 || $birthday 1) {
echo '取车时间输入错误,只能输入数字';
} elseif ($_POST ['quchedidian'] == '0') {
echo '请选择取车地点';
} elseif (! isset ( $_POST ['quchefangshi'] )) {
echo '请选择取车方式';
} else {
$sql_insert_table = "
insert into getcar values(
NULL,
'{$_POST['quchedidian']}',
'{$_POST['quchefangshi']}',
'{$birthday}',
'{$_POST['qucheshijian']}')
";
if (mysql_query ( $sql_insert_table )) {
echo '提交成功br /', '您提交的数据是:br /', '取车地点:', $_POST ['quchedidian'], 'br /', '取车方式:', $_POST ['quchefangshi'], 'br /', '取车时间:', $birthday . '日' . $_POST ['qucheshijian'], '点br /';
} else echo $sql_insert_table ;
;
}
} else {
echo '取车时间输入错误,只能输入数字';
}
}
?
form action="" method="post" name="yijiao"input type="hidden"
name="hidden" value="yes"
table width="400" height="246" border="0" cellpadding="0"
cellspacing="0" id="form"
tr
td
table width="100%" height="75" border="0" cellpadding="0"
cellspacing="0"
tr
td height="25"取车地点:/td
td height="25"select name="quchedidian"
option value="0"请您选择在何处发车/option
option value="东风路总店"东风路总店/option
option value="体育路一分店"体育路一分店/option
/select/td
/tr
tr
td width="43%" height="25"取车方式:/td
td width="57%" height="25"input name="quchefangshi" type="radio"
value="门店自取" chec ked / 门店自取 label input type="radio"
name="quchefangshi" value="上门送车" / 上门送车/label/td
/tr
tr
td height="25"label取车时间:/label/td
td height="25"span style="PADDING-LEFT: 10px" label input
name="birthday1" type="text" id="birthday1" size="8" maxlength="10" /
/label 日 select name="qucheshijian"
option value="1"01/option
option value="2"02/option
/select 点/span/td
/tr
/table
/td
/tr
tr
td valign="top"/td
/tr
tr
td height="30"labelinput type="submit" name="Submit" value="提交" //label
/td
/tr
/table
/form
用php处理表单数据
LZ应该是这样$_POST['hobby'][0] , 至于里面的''写与不写都可以,最好要写上。数字的话不写也可以。
PHP表单简单代码处理
?php
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']."
";
}
?
请把代码里面的中文标点换成英文的就可以了
分享标题:php表单处理数据代码 php提交表单数据有哪几种方法
URL地址:http://ybzwz.com/article/docpcos.html