简介
PHP(PHP: Hypertext Preprocessor)是一种开源的服务器端脚本语言,主要用于网页制作。它可以嵌入到 HTML 中使用,设计目标是允许web开发者快速编写出易于理解和维护的代码,它在服务器端运行。
PHP 文件可通过 HTTP 协议从web服务器提供,并在服务器端执行。PHP 代码在服务器上执行,结果会返回 HTML 格式的数据发送给浏览器。
PHP 文件可以在任何实现了 PHP 解释器的服务器上运行,如 Apache 服务器、Nginx 服务器等。
PHP 的主要特点包括:
-
简单易学易用:PHP 的语法类似于C,Perl和 Java,但是更为简单且使用广泛。
-
开源:PHP 是开源的,这意味着源代码是公开的,任何人都可以查看和修改。
-
兼容 ASP:PHP 代码可以直接嵌入到 HTML 文档中,使得学习成本降低。
-
速度:PHP 代码执行速度快,因为它在服务器端执行。
-
免费:PHP 是免费的,不需要支付额外的费用。
-
扩展和库:PHP 有大量的扩展库,可以用于完成各种任务,如数据库操作、图像处理等。
一、php记忆表单简单编写
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style type="text/css">
tr{
border: 2;
background-color: yellow;
}
.a1{
text-align: right; }
.a2{
text-align: lfet; }
.a3{
width: 80px;
height: auto;
}
</style>
</head>
<body>
<?php
$_POST['aihao']=(isset($_POST['aihao']))?($_POST['aihao']):array();
?> <form action="#" method="post" enctype="multipart/form-data">
<table >
<tr >
<td class="a1">姓名:</td>
<td class="a2"><input type="text" name="username" placeholder="请输入姓名" value="<?php echo $_POST['username'];?>"/></td>
</tr>
<tr>
<td class="a1">性别:</td>
<td class="a2"><input type="radio" name="sex" value="男" <?php echo ($_POST['sex']=="男")?"checked":"";?>/>男
<input type="radio" name="sex" value="女" <?php echo ($_POST['sex']=="女")?"checked":"";?>/>女</td>
</tr>
<tr>
<td class="a1">密码:</td>
<td class="a2"><input type="password" name="password" value="<?php echo $_POST['password'];?>" placeholder="请输入密码"/></td>
</tr>
<tr>
<td class="a1"> 学历:</td>
<td class="a2"><select name="xueli">
<option value="研究生" <?php echo ($_POST['xueli']=="研究生")?"selected":"";?>>研究生</option>
<option value="大学生" <?php echo ($_POST['xueli']=="大学生")?"selected":"";?>>大学生</option>
<option value="高中生" <?php echo ($_POST['xueli']=="高中生")?"selected":"";?>>高中生</option>
</select>
</td>
</tr>
<tr>
<td class="a1">爱好:</td>
<td class="a2"><input type="checkbox" name="aihao[]" value="唱歌" <?php echo (in_array("唱歌",$_POST["aihao"]))?"checked":"";?>/>唱歌
<input type="checkbox" name="aihao[]" value="跳舞" <?php echo (in_array("跳舞",$_POST['aihao']))?"checked":"";?>/>跳舞
<input type="checkbox" name="aihao[]" value="打篮球" <?php echo (in_array("打篮球",$_POST['aihao']))?"checked":"";?>/>打篮球
<input type="checkbox" name="aihao[]" value="羽毛球" <?php echo (in_array("羽毛球",$_POST['aihao']))?"checked":"";?>/>羽毛球
</td>
</tr>
<tr>
<td class="a1">个人写真:</td>
<td class="a2" > <input type="file" name="file" /></td>
</tr>
<tr>
<td class="a1">描述:</td>
<td class="a2"> <textarea name="miaoshu" ></textarea></td>
</tr>
<tr align="center">
<td colspan="2"><input type="submit" name="submit" value="提交"/></td>
</tr>
</table>
<?php
if(isset($_POST['submit'])){
echo "姓名:".$_POST['username']."<br>";
echo "性别:".$_POST['sex']."<br>";
echo "密码:".$_POST['password']."<br>";
echo "学历:".$_POST['xueli']."<br>";
echo "爱好:";
for($i=0;$i<count($_POST['aihao']);$i++){
echo $_POST['aihao'][$i];
} echo" <br>";
echo "个人写真:";
$path=$_FILES['file']['name'];
move_uploaded_file($_FILES['file']["tmp_name"],$path);
echo "<img src='$path' class='a3'><br>";
echo "描述:".$_POST['miaoshu'];
}
?>
</form>
</body>
</html>
二、php分页
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<?php
$sum=100;//总条数
$size=10;//每页条数
$pagecount=ceil($sum/$size);//总页数
$page=(isset($_GET['page']))?($_GET['page']):1;//当前页
for($i=($page-1)*$size;$i<$page*$size;$i++){
echo '<a href="count.php?nid='.$i.'">请点击打开第'.$i.'条新闻</a><br>';
}
for($i=1;$i<=$pagecount;$i++){
echo "|<a href='?page=$i'>$i</a>|";
}
?>
<br>
<h4>总共有<?php echo $pagecount?>页,当前第<?php echo $page?>页</h4>
<hr>
|<a href="?page=1">首页</a>|
|<a href="?page=<?php echo ($page>1)?($page-1):1?>">上一页</a>|
|<a href="?page=<?php echo ($page<$pagecount)?($page+1):$pagecount?>">下一页</a>|
|<a href="?page=<?php echo $pagecount?>">尾页</a>|
</body>
</html>
三、留言板
//messageShow.php 展示页面
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>查看留言</title>
<link rel="stylesheet" href="./message-check.css">
<link rel="stylesheet" href="./iconfont/iconfont.css">
<link rel="stylesheet" href="./box.css">
</head>
<body>
<?php
require 'connect.php';
if (isset($_GET['find_name'])) {
$sql = "select count(*) as count from usermessage join messageboard on usermessage.mb_id=messageboard.mb_id where mb_name like '%" . $_GET['find_name'] . "%'";
$res = mysqli_query($con, $sql);
if (!$res) {
echo "<script>alert('查询出错11!'); window.location.href='messageShow.php';</script>";
}
if (!mysqli_affected_rows($con)) {
echo "<script>alert('没有查询到数据!'); window.location.href='messageShow.php';</script>";
exit();
}
//$count = mysqli_num_rows($res);
while ($row = mysqli_fetch_array($res)) {
$count = $row['count'];
}
$sum = $count;
$size = 2;
$page = (isset($_GET['page'])) ? $_GET['page'] : 1;
$pagecount = ceil($sum / $size);
$sql1 = "select distinct mb_name from usermessage join messageboard on usermessage.mb_id=messageboard.mb_id where mb_name like '%" . $_GET['find_name'] . "%'";
$res1 = mysqli_query($con, $sql1);
} else {
$sql1 = "select * from messageboard";
$res1 = mysqli_query($con, $sql1);
// $sql1 = "select count(*) as count from messageboard";
// $res1 = mysqli_query($con, $sql1);
// $row1 = mysqli_fetch_array($res1);
// $count = $row1['count'];
$sql = "select count(*) as count from usermessage";
//$sql1 = "select * from messageboard";
$res = mysqli_query($con, $sql);
while ($row1 = mysqli_fetch_array($res)) {
$count = $row1['count'];
//echo $count;
}
//$count = count($row1);
// $count=mysqli_num_rows($res1);
$sum = $count;
$size = 2;
$page = (isset($_GET['page'])) ? $_GET['page'] : 1;
$pagecount = ceil($sum / $size);
}
?>
<!-- 留言搜索 -->
<div class="message-check">
<div class="message-container">
<ul class="messageCheck-ul">
<form action="messageShow.php" method="get" enctype="multipart/form-data">
<li class="messageCheck-find">
<span>留言搜素:</span>
<span class="messageCheckFind-input">
<input type="text" name="find_name" value="<?php echo $_GET['find_name'] ?>">
</span>
<input class="find" type="submit" value="搜索">
</li>
</form>
<li class="messageCheck-label">
<span>留言标签:</span>
<?php
while ($row = mysqli_fetch_array($res1)) {
?>
<span><?php echo $row['mb_name']; ?></span>
<?php
}
?>
</li>
<?php
if (isset($_GET['find_name'])) {
$sql = "select * from usermessage join messageboard on usermessage.mb_id=messageboard.mb_id where mb_name like '%" . $_GET['find_name'] . "%' order by um_id asc limit " . ($page - 1) * $size . "," . $size . ";";
$res = mysqli_query($con, $sql);
if ($res === false) {
echo "语法错误" . mysqli_error($con);
}
if (!mysqli_affected_rows($con)) {
echo "<script>alert('没有找到该信息!'); window.location.href='messageShow.php';</script>";
exit();
}
} else {
$sql = "select * from usermessage order by um_id asc limit " . ($page - 1) * $size . "," . $size . ";";
$res = mysqli_query($con, $sql);
}
while ($row = mysqli_fetch_array($res)) {
?>
<li class="messageCheck-result">
<span id="result-1">留言者:</span>
<span><?php echo $row['um_name']; ?></span>
<span id="result-2">留言时间:</span>
<span><?php echo $row['create_time']; ?></span>
<br><br>
<span id="result-3">留言内容:</span>
<span><?php echo $row['um_content']; ?></span>
<span id="result-4"><i>
<?php
echo "|<a href='message_update.php?um_id=" . $row['um_id'] . "'>编辑</a>|";
?></i>
<i> <?php
echo "|<a href='message_delete_do.php?um_id=" . $row['um_id'] . "'>删除</a>|";
?></i></span>
</li>
<?php
}
?>
<li class="pageCount">
<span>当前第<?php echo $page; ?>页,共有<?php echo $pagecount; ?>页</span>
</li>
<li class="page">
<?php
for ($i = 1; $i <= $pagecount; $i++) {
$find_name = $_GET['find_name'];
?>
<span><?php echo "|<a href='messageShow.php?page=$i&find_name=$find_name'>$i</a>|"; ?> </span>
<?php
}
mysqli_close($con);
?>
</li>
<li class="bott">
<ul>
<li><a href='?page=1& find_name=<?php echo $_GET['find_name'] ?>'>首页</a></li>
<li><a href='?page=<?php echo ($page > 1) ? ($page - 1) : 1; ?> & find_name=<?php echo $_GET['find_name'] ?>'>上一页</a></li>
<li><a href='?page=<?php echo ($page < $pagecount) ? ($page + 1) : ($pagecount); ?>& find_name=<?php echo $_GET['find_name'] ?>'>下一页</a></li>
<li><a href='?page=<?php echo $pagecount; ?>& find_name=<?php echo $_GET['find_name'] ?>'>尾页</a></li>
<li><a href='commitMessage.php'>插入</a></li>
</ul>
</li>
</ul>
</div>
</div>
<!-- /留言搜索 -->
</body>
</html>
其它代码在包中
//message_update.php 修改页面
//message_update_do.php //修改执行
//message_delete_do.php 删除执行
//connect.php 数据库连接
//commitMessage.php 留言添加页面
//commitMessage_do.php 留言添加执行
四、php简单增删改查
效果1
效果2:
//展示代码
//show.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
</body>
</html>
<?php
require 'connect.php';
include 'header.php';
$sql = "select * from goods";
$res = mysqli_query($con, $sql);
if (!$res) {
die("查询失败!" . mysqli_error($con));
}
?>
<table border=1px>
<tr>
<th>编号</th>
<th>商品名字</th>
<th>当前上架状态</th>
<th>商品上传人</th>
<th>是否参与促销活动</th>
<th>创建时间</th>
<th>更新时间</th>
<th>修改</th>
<th>删除</th>
</tr>
<?php
while ($row = mysqli_fetch_array($res)) {
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['g_name']; ?></td>
<td><?php echo $row['status']; ?></td>
<td><?php echo $row['up_user_id']; ?></td>
<td><?php echo $row['is_promotion']; ?></td>
<td><?php echo $row['created_at']; ?></td>
<td><?php echo $row['updated_at'] ?></td>
<td><?php
echo "<a href='update.php?no=" . $row['id'] . "'>编辑</a>||";
?></td>
<td><?php
echo "<a href='del_do.php?no=" . $row['id'] . "'>删除</a><br>";
?></td>
</tr>
<?php
}
?>
</table>
<?php
mysqli_close($con);
exit();
?>
//插入
//insert.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body >
<?php
include 'header.php';
?>
<table>
<form method="post" action="insert_do.php">
<tr>
<td>商品名:</d>
<td><input type="text" name="g_name" /></td>
</tr>
<tr>
<td class="a1"> 当前上架状态:</td>
<td class="a2"><select name="status">
<option value="在售" selected>在售</option>
<option value="未售" >未售</option>
<option value="无库存">无库存</option>
</select>
</td>
</tr>
<tr>
<td>商品上传人:</td>
<td><input type="number" name="up_user_id" /></td>
</tr>
<tr>
<td>是否参与促销活动:</td>
<td><input type="radio" name="is_promotion" value="Y" checked/>Y<input type="radio" name="is_promotion" value="N"/>N</td>
</tr>
<tr>
<td cowspan="2" class="sty1"><input type="submit" name="submit" value="插入"/></td>
</tr>
</form>
</table>
</body>
</html>
//insert_do.php
<?php
require 'connect.php';
$sql = "insert into goods(g_name,status,up_user_id,is_promotion) values('" . $_POST['g_name'] . "','" . $_POST['status'] . "'," . $_POST['up_user_id'] . ",'" . $_POST['is_promotion'] . "')";
$res = mysqli_query($con, $sql);
if (!$res) {
echo mysqli_error($con);
exit();
}
if (!mysqli_affected_rows($con)) {
echo "<script>alert('插入出错!'); window.location.href='show.php';</script>";
exit();
}
mysqli_close($con);
echo "<script>alert('插入成功!'); window.location.href='show.php';</script>";
exit();
?>
//删除
//del_do.php
<?php
require 'connect.php';
if(!isset($_GET['no']))
{
echo "<script>alert('请从提交页面选择信息!'); window.location.href='show.php';</script>";
exit();
}
$sql="delete from goods where id=".$_GET['no'];
$res=mysqli_query($con,$sql);
if(!$res)
{
die("删除错误".mysqli_error($con));
exit();
}
if(!mysqli_affected_rows($con))
{
echo "<script>alert('没有找到待删除的数据!'); window.location.href='show.php';</script>";
exit();
}
header("location:show.php");
?>
//修改
//update.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<?php
include 'header.php';
require 'connect.php';
// $_POST['art']=(isset($_POST['art']))?($_POST['art']):array();
if (!isset($_GET['no'])) {
echo "<script>alert('请选择要编辑的信息!'); window.location.href='show.php';</script>";
exit();
}
$sql = "select * from goods where id=" . $_GET['no'];
$res = mysqli_query($con, $sql);
if (!$res) {
die("查询错误" . mysqli_error($con));
}
if (!mysqli_affected_rows($con)) {
echo "<script>alert('没有找到待修改的数据!'); window.location.href='show.php';</script>";
exit();
}
$id;
$g_name;
$status;
$up_user_id;
$is_promotion;
while ($row = mysqli_fetch_array($res)) {
$id = $row['id'];
$g_name = $row['g_name'];
$stutus = $row['stutus'];
$up_user_id = $row['up_user_id'];
$is_promotion = $row['is_promotion'];
}
mysqli_close($con);
?>
<table>
<form method="post" action="update_do.php">
<tr>
<td><input type="hidden" name="id" value="<?php echo $id; ?>" /></td>
</tr>
<tr>
<td>商品名:</d>
<td><input type="text" name="g_name" value="<?php echo $g_name; ?>" /></td>
</tr>
<tr>
<td class="a1"> 当前上架状态:</td>
<td class="a2"><select name="status">
<option value="在售" <?php echo ($_POST['xueli'] == "在售") ? "selected" : ""; ?>>在售</option>
<option value="未售" <?php echo ($_POST['xueli'] == "未售") ? "selected" : ""; ?>>未售</option>
<option value="无库存" <?php echo ($_POST['xueli'] == "无库存") ? "selected" : ""; ?>>无库存</option>
</select>
</td>
</tr>
<tr>
<td>商品上传人:</td>
<td><input type="number" name="up_user_id" value="<?php echo $up_user_id; ?>" /></td>
<tr>
<td>是否参与促销活动:</td>
<td><input type="radio" name="is_promotion" value="Y" <?php echo ($is_promotion == "Y") ? "checked" : ""; ?> />Y<input type="radio" name="is_promotion" value="N" <?php echo ($is_promotion == "N") ? "checked" : ""; ?> />N</td>
</tr>
<tr>
<td cowspan="2" class="sty1"><input type="submit" name="submit" value="确认修改" /></td>
</tr>
</form>
</table>
</body>
</html>
//update_do.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<?php
require 'connect.php';
if (!isset($_POST['id'])) {
echo "<script>alert('请从页面选择信息!'); window.location.href='show.php';</script>";
exit();
}
$sql = "update goods set g_name='" . $_POST['g_name'] . "',status='" . $_POST['status'] . "',up_user_id=" . $_POST['up_user_id'] . ",is_promotion='" . $_POST['is_promotion'] . "' where id=" . $_POST['id'] . "";
echo $sql;
$res = mysqli_query($con, $sql);
if (!$res) {
die("修改错误" . mysqli_error($con));
exit();
}
if (!mysqli_affected_rows($con)) {
echo "<script>alert('没有找到待修改的数据!'); window.location.href='show.php';</script>";
exit();
}
echo "<script>alert('数据修改成功!'); window.location.href='show.php';</script>";
mysqli_close($con);
exit();
?>
</body>
</html>
?>
//数据库连接
//connect.php
<?php
$con=mysqli_connect("localhost","root","root","demo_new");
if(!$con){
die("连接失败!".mysqli_connect_error($con));
}
mysqli_query($con,"set names utf8");
?>
//header.php
<a href="show.php">显示</a>
<a href="insert.php">插入</a> <br><hr>
mysql
# Host: localhost (Version 5.7.23-log)
# Date: 2023-06-09 22:20:16
# Generator: MySQL-Front 6.0 (Build 2.20)
#
# Structure for table "goods"
#
CREATE TABLE `goods` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`g_name` varchar(255) DEFAULT NULL COMMENT '名字',
`status` varchar(255) DEFAULT NULL COMMENT '上货状态',
`up_user_id` int(11) DEFAULT NULL COMMENT '商品上传人',
`is_promotion` varchar(4) DEFAULT NULL COMMENT '是否参与促销活动',
`created_at` timestamp NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
`updated_at` timestamp NULL DEFAULT NULL ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
PRIMARY KEY (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=8 DEFAULT CHARSET=utf8 COMMENT='商品表';
五、 php(简单验证码)
简单验证码
<?php
$im=imagecreate(100,30);//创建图像
imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));//随机背景颜色分配
for($i=0;$i<20;$i++){
$pointcolor=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));//随机点颜色分配
imagesetpixel($im,rand(0,100),rand(0,30),$pointcolor);//随机位置画点20个
}
for($j=0;$j<3;$j++){
$linecolor=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));//线的颜色
imageline($im,rand(0,100),rand(0,30),rand(0,100),rand(0,30),$linecolor);//随机画3条线段
}
$str='1234567890qwertyuiopasdfghjklzxcvbnm';//验证码类容
$text="";
for($k=0;$k<4;$k++){//循环取随机取出验证码的各个字符
$text=$str[rand(1,36)].$text;
}
$textcolor=imagecolorallocate($im,rand(0,255),rand(0,255),rand(0,255));//颜色分配
imagestring($im,40,5,8,$text,$textcolor);//添加字符和颜色
ob_clean();//清除缓存缓冲
//显示图片
header("Content-type:image/jpg;charset=utf-8");
imagejpeg($im);
六、php简单年月日输入输出(入门级)
<form method="post" enctype="multipart/from-data">
请输入你的生日:
<br>
<select name="nian" >
<?php
if(isset($_POST['nian'])){
echo '<option value="'.$_POST['nian'].'" selected>'.$_POST['nian'].'</option>';
}
for($i=1920;$i<=2050;$i++){
echo '<option value="'.$i.'">'.$i.'</option>';
}
?>
</select>年
<select name="yue">
<?php
if(isset($_POST['yue'])){
echo '<option value="'.$_POST['yue'].'" selected>'.$_POST['yue'].'</option>';
}
for($j=1;$j<=12;$j++){
echo '<option value="'.$j.'">'.$j.'</option>';
}
?>
</select>月
<select name="ri">
<?php
if(isset($_POST['ri'])){
echo '<option value="'.$_POST['ri'].'" selected>'.$_POST['ri'].'</option>';
}
for($k=1;$k<=30;$k++){
echo '<option value="'.$k.'">'.$k.'</option>';
}
?>
</select>日
<input type="submit" name="submit" value="获取生日"/>
</form>
<?php
if(isset($_POST['submit']))
{
echo "你的生日是".$_POST['nian']."年".$_POST['yue']."月".$_POST['ri']."日";
}?>
七、 php字符串分割
1字符串分割(看效果)
前:
后:
利用 explode进行字符串分割
代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
* {
margin: 0px;
padding: 4px;
}
.table1 {
border: 1px solid aque;
background-color:aqua;
}
tr{
background-color: greenyellow;
}
td {
border: 1px solid aqua;
}
.p1 {
font-size: 20px;
font-weight: 700;
color: red;
}
</style>
</head>
<body>
<form action="2.php" method="post">
<table class="table1">
<tr>
<td>添加投票选项:</td>
<td><textarea name="voters" cols="30" rows="8"></textarea>
<p class="p1">注意:每个选项间用*分隔</p>
</td>
<td><input type="submit" name="submit" value="提交" /></td>
</tr>
<tr>
<td>投票选项:</td>
<td> <?php
if ($_POST['submit']) {
$array = explode("*", $_POST['voters']);
foreach ($array as $k1 => $v1) {
?>
<input type="checkbox" name="happy" value="<?php echo $v1; ?>" /><?php echo $v1; ?>
<?php
}
}
?>
</td>
<td></td>
</tr>
</table>
</form>
</body>
</html>
2.年月日对日期的使用
效果如下:
前:
后:
代码如下:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style>
*{
margin: auto;
padding: 0;
}
table{
text-align: center;
border-color: blue;
}
</style>
</head>
<body>
<?php
if(isset($_POST['submit'])){
$time=time();
$time1=date("H:s:i",$time);
$time2=strtotime($_POST['year']."/".$_POST['month']."/".$_POST['day']);
$time3=date("Y-F-D",$time2);
}
?>
<form action="1.php" method="post">
<table border="1px">
<tr>
<td colspan="3">获取指定任意一天的时间</td>
</tr>
<tr>
<td>输入日期:</td>
<td><input type="text" name="year" />年<input type="text" name="month" />月<input type="text" name="day" />日</td>
<td><input type="submit" name="submit" value="提交"/></td>
</tr>
<tr>
<td colspan="3">正确的日期为:<?php if(isset($_POST['submit'])){echo $time3." ".$time1;}?></td>
</tr>
</table>
</form>
</body>
</html>
3.字符串str_ireplace使用
效果如下:
前:
后:
源码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<form action="1.php" method="post">
<?php
$str="白领女子公寓,温馨街南街200米,交通便利,亲情化专人管理,你的理想选择!";
$count=0;
if(isset($_POST['submit'])){
//str_replace($_POST['serch'],"<b style=\"color: red;\">".$_POST['serch']."</b>",$str);
$str=str_ireplace($_POST['serch'],"<b style=\"color: red;\">".$_POST['serch']."</b>",$str,$count);
echo "共查询出".$count."处:";
}
?>
<p><?php echo $str;?></p>
查询关键字:<input type="text" name="serch" />
<input type="submit" name="submit" value="查询"/>
</form>
</body>
</html>
4.数据库连接 数据查询和添加
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<style type="text/css">
tr {
padding: 4px;
border: 2;
background-color: yellow;
}
.a1 {
text-align: right;
}
.a2 {
text-align: lfet;
}
.a3 {
width: 80px;
height: auto;
}
</style>
</head>
<body>
<?php
$_POST['art'] = (isset($_POST['art'])) ? ($_POST['art']) : array();
?> <form action="#" method="post" enctype="multipart/form-data">
<table>
<tr>
<td class="a1">姓名:</td>
<td class="a2"><input type="text" name="username" placeholder="请输入姓名" value="<?php echo $_POST['username']; ?>" /></td>
</tr>
<tr>
<td class="a1">性别:</td>
<td class="a2"><input type="radio" name="sex" value="男" <?php echo ($_POST['sex'] == "男") ? "checked" : ""; ?> />男
<input type="radio" name="sex" value="女" <?php echo ($_POST['sex'] == "女") ? "checked" : ""; ?> />女
</td>
</tr>
<tr>
<td class="a1">年龄:</td>
<td class="a2"><input type="number" name="age" value="<?php echo $_POST['age']; ?>" placeholder="请输入年龄" /></td>
</tr>
<tr>
<td class="a1"> 地址:</td>
<td class="a2"><select name="dizhi">
<option value="重庆市" <?php echo ($_POST['dizhi'] == "重庆市") ? "selected" : ""; ?>>重庆市</option>
<option value="武汉" <?php echo ($_POST['dizhi'] == "武汉") ? "selected" : ""; ?>>武汉</option>
<option value="广东" <?php echo ($_POST['dizhi'] == "广东") ? "selected" : ""; ?>>广东</option>
</select>
</td>
</tr>
<tr>
<td class="a1">爱好:</td>
<td class="a2"><input type="checkbox" name="art[]" value="唱歌" <?php echo (in_array("唱歌", $_POST["art"])) ? "checked" : ""; ?> />唱歌
<input type="checkbox" name="art[]" value="跳舞" <?php echo (in_array("跳舞", $_POST['art'])) ? "checked" : ""; ?> />跳舞
<input type="checkbox" name="art[]" value="打篮球" <?php echo (in_array("打篮球", $_POST['art'])) ? "checked" : ""; ?> />打篮球
<input type="checkbox" name="art[]" value="羽毛球" <?php echo (in_array("羽毛球", $_POST['art'])) ? "checked" : ""; ?> />羽毛球
</td>
</tr>
<tr>
<td class="a1">邮箱:</td>
<td class="a2"><input type="email" name="emial" placeholder="请输入邮箱" value="<?php echo $_POST['emial']; ?>" /></td>
</tr>
<tr align="center">
<td colspan="2"><input type="submit" name="submit" value="提交" /> <input type="reset" name="reset" value="重置" name="reset" /></td>
</tr>
</table>
</form>
<?php
if (isset($_POST['submit'])) {
$con = mysqli_connect("localhost", "root", "root", "tasklistsystem");
if (!$con) {
die("连接失败" . mysqli_connect_error());
} else {
echo "连接成功<br><hr>";
mysqli_query($con, "set names utf8");
$art1 = "";
for ($i = 0; $i < count($_POST['art']); $i++) {
$art1 = $art1 . $_POST['art'][$i] . "";
}
//$sql1="insert into user(name,gender,age,address,art,email) values('小民','男',14,'湖南','qwer','1234@qq.com')";
$sql1 = "insert into user(name,gender,age,address,art,email) values('" . $_POST['username'] . "','" . $_POST['sex'] . "'," . $_POST['age'] . ",'" . $_POST['dizhi'] . "','" . $art1 . "','" . $_POST['emial'] . "')";
mysqli_query($con, $sql1);
//$list=mysqli_fetch_all($res);
//print_r($list);
$sql = "select * from user";
$res = mysqli_query($con, $sql);
while ($row = mysqli_fetch_array($res)) {
echo $row['id'] . "| |" . $row['name'] . "| |" . $row['gender'] . "| |" . $row['age'] . "| |" . $row['address'] . "| |" . $row['art'] . "| |" . $row['email'] . "<br><hr>";
}
mysqli_close($con);
}
}
?>
</body>
</html>
5.简单加法计算机
5.数据库数据简单增删改查
效果图:
代码:
//show.php 展示
<?php
require 'connect.php';
include 'header.php';
$sql="select * from user";
$res=mysqli_query($con,$sql);
if(!$res){
die("查询失败!".mysqli_error($con));
}
while($row=mysqli_fetch_array($res)){
echo $row['id']." ".$row['name']." ".$row['gender']." ".$row['age']." ".$row['address']." ".$row['art']." ".$row['email']."|";
echo "<a href='update.php?no=".$row['id']."'>编辑</a>||";
echo "<a href='del_do.php?no=".$row['id']."'>删除</a><br><hr>";
}
mysqli_close($con);
?>
//update.php 修改
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<?php
include 'header.php';
require 'connect.php';
// $_POST['art']=(isset($_POST['art']))?($_POST['art']):array();
if (!isset($_GET['no'])) {
echo "<script>alert('请选择要编辑的信息!'); window.location.href='show.php';</script>";
exit();
}
$sql = "select * from user where id=" . $_GET['no'];
$res = mysqli_query($con, $sql);
if (!$res) {
die("查询错误" . mysqli_error($con));
}
// if(!mysqli_affected_rows($con)){
// echo "<script>alert('没有找到待修改的数据!'); window.location.href='show.php';</script>";
// exit();
// }
$id;
$name;
$gender;
$age;
$address;
$art = array();
$email;
while ($row = mysqli_fetch_array($res)) {
//echo $row['id']." ".$row['name']." ".$row['gender']." ".$row['age']." ".$row['address']." ".$row['art']." ".$row['email']."|";
$id = $row['id'];
$name = $row['name'];
$gender = $row['gender'];
$age = $row['age'];
$address = $row['address'];
//str_split($row['art'],"-");
$art = explode("-", $row['art']);
//print_r(explode("-",$row['art']));
//$art=$row['art'];
$email = $row['email'];
}
mysqli_close($con);
?>
<table>
<form method="post" action="update_do.php">
<tr>
<td><input type="hidden" name="id" value="<?php echo $id; ?>" /></td>
</tr>
<tr>
<td>用户名:</d>
<td><input type="text" name="name" value="<?php echo $name; ?>" /></td>
</tr>
<tr>
<td>性别:</td>
<td><input type="radio" name="gender" value="女" <?php echo ($gender == "女") ? "checked" : ""; ?> />女
<input type="radio" name="gender" value="男" <?php echo ($gender == "男") ? "checked" : ""; ?> />男
</td>
</tr>
<tr>
<td>年龄:</td>
<td><input type="mumber" name="age" value="<?php echo $age; ?>" /></td>
</tr>
<tr>
<td>地址:</td>
<td><input type="text" name="address" value="<?php echo $address; ?>" /></td>
</tr>
<tr>
<td>爱好:</td>
<td><input type="checkbox" name="art[]" value="乒乓球" <?php echo (in_array("乒乓球", $art)) ? "checked" : ""; ?> />乒乓球
<input type="checkbox" name="art[]" value="打篮球" <?php echo (in_array("打篮球", $art)) ? "checked" : ""; ?> />打篮球
<input type="checkbox" name="art[]" value="羽毛球" <?php echo (in_array("羽毛球", $art)) ? "checked" : ""; ?> />羽毛球
<input type="checkbox" name="art[]" value="足球" <?php echo (in_array("足球", $art)) ? "checked" : ""; ?> />足球
</td>
</tr>
<tr>
<td>邮箱:</td>
<td><input type="email" name="email" value="<?php echo $email; ?>" /></td>
</tr>
<tr>
<td cowspan="2" class="sty1"><input type="submit" name="submit" value="确认修改" /></td>
</tr>
</form>
</table>
</body>
</html>
//update_do.php 修改执行
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
</head>
<body>
<?php
require 'connect.php';
if (!isset($_POST['id'])) {
echo "<script>alert('请从页面选择信息!'); window.location.href='show.php';</script>";
exit();
}
$art1 = "";
for ($i = 0; $i < count($_POST['art']); $i++) {
$art1 = $art1 . "-" . $_POST['art'][$i];
}
$sql = "update user set name='" . $_POST['name'] . "',gender='" . $_POST['gender'] . "',age=" . $_POST['age'] . ",address='" . $_POST['address'] . "',art='" . $art1 . "',email='" . $_POST['email'] . "' where id=" . $_POST['id'] . "";
echo $sql;
$res = mysqli_query($con, $sql);
if (!$res) {
die("修改错误" . mysqli_error($con));
//echo "删除错误".mysqli_error($con);
//exit();
}
if (!mysqli_affected_rows($con)) {
//die("修改出错:".mysqli_error($con));
echo "<script>alert('没有找到待修改的数据!'); window.location.href='show.php';</script>";
exit();
}
echo "<script>alert('数据修改成功!'); window.location.href='show.php';</script>";
mysqli_close($con);
?>
</body>
</html>
//insert.php 插入
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Document</title>
<sytle>
</sytle>
</head>
<body>
<?php
include 'header.php';
?>
<table>
<form method="post" action="insert_do.php">
<tr>
<td>用户名:</d>
<td><input type="text" name="name" /></td>
</tr>
<tr>
<td>性别:</td>
<td><input type="radio" name="gender" value="女" />女<input type="radio" name="gender" value="男" />男</td>
</tr>
<tr>
<td>年龄:</td>
<td><input type="mumber" name="age" /></td>
</tr>
<tr>
<td>地址:</td>
<td><input type="text" name="address" /></td>
</tr>
<tr>
<td>爱好:</td>
<td><input type="checkbox" name="art[]" value="乒乓球" />乒乓球
<input type="checkbox" name="art[]" value="打篮球" />打篮球
<input type="checkbox" name="art[]" value="羽毛球" />羽毛球
<input type="checkbox" name="art[]" value="足球" />足球
</td>
</tr>
<tr>
<td>邮箱:</td>
<td><input type="email" name="email" /></td>
</tr>
<tr>
<td cowspan="2" class="sty1"><input type="submit" name="submit" value="插入" /></td>
</tr>
</form>
</table>
</body>
</html>
//insert_do.php 插入执行
<?php
require 'connect.php';
// require('conect.php');
$art1="";
for($i=0;$i<count($_POST['art']);$i++){
$art1=$art1."-".$_POST['art'][$i];
}
$sql="insert into user(name,gender,age,address,art,email) values('".$_POST['name']."','".$_POST['gender']."',".$_POST['age'].",'".$_POST['address']."','".$art1."','".$_POST['email']."')";
$res=mysqli_query($con,$sql);
if(!$res){
echo mysqli_error($con);
exit();
}
mysqli_close($con);
header('Location:show.php');
?>
//header.php 头
<a href="show.php">显示</a>
<a href="insert.php">插入</a> <br><hr>
//del_do.php 数据删除
<?php
require 'connect.php';
if (!isset($_GET['no'])) {
echo "<script>alert('请从提交页面选择信息!'); window.location.href='show.php';</script>";
exit();
}
$sql = "delete from user where id=" . $_GET['no'];
$res = mysqli_query($con, $sql);
if (!$res) {
die("删除错误" . mysqli_error($con));
//echo "删除错误".mysqli_error($con);
//exit();
}
if (!mysqli_affected_rows($con)) {
//die("删除出错:".mysqli_error($con));
echo "<script>alert('没有找到待删除的数据!'); window.location.href='show.php';</script>";
exit();
}
header("location:show.php");
//connect.php 数据库连接
<?php
$con=mysqli_connect("localhost","root","root","tasklistsystem");
if(!$con){
die("连接失败!".mysqli_connect_error($con));
}
mysqli_query($con,"set names utf8");
?>