php+mysql的简单应用

纯弱智文,就记录一下代码,方便以后用

php+html+javascript简单的网页设计

扫雷游戏

<?php 
$init = isset($_POST["init"]) ? $_POST["init"] : '';//game restart 
if($init=='重新开始'){
  $_POST["timeshow"] = "00:00";
}
$clickvalue = isset($_POST["clickvalue"]) ? $_POST["clickvalue"] : '';//minesweeping 
$checkflag = 0;//Victory or defeat 
$click_count = 0;//clicks count 
if($init == null && $clickvalue == null){//initialization 
  $_POST = array();//set POST with a array 
  $_POST["rows"] = 9;//set rows 
  $_POST["cols"] = 9;//set cols 
  $_POST["num"] = 10;//set num 
  $_POST["timeshow"] = "00:00"; //set starttime 
  $init = true;//set initialization 
} 
$rows = $_POST["rows"];//get rows 
$cols = $_POST["cols"];//get cols 
$num = $_POST["num"];//get num 
$starttime = isset($_POST["starttime"]) ? $_POST["starttime"] : '';//get starttime 
if($init){// is initialization 
  $timeshow = "00:00";//set starttime 
  $data = array();//data initialization 
  for($i=0;$i<$rows;$i++){//all the rows 
    for($j=0;$j<$cols;$j++){//all the cols 
      $data["data".$i."_".$j] = 0;//set mine with null 
      $data["open".$i."_".$j] = 0;//set node with close 
    } 
  } 
  $i=0;//reset the index,and set the mines(Random setting) 
  while($i < $num){//number of mine 
    $r = rand(0,$rows - 1);//row's index 
    $c = rand(0,$cols - 1);//col's index 
    if($data["data".$r."_".$c] == 0){//if not a mine 
      $data["data".$r."_".$c] = 100;//set the node with a mine 
      $i++; 
    } 
  } 
  for($i=0;$i<$rows;$i++){//all the rows 
    for($j=0;$j<$cols;$j++){//all the cols 
      if($data["data".$i."_".$j] == 100)continue;
      //is not a mine , set number of adjacent mines  
      $cnt = 0; 
      if($i - 1 >= 0 && $j - 1 >= 0 && $data["data".($i - 1)."_".($j - 1)] == 100)$cnt++;//upper left 
      if($i - 1 >= 0 && $data["data".($i - 1)."_".$j] == 100)$cnt++;//left 
      if($i - 1 >= 0 && $j + 1 < $cols && $data["data".($i - 1)."_".($j + 1)] == 100)$cnt++;//lower left 
      if($j - 1 >= 0 && $data["data".$i."_".($j - 1)] == 100)$cnt++;//upper 
      if($j + 1 < $cols && $data["data".$i."_".($j + 1)] == 100)$cnt++;//lower 
      if($i + 1 < $rows && $j - 1 >= 0 && $data["data".($i + 1)."_".($j - 1)] == 100)$cnt++;//upper right 
      if($i + 1 < $rows && $data["data".($i + 1)."_".$j] == 100)$cnt++;//right 
      if($i + 1 < $rows && $j + 1 < $cols && $data["data".($i + 1)."_".($j + 1)] == 100)$cnt++;//lower right 
      $data["data".$i."_".$j] = $cnt;//set number 
    } 
  } 
}else{ 
  $data = $_POST;//get data 
  if($data["data".$clickvalue] == 100){
  //check the value of users click 
    $checkflag = 2;//if click on a mine,gameover 
    for($i=0;$i<$rows;$i++){//all the rows 
      for($j=0;$j<$cols;$j++){//all the cols 
        $data["open".$i."_".$j] = 1;
        //set all nodes to open 
      } 
    } 
  }else{ 
    $node = explode("_", $clickvalue);//get the node of click 
    openNode($node[0],$node[1]);//set nodes to open 
    for($i=0;$i<$rows;$i++){//all the rows 
      for($j=0;$j<$cols;$j++){//all the cols  
        if($data["open".$i."_".$j] == 1)$click_count++;
        //get the number of opennode  
      } 
    } 
    if($rows*$cols - $click_count == $num)$checkflag = 1;
    //if all the node is open,game clear  
  } 
} 
if($checkflag == 0 && $click_count == 1){
//if game is start ,time start 
  $starttime = date("H:i:s"); 
} 
if($starttime){//Computing time and display 
  $now = date("H:i:s"); 
  $nowlist = explode(":",$now); 
  $starttimelist = explode(":",$starttime); 
  $time_count = $nowlist[0]*3600+$nowlist[1]*60 + $nowlist[2] - ($starttimelist[0]*3600+$starttimelist[1]*60 + $starttimelist[2]);
  $min = floor($time_count / 60); 
  $sec = $time_count % 60; 
  $timeshow = ($min>9?$min:"0".$min).":".($sec>9?$sec:"0".$sec); 
}else{ 
  $timeshow = "00:00";//if game is stop , time stop 
} 
function openNode($i,$j){//set nodes to open,if it is can open 
  global $rows;//get the rows 
  global $cols;//get the cols 
  global $data;//get the data 
  if($i < 0 || $i >= $rows || $j < 0 || $j >= $cols || $data["open".$i."_".$j])return;
  //it is not a node,or it has been opened 
  $data["open".$i."_".$j] = 1;//open the node 
  if($data["data".$i."_".$j] > 0)return;//need to continue? 
  openNode($i - 1,$j - 1); 
  openNode($i - 1,$j); 
  openNode($i - 1,$j + 1); 
  openNode($i,$j - 1); 
  openNode($i,$j + 1); 
  openNode($i + 1,$j - 1); 
  openNode($i + 1,$j); 
  openNode($i + 1,$j + 1); 
} 
?> 

<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 
<title>扫雷游戏</title> 
</head> 
<body> 
<form action="" method="post"> 
<input type="hidden" name="starttime" value="<?php echo $starttime;?>"/> 
<input type="hidden" name="clickvalue"/> 
<table style="top:10px;left:0px;z-index:0;margin:10px auto" border="1px"> 
<tr> 
<td width="100px" align="center"> 
  <table width="100%" border="1px"> 
    <tr><td>行数:</td><td><input type="text" name="rows" value="<?php echo $rows;?>" size="1"/></td></tr> 
    <tr><td>列数</td><td><input type="text" name="cols" value="<?php echo $cols;?>" size="1"/></td></tr> 
    <tr><td>雷数:</td><td><input type="text" name="num" value="<?php echo $num;?>" size="1"/></td></tr> 
    <tr><td colspan="2" align="center"><input type="submit" value="重新开始" name="init" onclick="resettime()" /></td></tr> 
  </table> 
</td> 
<td width="50px" align="center"><font size="10px"><?php echo $checkflag < 2?"☺":"☹";?></font></td> 
<td width="100px" align="center"> 
<?php  
  if($checkflag == 1)echo "恭喜,雷全部清掉了!<br />"; 
  else if($checkflag == 2)echo "太挫了,又被雷炸死了<br />"; 
?> 
  <input type="text" onclick="timeshow(e)" name="timeshow" value="<?php echo $timeshow;?>" size="4" readonly > 
</td> 
</tr> 
</table> 
<table style="top:155px;left:0px;z-index:0;margin:10px auto" border="1px"> 
<?php for($i=0;$i<$rows;$i++){ ?> 
  <tr> 
  <?php for($j=0;$j<$cols;$j++){  ?> 
    <td style="width:24px;height:24px;" align="center"> 
    <input type="hidden" name="open<?php echo $i."_".$j;?>" value="<?php echo $data["open".$i."_".$j];?>"> 
    <input type="hidden" name="data<?php echo $i."_".$j;?>" value="<?php echo $data["data".$i."_".$j];?>"> 
    <?php if($data["open".$i."_".$j]){//show the value of node,if the node has been opened ?> 
      <?php echo $data["data".$i."_".$j]==100?"☀":$data["data".$i."_".$j];?> 
    <?php }else{//show a button ,if the node has not been opened ?>
      <input type="button" value="" onclick="clickNum('<?php echo $i."_".$j;?>')" style="width:20px;height:20px;"> 
    <?php } ?> 
    </td> 
  <?php } ?> 
  </tr> 
<?php } ?> 
</table> 
</form> 

<script type="text/javascript"> 
function clickNum(value){//click a node 
  <?php if($checkflag > 0)echo 'return;';//if game is clear or game is over ?> 
  document.forms[0].clickvalue.value = value; 
  document.forms[0].submit(); 
} 
<?php if($checkflag == 0 && $click_count>0)echo 'setTimeout("timerun()",1000);';//time running ?> 
<?php if($checkflag == 1)echo 'alert("恭喜,雷全部清掉了!");';?> 
<?php if($checkflag == 2)echo 'alert("太挫了,又被雷炸死了");';?> 
function timerun(){//time running 
  var timelist = document.forms[0].timeshow.value.split(":"); 
  var sec = parseInt(timelist[1],10) + 1; 
  var min = sec < 60?parseInt(timelist[0],10):(parseInt(timelist[0],10) + 1); 
  document.forms[0].timeshow.value = (min>9?min:"0"+min)+":"+(sec > 9?sec:"0"+sec); 
  setTimeout("timerun()",1000); 
} 
</script> 
</body> 
</html>

在这里插入图片描述
在这里插入图片描述

网页端的数据库增删改查

在phpMyAdmin上创建一个数据库EXP3
在这里插入图片描述
在EXP3数据库中新建一张数据表News
在这里插入图片描述
将数据表News的id字段改为自增字段,否则之后增加的数据行的id都是相同的,会影响数据的查找,修改和删除

在这里插入图片描述

在这里插入图片描述

给数据表News添加一个字段repoter

在这里插入图片描述
在这里插入图片描述
添加conn.php,这个代码用于设置连接数据库时必要的信息,这个代码文件需要在每一个使用到mysql数据的php文件上include

<?php
$hostname = "localhost"; //主机名,可以用ip代替
$database = "exp3"; //数据库名称 
$username = "root"; //数据库用户名
$password = "";  //数据库密码
$conn = mysqli_connect($hostname, $username, $password); 
$db = mysqli_select_db($conn, $database);
?>

php在数据库添加数据

编写add_new.php文件,这个代码可用于添加新的新闻的信息

<!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>
<style>
form{
padding:0px;
margin:0px;
}
</style>
</head>
<body>
<table width="70%" height="30" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td align="center">请填写要添加新闻的信息</td>
</tr>
</table>

<!--数据传输的方法为post-->
<form action="save_news.php" method="post">
<!--这里是一个表单,意思是以post方式把下面输入的数据传到save_news.php页面. ,表单以</form>结束-->
<table width="70%" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<!--新闻标题的name为title-->
<td width="30%" align="right">新闻标题</td>
<td width="70%" align="left"><input type="text" name="title" size="30"/></td>
</tr>
<tr>
<!--新闻内容的name为content-->
<td align="right">新闻内容</td>
<td align="left"><textarea name="content" cols="30" rows="5"></textarea></td>
</tr>
</table>
<table width="70%" height="30" border="0" align="center" cellpadding="0" cellspacing="0">
<tr>
<td align="center"><input type="submit" name="submit1" value="确定添加"/></td>
</tr>
</table>
</form>

</body>
</html>

在这里插入图片描述
编写save_news.php文件,接收用户输入新闻信息并添加进数据库

<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</html>
<?php
include ("conn.php");
// 进行数据库查询
mysqli_query($conn,"set names utf-8");
error_reporting(0); 
//下面先接收从add_news.php传过来的新闻标题与新闻内容.

// PHP变量是以$开头的,如$a,$b 变量,与C,C++一样都是以";"分号结果一句子;注释也与C,C++一样.
// 因为add_news.php表单定义的传输方式为POST所以这里要对应用POST接收,如果定义为GET则要用GET接收.

// $_POST用于收集来自“method="post"”的表单中的值
$title=$_POST['title'];  // 保存从add_news.php传过来的新闻标题title值.
$content=$_POST['content'];  // 保存新闻内容值.

//下面用一if语句检测系统的香港时区的时间,我们用的PHP一般以香港时间为准的,
if(function_exists('date_default_timezone_set')) { 
   date_default_timezone_set('Hongkong');//该函数为PHP5.1内置. 
} 
//把获取到的系统当前时间赋给变量$add_time
$add_time=date("Y-m-d");
   
// 执行插入的sql
$sql = "INSERT INTO news (title,content,add_time)
        VALUES ('$title','$content','$add_time')";

$result = @mysqli_query($conn,$sql) or die(mysqli_error($conn));  

if($result){
    echo "添加新闻成功,<a href='add_news.php'>返回继续</a>";
}
else{
    echo "添加新闻失败,<a href='add_news.php'>请返回</a>";
}

在phpMyAdmin我们能看到成功添加的数据

在这里插入图片描述

php查询数据库数据

编写read.php来进行数据库的多行查询

<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</html>
<?php 
include ("conn.php");
mysqli_query($conn,"set names utf-8");
echo "下面为查询标题中有计软学院四个字的的新闻数据.<br>";
//这样可能有很多标题包含有这四个字的新闻都会显示出来. 
//大家可以添加多几条新闻试试.还可以用OR 或AND 限制更多查询条件.
$query = "select * from news where title like '%计软学院%'";    
$res = mysqli_query($conn,$query) or die(mysqli_error($conn));
$row = mysqli_num_rows($res);    //如果查询成功这里返回真否则为假
if($row){
    for($i = 0;$i < $row;$i++)           
    {   
        //将返回的结果(数据表)存为数组
        $dbrow=mysqli_fetch_array($res);
        $id=$dbrow['id']; 
        $title=$dbrow['title']; 
        $content=$dbrow['content']; 
        $add_time=$dbrow['add_time'];
        $content = str_replace("\r", "<br>", $content);   //用替换函数据新闻内容中的空格与换行符换回html语法输出.
        $content= str_replace(" ", "&nbsp;", $content);

        echo 'id: ',$id;       //PHP的输出用 echo 
        echo "  ";
        echo 'title: ',$title."<br>";   //<br>为换行
        echo 'add_time: ',$add_time;
        echo "<br>";
        echo 'content: ',$content;
        echo "<br>";
        echo "--------------------------------";
        echo "<br>";
    }
}
else{
    echo "无相关数据";
}

?>

在这里插入图片描述

php更新数据库数据

编写news.php,来显示所有新闻来让我们进行选择修改

<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</html>
<?php 
include ("conn.php");
mysqli_query($conn,"set names utf-8");
echo "下面显示所有新闻数据.<br>";
$query = "select * from news ";  
$res = mysqli_query($conn,$query) or die(mysqli_error($conn));
$row = mysqli_num_rows($res);    //如果查询成功这里返回真否则为假
if($row)
{
    for($i = 0;$i < $row;$i++)            //这里用一个FOR 语句查询显示多条结果
    { 
        $dbrow=mysqli_fetch_array($res);
        $id=$dbrow['id']; 
        $title=$dbrow['title']; 
        //$content=$dbrow['content'];   内容不用显示了.
        $add_time=$dbrow['add_time'];
        //$content = str_replace("\r", "<br>", $content);   //用替换函数据新闻内容中的空格与换行符换回html语法输出.
        //$content= str_replace(" ", "&nbsp;", $content);

        echo 'id: ',$id;       //PHP的输出用 echo 
        echo "  ";
        echo "<a href='edit_news.php?id=$id'>$title.</a>";   //这里意思是以GET方式把id这个变量传到edit_news.php这个页面.
        echo "   ";
        echo "<a href='del_news.php?id=$id'><font color='red'>删除</font>";
        echo "<br>";
    }
}
else{
    echo "无相关数据";
}
?>

在这里插入图片描述
编写edit_news.php文件,来显示具体要改动的那条新闻的信息

<!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>
<style>
form{
padding:0px;
margin:0px;
}
</style>
</head>

<body>
<?php 
include ("conn.php");
mysqli_query($conn,"set names utf-8");
$id=$_GET['id'];   //先接收从news.php传过来的ID值以确定要修改的新闻
//接着查出该新闻有关数据.
$query = "select * from news where id=$id";  
$res = mysqli_query($conn,$query) or die(mysqli_error($conn));
$row = mysqli_num_rows($res);    //如果查询成功这里返回真否则为假
$dbrow=mysqli_fetch_array($res);
$id=$dbrow['id']; 
$title=$dbrow['title']; 
$content=$dbrow['content'];  
$add_time=$dbrow['add_time'];

?>
<table width="70%" height="30" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td align="center">请填写要修改新闻的信息</td>
  </tr>
</table>
<form action="save_edit_news.php" method="post">  <!---把内容传到save_edit_news.php 保存-->
<table width="70%" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td width="30%" align="right">新闻标题</td>
    <td width="70%" align="left"><input type="text" name="title" size="30" value="<?php echo $title; ?>"/></td>
  </tr>
  <tr>
    <td align="right">新闻内容</td>
    <td align="left"><textarea name="content" cols="30" rows="5"><?php echo $content; ?></textarea></td>
  </tr>
</table>
<table width="70%" height="30" border="0" align="center" cellpadding="0" cellspacing="0">
  <tr>
    <td align="center">
	<input type="hidden" name="id" value="<?php echo $id; ?>" /> <!----这里很重要,以隐藏方式把ID值也传到save_edit_news.php文,以确定更新的具体是哪条新闻--->
	<input type="submit" name="submit1" value="确定修改"/></td>
  </tr>
</table>
</form>

</body>
</html>

在这里插入图片描述

编写save_edit_news.php文件,用于把修改的内容更新到数据库

<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</html>
<?php 
include ("conn.php");
mysqli_query($conn,"set names UTF-8");
//先接收传过来的数据.
$id=$_POST['id'];
$title=$_POST['title'];
$content=$_POST['content'];

$query = "Update news set title='$title',content='$content' where id=$id"; 
echo $query;
$res = mysqlI_query($conn,$query) or die(mysqli_error($conn));
//echo "修改成功";
if($res){
    ?>
    <?php
    echo "<script language=javascript>window.alert('修改成功,请返回');history.back(1);</script>";
}else{
    ?>
    echo "<script language=javascript>window.alert('修改失败,请返回');history.back(1);</script>";
    <?php
}
?>

php删除数据库的数据

编写del_news.php文件,用以进行删除操作

<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
</html>
<?php 
include ("conn.php");
mysqli_query($conn,"set names utf-8");
$id=$_GET['id'];
$sql = "delete from news where id=$id";
$result1 = mysqli_query($conn,$sql) or die(mysqli_error($conn));
if($result1){
    ?>
    echo "<script language=javascript>window.alert('成功删除,请返回');history.back(1);</script>";
    <?php
} //result1==true
?>

如下图所示,点击news.php中的删除键之后,网页跳转到del_news.php文件,并弹窗提示我们成功删除数据

在这里插入图片描述

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值