php表单写入mysql数据库的代码

php表单写入mysql数据库的代码,第1张

<!--表单文件,拷入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 */

//自己写限制

<div class="daohang">

<div class="daohang_title">添加大三平面作品</div>

<li class="ti">

<div class="bd"><a href="../main_pingmian_1" target="mainFrame">大三作品</a></div>

<div class="bd"><a href="../main_pingmian_2" target="mainFrame">大二作品</a></div>

<div class="bd"><a href="../main_pingmian_3" target="mainFrame">大一作品</a></div>

</li>

</div><div class="daohang">

<form nname="form1" method="post" action="pm_cheak.php" onSubmit="return jiancha()" enctype="multipart/form-data"> //注意 enctype="multipart/form-data一定要有

<div class="daohang_title">请认真填写平面作品的资料</div> <div class="he" >

<li class="he_1">创作人</li>

<li class="he_2"><input name="pm_zuozhe" type="text" id="pm_zuozhe" value="" size="8" maxlength="4" /></li>

</div>

<div class="he" >

<li class="he_1"><span class="lanmu_title">作品说明</span></li>

<li class="he_2"><input name="pm_text" type="text" id="pm_text" value="" size="30" maxlength="30" /> 最多可以输入15个汉字或30个字母!</li>

</div>

<div class="he" >

<li class="he_1">连接地址</li>

<li class="he_2"><input name="pm_href" type="text" id="pm_href" value="" size="30" maxlength="30" />

如:www.hnanv0.com</li>

</div>

<div class="he" >

<li class="he_1">作品图片</li>

<li class="he_2">

<label>

<input type="file" name="pm_images" style="width:220border:1 solid #6899B7font-size:9ptsize="14" > </label> 允许上传文件类型为:jpg|jpeg|png|pjpeg|gif|bmp

</li>

</div>

<div class="he" >

<li class="he_1"></li>

<li class="he_2">

<label>

<input type="submit" name="upload" value="提交" />

</label>

<label>

<input type="reset" name="button2" id="button2" value="重置" >

</label>

<input name="date" type="hidden" id="date" value="">

</li>

</div>

</form></div>程序部分pm_cheak.php: <?

include ("../../../../conn.php")

if ($_POST['upload'] == '提交') { $pm_zuozhe = $_POST[pm_zuozhe]

$pm_text = $_POST[pm_text]

$pm_href = $_POST[pm_href]//一一对应 $pm_date = date("Y-m-d")$link=date("YmjHis") //获取当前时间

//为表单中提交的数据重新命名,以当前时间和随机数作为名称,其中使用$_FILES获取表单中真实的名称,使用strstr函数获取文件的后缀

$path=$link.mt_rand(1000,9999).strstr($_FILES["pm_images"]["name"],".")

$address="../../../../images/pm_btn/".$path //定义文件上传的路径

move_uploaded_file($_FILES["pm_images"]["tmp_name"],$address) //将文件上传到指定的文件中

$pm_images="images/pm_btn/".$path //获取上传文件在服务器中的存储路径

//将表单中提交的数据存储到数据库中 $sql = mysql_query("insert into pm(pm_zuozhe,pm_text,pm_href,pm_images,pm_date)values('$pm_zuozhe','$pm_text','$pm_href','$pm_images','$pm_date')")//执行插入语句

mysql_close($conn)//关闭连接

}

echo ("<script>alert('大三平面作品添加成功')window.location.href='add_pm_zuopin.php'</script>")

?>

<meta http-equiv="Content-Type" content="text/htmlcharset=GBK" />//注意编码格式

explode

implode

数组

in_array -- 检查数组中是否存在某个值

array -- 新建一个数组

count -- 计算数组中的单元数目或对象中的属性个数

range -- 建立一个包含指定范围单元的数组

array_key_exists -- 检查给定的键名或索引是否存在于数组中

array_keys -- 返回数组中所有的键名

array_map -- 将回调函数作用到给定数组的单元上

array_merge_recursive -- 递归地合并一个或多个数组

array_merge -- 合并一个或多个数组

array_push

mysql

mysql_connect

mysql_query

mysql_select_db -- 选择 MySQL 数据库

mysql_close -- 关闭 MySQL 连接

mysql_fetch_array -- 从结果集中取得一行作为关联数组,或数字数组,或二者兼有

mysql_fetch_assoc -- 从结果集中取得一行作为关联数组

mysql_fetch_field -- 从结果集中取得列信息并作为对象返回

mysql_fetch_lengths -- 取得结果集中每个输出的长度

mysql_fetch_object -- 从结果集中取得一行作为对象

mysql_fetch_row -- 从结果集中取得一行作为枚举数组

REG

preg_match_all -- 进行全局正则表达式匹配

preg_match -- 进行正则表达式匹配

preg_quote -- 转义正则表达式字符

preg_replace_callback -- 用回调函数执行正则表达式的搜索和替换

preg_replace -- 执行正则表达式的搜索和替换

preg_split -- 用正则表达式分割字符串

ereg_replace -- 正则表达式替换

ereg -- 正则表达式匹配

eregi_replace -- 不区分大小写的正则表达式替换

eregi -- 不区分大小写的正则表达式匹配

split -- 用正则表达式将字符串分割到数组中

spliti -- 用正则表达式不区分大小写将字符串分割到数组中

sql_regcase -- 产生用于不区分大小的匹配的正则表达式

字符串

str_getcsv -- Parse a CSV string into an array

str_ireplace -- Case-insensitive version of str_replace().

str_pad -- Pad a string to a certain length with another string

str_repeat -- Repeat a string

str_replace -- Replace all occurrences of the search string with the replacement string

str_rot13 -- Perform the rot13 transform on a string

str_shuffle -- Randomly shuffles a string

str_split -- Convert a string to an array

str_word_count -- Return information about words used in a string

strcasecmp -- Binary safe case-insensitive string comparison

strchr -- 别名 strstr()

strcmp -- Binary safe string comparison

strcoll -- Locale based string comparison

strcspn -- Find length of initial segment not matching mask

strip_tags -- Strip HTML and PHP tags from a string

stripcslashes -- Un-quote string quoted with addcslashes()

stripos -- Find position of first occurrence of a case-insensitive string

stripslashes -- Un-quote string quoted with addslashes()

stristr -- Case-insensitive strstr()

strlen -- Get string length

strnatcasecmp -- Case insensitive string comparisons using a "natural order" algorithm

strnatcmp -- String comparisons using a "natural order" algorithm

strncasecmp -- Binary safe case-insensitive string comparison of the first n characters

strncmp -- Binary safe string comparison of the first n characters

strpbrk -- Search a string for any of a set of characters

strpos -- Find position of first occurrence of a string

strrchr -- Find the last occurrence of a character in a string

strrev -- Reverse a string

strripos -- Find position of last occurrence of a case-insensitive string in a string

strrpos -- Find position of last occurrence of a char in a string

strspn -- Find length of initial segment matching mask

strstr -- Find first occurrence of a string

strtok -- Tokenize string

strtolower -- Make a string lowercase

strtoupper -- Make a string uppercase

strtr -- Translate certain characters

substr_compare -- Binary safe optionally case insensitive comparison of 2 strings from an offset, up to length characters

substr_count -- Count the number of substring occurrences

substr_replace -- Replace text within a portion of a string

substr -- Return part of a string


欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/7683503.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-08
下一篇 2023-04-08

发表评论

登录后才能评论

评论列表(0条)

保存