PHP MySQLi 增删改查

     最近几天,我们一直在学习利用MySQLi访问数据库并对其中的数据进行操作。今天给大家展现一个完整的例子,我们来制作一个新闻发布系统,利用MySQLi来操作数据库,实现对新闻的添加、修改、删除、查询等基本功能。(以下代码分为前端显示和后台php处理代码,中间用空行隔开,注意区分)

1、登陆页面:这由本公司内部人员通过员工号和身份证号才能登陆,代码如下:

//前端显示部分

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>
<h2>管理员登陆</h2>
<form action="dlyz.php" method="post">
<div>用户名:<input type="text" name="user" value="请输入您的工号" /></div>
<br />
<div>密&nbsp;&nbsp;码:<input type="password" name="psd" /></div>
<br />
<input type="submit" value="登录" />
<input type="submit" value="注册新用户" formaction="zhuc.php"/>
</form>
</body>
</html>

//php代码对提交登陆的信息进行处理

<?php

$user = $_POST["user"];
$psd = $_POST["psd"];


//造对象
$db = new MySQLi("localhost","root","","newssystem");

//判断是否出错
!mysqli_connect_error() or die("连接失败!!");

//写sql语句

$sql = "select psd from yonghu where user='{$user}'";


//执行SQL语句

$result = $db-> query($sql);
$v = $result->fetch_row();
if($psd==$v[0])
{
header("location:fabuxinwen.php");
}
else
{
echo"您输入的用户名或密码不正确,请重新输入!!";
}

2、注册页面:公司/报社来了新员工,只有注册后才能登陆

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>
<h2>欢迎注册</h2>
<body>

<form action="zhucyz.php"method="post">
<div>用户名:<input type="text" name="user" value="请输入您的工号"/></div><br />

<div>密&nbsp;&nbsp;码:<input type="password" name="psd" /></div><br />

<input type="submit" value="提交" />
</form>

</body>
</html>

 

<?php
$user = $_POST["user"];
$psd = $_POST["psd"];


//造对象
$db = new MySQLi("localhost","root","","newssystem");

//判断是否出错
!mysqli_connect_error() or die("连接失败!!");

//写sql语句

$sql = "insert into yonghu values('{$user}','{$psd}')";

//执行SQL语句

$result = $db-> query($sql);

if($result)
{
header("location:dl.php");
}
else
{
echo"很抱歉,注册失败!!";
}

 

3、登陆进去以后,是发布新闻页面,点击提交按钮进行提交保存到已经建立好的数据库,点击查看按钮进行查看确认

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>
<div style="width:100%; text-align:center" >
<h2>发布新闻</h2>
<form method="post">
<input type="hidden" name="newsid"/>
<table style="margin:0 auto; text-align:left" >
<tr>
<td >标题:</td><td><input type="text" style="width:400px" name="title" /></td>
</tr>
<tr>
<td >作者:
</td><td><input type="text" style="width:400px" name="Author" /></td>
</tr>
<tr>
<td >来源:</td><td><input type="text" style="width:400px" name="source"/></td>
</tr>
<tr>
<td >内容:</td>
<td><textarea cols="auto" rows="auto" style="width:400px; height:400px" name="content"></textarea></td>
</tr>

</table><br />
<?php
$time = date('y-m-d h:i:s');

echo "<input type=\"hidden\" name=\"time\" value=\"{$time}\"/>";
?>

<input type="submit" value="提交" formaction="tijiao.php"/>

<input type="submit" value="查看" formaction="chakan.php"/>
</form>
</div>
</body>
</html>

 

<?php
$title = $_POST["title"];
$Author = $_POST["Author"];
$source = $_POST["source"];
$content = $_POST["content"];

//造对象
$db = new MySQLi("localhost","root","","newssystem");

//判断是否出错
!mysqli_connect_error() or die("添加失败!!");

//写sql语句

$sql = "insert into news(title,Author,source,content) values('{$title}','{$Author}','{$source}','{$content}')";


//执行SQL语句

$result = $db-> query($sql);

if($result)
{
header("location:fabuxinwen.php");
}
else
{
echo"很抱歉,添加失败!!";
}

 

4、查看页面

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>
<table width="70%" border="1px" cellpadding="0" cellspacing="0" style="text-align:center">
<tr>
<td>编号</td>
<td>标题</td>
<td>作者</td>
<td>来源</td>
<td>日期</td>
<td>删除</td>
<td>修改</td>

</tr>

<?php
$db=new mysqli("localhost","root","","newssystem");
!mysqli_connect_error() or die("连接错误");
$sql="select * from news";
$result=$db->query($sql);
while($attr=$result->fetch_row())
{
echo "
<tr>
<td>{$attr[0]}</td>
<td>{$attr[1]}</td>
<td>{$attr[2]}</td>
<td>{$attr[3]}</td>
<td>{$attr[5]}</td>
<td><a οnclick=\" return confirm('确定删除')\" href='scchuli.php?newsid={$attr[0]}'>删除</a></td>
<td><a href='xiugai.php?newsid={$attr[0]}'>修改</a></td>
</tr>
";
}

?>
</table>
</body>
</html>

5、在查看页面可以进行相关内容的修改和删除

//修改:在点击修改按钮后,会跳转到修改页面,此时会显示出之前已经发布的内容、标题等相关内容

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>无标题文档</title>
</head>

<body>
<?php
$id = $_GET["newsid"];

$db=new mysqli("localhost","root","","newssystem");
!mysqli_connect_error() or die("连接错误");
$sql="select * from news where newsid='{$id}'";
$result=$db->query($sql);
$attr=$result->fetch_row();
?>
<div style="width:100%; text-align:center" >
<h2>修改新闻</h2>
<form action="xgchuli.php" method="post">
<input type="hidden" name="newsid" <?php echo "value='{$attr[0]}'";?>/>
<table style="margin:0 auto; text-align:left" >
<tr>
<td >标题:</td><td><input type="text" style="width:400px" name="title" <?php echo "value='{$attr[1]}'";?>/></td>
</tr>
<tr>
<td >作者:
</td><td><input type="text" style="width:400px" name="Author" <?php echo "value='{$attr[2]}'";?>/>
</td>
</tr>
<tr>
<td >来源:</td><td><input type="text" style="width:400px" name="source" <?php echo "value='{$attr[3]}'";?>/>
</td>
</tr>
<tr>
<td >内容:</td>
<td><textarea cols="auto" rows="auto" style="width:400px; height:400px" name="content"><?php echo "{$attr[4]}";?>
</textarea></td>
</tr>

</table><br />
<?php
$time = date('y-m-d h:i:s');

echo "<input type=\"hidden\" name=\"time\" value=\"{$time}\"/>";
?>

<div><a href="chakan.php"><input type="button" title="查看" value="查看" /></a><input type="submit" title="修改" value="修改"/>
</div>

</form>

</body>
</html>

 

<?php

$id=$_POST["newsid"];
$title=$_POST["title"];
$Author=$_POST["Author"];
$source=$_POST["source"];
$content=$_POST["content"];
$time=$_POST["time"];

$db = new MySQLi("localhost","root","","newssystem");

!mysqli_connect_error() or die("连接失败");

$sql="update news set title='{$title}',Author='{$Author}',source='{$source}',content='{$content}',time='{$time}' where newsid='{$id}' ";

$result=$db->query($sql);
if ($result)
{
header("location:chakan.php");
}
else
{
echo "修改失败";
}

 

//删除:如果要删除一条新闻,在点击删除按钮之后,不会跳转到任何前台显示页面,只会在后台通过php代码进行相关处理

<?php
$id=$_GET["newsid"];

$db=new mysqli("localhost","root","","newssystem");

!mysqli_connect_error() or die("连接失败");

$sql="delete from news where newsid='{$id}'";

$result=$db->query($sql);

if ($result)
{
header("location:chakan.php");
}
else
{
echo "删除失败";
}

6、修改完成之后,点击页面下方的修改按钮进行提交,在这里也不会有相关的前端显示页面,只是在后台利用相关php代码进行相应处理,成功后返回发布新闻页面

<?php
$title = $_POST["title"];
$Author = $_POST["Author"];
$source = $_POST["source"];
$content = $_POST["content"];

//造对象
$db = new MySQLi("localhost","root","","newssystem");

//判断是否出错
!mysqli_connect_error() or die("添加失败!!");

//写sql语句

$sql = "insert into news(title,Author,source,content) values('{$title}','{$Author}','{$source}','{$content}')";


//执行SQL语句

$result = $db-> query($sql);

if($result)
{
header("location:fabuxinwen.php");
}
else
{
echo"很抱歉,添加失败!!";
}

 

转载于:https://www.cnblogs.com/sdzbxfcy/p/5596936.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/372784.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

20162303《程序设计与数据结构》第一周学习总结

学号 2016-2017-2 《程序设计与数据结构》第1周学习总结 教材学习内容总结 本周学习了基本的JAVA知识&#xff0c;虽然比较基础&#xff0c;但是在实际过程中还是出现了许许多多的问题&#xff0c;代码一遍遍的敲错&#xff0c;又一遍遍的修改&#xff0c;刚开始甚至不会切换模…

Java EE与NoSQL的未来

自一段时间以来&#xff0c;我一直在关注NoSQL的近期发展势头&#xff0c;似乎这个流行语也引起了企业Java界的某种关注。 即EclipseLink 2.4开始支持MongoDB和Oracle NoSQL 。 将EclipseLink作为JPA参考实现&#xff0c;您可能想知道这对Java EE 7意味着什么。这里简短说明&am…

【C/C++开发】C语言实现函数可变参数

函数原型: int printf(const char *format[,argument]...) 返 回 值: 成功则返回实际输出的字符数&#xff0c;失败返回-1. 函数说明: 在printf()函数中&#xff0c;format后面的参数个数不确定&#xff0c;且类型也不确定&#xff0c;这些参数都存放在栈内.调用…

java postgresql json_java – 将PostgreSQL JSON列映射到Hibernate值类...

See PgJDBC bug #265.PostgreSQL对数据类型转换过于严格,非常严格.它不会隐式地将文本转换为类似文本的值,例如xml和json.解决此问题的严格正确方法是编写使用JDBC setObject方法的自定义Hibernate映射类型.这可能有点麻烦,所以你可能只想通过创建一个较弱的强制转换来使Postgr…

面向接口编程详解(三)——模式研究

通过前面两篇&#xff0c;我想各位朋友对“面向接口编程”的思想有了一定认识&#xff0c;并通过第二篇的例子&#xff0c;获得了一定的直观印象。但是&#xff0c;第二篇中的例子旨在展示面向接口编程的实现方法&#xff0c;比较简单&#xff0c;不能体现出面向接口编程的优势…

错误学习:Java + OSGi

最近&#xff0c;我致力于在OSGi环境中使Apache Hive工作。 虽然没有被证明是小菜一碟&#xff08;软件对吗&#xff1f;。。为什么我不感到惊讶&#xff1f; &#xff09;&#xff0c;它引导我解决了各种Java和OSGi错误。 在这里&#xff0c;我列出了其中一些让我有些吃力的东…

iOS多Targets管理

序言&#xff1a; 个人不善于写东西&#xff0c;就直奔主题了。 其实今天会注意到多targets这个东西&#xff0c;是因为在学习一个第三方库FBMemoryProfiler的时候&#xff0c;用到了&#xff0c;所以就搜索了一些相关资料&#xff0c;就在这里记录一下。 可能每个人都会遇到这…

优化的34条定律

1.Minimize HTTP Requests 减少HTTP请求 图片、css、script、flash等等这些都会增加http请求数&#xff0c;减少这些元素的数量就能减少响应时间。把多个JS、CSS在可能的情况下写进一个文件&#xff0c;页面里直接写入图片也是不好的做法&#xff0c;应该写进CSS里&#xff0c;…

休眠提示:排序和排序

让我们介绍另一个休眠性能提示。 你还记得以前的休眠的模式后 &#xff1f; 我们有一个与一对多协会有关的星际飞船和军官。 Entity public class Starship {Id GeneratedValue(strategyGenerationType.SEQUENCE) private Long id;public Long getId() {return id;}protected v…

java 基本类型 线程安全_java的基本类型和i++线程安全性的深入解析

在java中&#xff0c;除了long和double的8个字节、64位比特的变量外&#xff0c;其他的基本变量都是原子性的。java存储模型要求获取和存储操作都为原子性&#xff0c;但是对于非volatile的long和double变量&#xff0c;jvm允许将64位的读或写划分为两个32位的操作。如果读和写…

MySQL配置文件mysql.ini参数详解

my.ini&#xff08;Linux系统下是my.cnf&#xff09;&#xff0c;当mysql服务器启动时它会读取这个文件&#xff0c;设置相关的运行环境参数。 my.ini分为两块&#xff1a;Client Section和Server Section。 Client Section用来配置MySQL客户端参数。 要查看配置参数可以用下面…

微信公众平台和微信开放平台的区别

自己也刚开始做微信开发&#xff0c;先写写自己的认识&#xff1a; 用微信公众平台可以做手机端H5页面的微信登录&#xff0c;微信支付 用微信开放平台可以做PC端网页的微信登录。 转载于:https://www.cnblogs.com/mafeng/p/5610770.html

java 传递bean_如何将bean作为参数传递给JSP标记?

我ve created a custom JSP tag that is supposed to accept a list of products to render, but I我无法弄清楚如何将列表传递给标签 . 产品列表作为页面范围的bean存在 . Web应用程序使用Struts taglib在Struts 1.2.x中编写 .这是我的代码的简化版本&#xff1a;renderProduc…

Business Component(BC)和Business Object(BO)

Siebel应用架构的一个成功的地方就是在应用里引入了BC&#xff0c;BO的概念&#xff0c;从而使得几千张关系数据表能够按照业务的含义组织成业务对象&#xff0c;对于业务人员而言具有了业务上的含义&#xff0c;而不仅仅是从技术人员的观点来对待数据&#xff08;就是关系表而…

NetBeans可用性提示

的Java IDE都来了&#xff0c;因为在很长的路要走天的JBuilder的 &#xff08;尽管JBuilder中似乎是一个值得欢迎提前在时间&#xff09;。 当今的Java IDE&#xff08;例如NetBeans &#xff0c; Eclipse &#xff0c; IntelliJ IDEA和JDeveloper &#xff09;是非常先进的工具…

一个JVM进程启动后里面有几个线程

在写Java程序时&#xff0c;通常我们管只有一个main函数&#xff08;而没有别的Thread或Runnable的程序&#xff09;叫单线程程序。但是我们写的这个所谓的单线程程序只是JVM这个程序中的一个线程&#xff0c;JVM本身是一个多线程的程序&#xff0c;至少得有一个垃圾收集器线程…

WPF 反编译后错误处理

1. 首先&#xff0c;手动创建一个WPF工程&#xff08;WpfApplicationReflectorDemo&#xff09; 2. 把生成的WpfApplicationReflectorDemo.exe 拖到ILSpy里 3.点击 File -> Save Code...: 相应的代码会生成到指定地方。 4. 打开应用程序&#xff0c;并且编译它&#xff0c;此…

JavaFX 2 GameTutorial第1部分

介绍 我相信大多数软件开发人员可能会在年轻人&#xff08;年轻人&#xff09;一生中的某一时刻被迫创建游戏来帮助他们学习编程语言&#xff08;我知道我确实做到了&#xff09;。 以前&#xff0c;我的第一台计算机实际上是Franklin Ace 1000 &#xff0c;后来是Apple [] 。 …

虚拟现实-VR-UE4-认识UE4

VR的火热&#xff0c;让每个人都想参与一下&#xff0c; 公司在展会上面搞了一个VR的Demo&#xff0c;关注度超出预期&#xff0c;使得公司高层决定来个VR项目 所以 关于UE4 百度百科地址&#xff1a;http://baike.baidu.com/link?urlmEmbwOcqEuqtkfdu9lNdxVtWAkv0Q6UHZ4VgIHr…

java concurrent 例子_[Java Concurrent] 并发访问共享资源的简单案例

EvenGenerator 是一个偶数生成器&#xff0c;每调用一个 next() 就会加 2 并返回叠加后结果。在本案例中&#xff0c;充当被共享的资源。EvenChecker 实现了 Runnable 接口&#xff0c;可以启动新的线程执行 run() 任务&#xff0c;用于检测所指向的偶数生成器是否每次都返回偶…