PHP 在网页中的运用

与html的结合

1、表单中输入的值在另一个页面中显示

在body中加入一个表单,主要根据 action(地址)=“跳转的网页的名字”,method="post"。

表单设置一个按钮,设置onclick事件,在head中加入JS,设置function ()函数,用var u = document.getElementById("one").value 获取输入框的值。

在跳转的网页中,只需要<?php   $ur= $_POST["userName"]  ?>,得到值就行了。

2、表单与数据库

连接到MySQL 数据库:

必须先创建到达数据库的连接,在 PHP 中,这个任务通过 mysql_connect() 函数完成。

mysql_connect("要连接的服务器","登录使用的用户名","密码");

为了完成数据的增删改,让 PHP 执行,我们必须使用 mysql_query() 函数。此函数用于向 MySQL 连接发送查询或命令。

删除数据:

<?php   

  $id = $_GET["messageid"];

  $conn = @mysql_connect("localhost","root","root") or die("数据库连接失败,请检查你的网络,稍后再试试");

  mysql_select_db("test");

  mysql_query("set names 'utf8'");

  $sql = "delete from `message` where id=$id";

  mysql_query($sql);

  mysql_close($conn); 

?>

插入数据:

<?php    

  $conn = @mysql_connect("localhost","root","root") or die("数据库连接失败,请检查你的网络,稍后再试试");  

  mysql_select_db("test");    

  mysql_query("set names 'utf8'");

  $sql = "INSERT INTO `test`.`message` (`id`,`user`, `title`, `content`, `lastdate`) VALUES (NULL, '编程大浪子', '编程浪子是编程大神', '浪子帅哥', now())";    

  mysql_query($sql);

 ?>

原文地址:https://www.cnblogs.com/zouyajun/p/3691726.html