search
HomeBackend DevelopmentPHP TutorialPHP connects to the database to implement addition, deletion, modification and query operations on the registration page, php addition and deletion_PHP tutorial

PHP connects to the database to implement add, delete, modify and check operations on the registration page, php add and delete

This article shares an example of how to connect PHP to the database to implement add, delete, modify and check operations on the registration page. For your reference, the specific content is as follows

1. Connect to database

<&#63;php
 //本地测试
 $host = '127.0.0.1';
 $port = 3306;
 $user = "root";
 $pwd = "";
 $link = @mysql_connect("{$host}:{$port}",$user,$pwd,true);
 if(!$link) {
  die("Connect Server Failed: " . mysql_error());
 }
 //选择连接的数据库库名
 mysql_select_db("my");
 //设置字符编码utf8
 mysql_set_charset('utf8');
&#63;>

2. Registration page (html page)

<!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" xml:lang="en">
<head>
 <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
 <title>Document</title>
</head>
<body>
<h3 id="注册页面">注册页面</h3>
 <form action="add.php" method='post'>
  <table border='1' cellpadding='0' cellspacing='0' width='80%' bgcolor='#ABCDEF'>
   <tr>
    <td align='right'>用户名</td>
    <td><input type="text" name="username" id=""/>以小写字母开始,长度要求5~10</td>
   </tr>
   <tr>
    <td align='right'>密码</td>
    <td><input type="password" name="password" id=""/>密码不能为空</td>
   </tr>
   <tr>
    <td align='right'>邮箱</td>
    <td><input type="text" name="email" id="" /></td>
   </tr>
   <tr>
    <td align='right'>性别</td>
    <td>
     <input type="radio" name="sex" id="" value='1' />男
     <input type="radio" name="sex" id="" value='2' />女
     <input type="radio" name="sex" id="" value='3' />保密
    </td>
   </tr>
   <tr>
    <td align='right'>个人简介</td>
    <td>
     <textarea name="txt" id="" cols="50" rows="10"></textarea>
    </td>
   </tr>
   <tr>
    <td colspan='2'><input type="submit" name='act' value='注册' /></td>
   </tr>
  </table>
 </form>

</body>
</html>

3. Display registration data in the database

//往数据库中添加数据

<&#63;php
header("Content-type:text/html; charset=utf-8");
//-----------------------连接数据库---------------------------
include_once "connect.php";
//-------------------------将数据连接到数据库------------------
$time=time();
$sql="insert into user (username,password,email,sex,txt,`time`) value('{$_POST['username']}','{$_POST['password']}','{$_POST['email']}','{$_POST['sex']}','{$_POST['txt']}','{$time}')";
$res=mysql_query($sql);
header("location:hello.php");
&#63;>

4. Return to the background interface

<&#63;php
header("Content-type:text/html; charset=utf-8");
//-----------------------连接数据库------------------------------
include_once "connect.php";
//--------------------查询数据库--------------------------------
$query="select * from user";
$result=mysql_query($query);
if(!$result)
{
 die("could not to the database<br/>".mysql_error());
}
//-------------------封装函数-----------------------------
//该函数将数据库的数据写成数组形式
function result2Arr($result){
 while($result_row=mysql_fetch_assoc($result)){
  $arr[] = $result_row;
 }
 return $arr;
}
$arr = result2Arr($result);
foreach($arr as $key=>$value){
 echo "<table border='1px'>";
 echo "<table border='1px' >";
 echo "<tr> ";
 echo "<td width='100px'>".$value['id']."</td>";
 echo "<td width='100px'>".$value['username']."</td>";
 echo "<td width='100px'>".$value['password']."</td>";
 echo "<td width='200px'>".$value['email']."</td>";
 echo "<td width='100px'>".$value['sex']."</td>";
 echo "<td width='100px'>".$value['txt']."</td>";
 echo "<td width='100px'>".date('Y-m-d H:i:s',$value['time'])."</td>";
 echo "<td width='100px'><a href='update1.php&#63;id=$value[id]'>修改</a>    <a href='delete.php&#63;id=$value[id]'>删除</a></td>";
 echo "<tr/>";
 echo "</table>";
}
&#63;>

5. Modify data

//当用户要修改信息时,返回页面,页面中包含之前填写的信息

<!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" xml:lang="en">
<head>
 <meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
 <title>Document</title>
</head>
<body>
<div>

<&#63;php
 include_once "connect.php";
 $sql="select * from user where id='".$_GET['id']."'";
 //echo "sql:".$sql;(显示出修改哪一行)
 $result=mysql_query($sql,$link);
 $arr = result2Arr($result);
 //print_r($arr);
 $row = $arr[0];

function result2Arr($result){
 while($result_row=mysql_fetch_assoc($result)){
  $arr[] = $result_row;
 }
 return $arr;
}
&#63;>
  <h3 id="注册页面">注册页面</h3>
  <form action="update.php" method='post'>
   <input type="hidden" name="id" id="" value="<&#63;php echo $row['id']&#63;>"/>
   <table border='1' cellpadding='0' cellspacing='0' width='80%' bgcolor='#ABCDEF'>
    <tr>
     <td align='right'>用户名</td>
     <td><input type="text" name="username" id="" value="<&#63;php echo $row['username']&#63;>"/>以小写字母开始,长度要求5~10</td>
    </tr>
    <tr>
     <td align='right'>密码</td>
     <td><input type="password" name="password" id=""value="<&#63;php echo $row['password']&#63;>"/>密码不能为空</td>
    </tr>
    <tr>
     <td align='right'>邮箱</td>
     <td><input type="text" name="email" id="" value="<&#63;php echo $row['email']&#63;>"/></td>
    </tr>
    <tr>
     <td align='right'>性别</td>
     <td>
      <input type="radio" name="sex" id="" value='1' <&#63;php if($row['sex']=='1'){ echo 'checked';}&#63;>/>男
      <input type="radio" name="sex" id="" value='2' <&#63;php if($row['sex']=='2'){ echo 'checked';}&#63;>/>女
      <input type="radio" name="sex" id="" value='3' <&#63;php if($row['sex']=='3'){ echo 'checked';}&#63;>/>保密
     </td>
    </tr>
    <tr>
     <td align='right'>个人简介</td>
     <td>
      <textarea name="txt" id="" cols="50" rows="10"><&#63;php echo $row['txt']&#63;></textarea>
     </td>
    </tr>
    <tr>
     <td colspan='2'><input type="submit" name='act' value='修改' /></td>
    </tr>
   </table>
  </form>
</div>
</body>
</html>

//将修改的信息存入数据库

<&#63;php
header("Content-type:text/html; charset=utf-8");
//通过post获取页面提交数据信息
$data = $_POST;
//print_r($data);
include_once "connect.php";
$sql = "update `user` set username='{$data['username']}',password='{$data['password']}', email='{$data['email']}',sex='{$data['sex']}',txt='{$data['txt']}' where id='{$data['id']}'";
echo $sql;
$res = mysql_query($sql,$link);
if($res){
 header("Location:hello.php");
 //echo "alert('修改成功')";
}else{
 header("Location:update1.php&#63;id=".$data['id']);
 //echo "alert('修改失败')";
}
&#63;>

6. Delete data

//删除数据库里的数据

<&#63;php
header("Content-type:text/html; charset=utf-8");
include_once 'connect.php';
$sql = "delete from user where id='".$_GET['id']."'";
$sus=mysql_query($sql,$link);
if($sus){
 header("location:hello.php");
}else{
 echo "alert('删除失败')";
}
&#63;>
//若要删除李四,点击删除后,会自动跳转到后台页面,数据库里数据也删除

The above is the entire content of this article, I hope it will be helpful to everyone’s study.

Articles you may be interested in:

  • PHP code to verify the existence of the username before registering on the page
  • Using Php to write example code for email activation verification after registration
  • Specific example of using js for form verification on the PHP user registration page
  • ThinkPHP complete example of user registration login message
  • PHP Ajax detection of whether the user name or email already exists when registering
  • 🎜>
  • Detailed explanation of the development of PHP jQuery registration module
  • Improvements of PHP jQuery registration module (1): Verification code is stored in SESSION
  • Improvements of PHP jQuery registration module (2): Email activation
  • php mysql method to implement user registration and login
  • php sends SMS verification code to complete the registration function

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/1114240.htmlTechArticlePHP connects to the database to implement the addition, deletion, modification and check operations of the registration page. The example of php addition and deletion in this article shares with everyone the implementation of PHP connection to the database. The method of adding, deleting, modifying and checking on the registration page is for your reference...
Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
php怎么把负数转为正整数php怎么把负数转为正整数Apr 19, 2022 pm 08:59 PM

php把负数转为正整数的方法:1、使用abs()函数将负数转为正数,使用intval()函数对正数取整,转为正整数,语法“intval(abs($number))”;2、利用“~”位运算符将负数取反加一,语法“~$number + 1”。

php怎么实现几秒后执行一个函数php怎么实现几秒后执行一个函数Apr 24, 2022 pm 01:12 PM

实现方法:1、使用“sleep(延迟秒数)”语句,可延迟执行函数若干秒;2、使用“time_nanosleep(延迟秒数,延迟纳秒数)”语句,可延迟执行函数若干秒和纳秒;3、使用“time_sleep_until(time()+7)”语句。

php怎么除以100保留两位小数php怎么除以100保留两位小数Apr 22, 2022 pm 06:23 PM

php除以100保留两位小数的方法:1、利用“/”运算符进行除法运算,语法“数值 / 100”;2、使用“number_format(除法结果, 2)”或“sprintf("%.2f",除法结果)”语句进行四舍五入的处理值,并保留两位小数。

php字符串有没有下标php字符串有没有下标Apr 24, 2022 am 11:49 AM

php字符串有下标。在PHP中,下标不仅可以应用于数组和对象,还可应用于字符串,利用字符串的下标和中括号“[]”可以访问指定索引位置的字符,并对该字符进行读写,语法“字符串名[下标值]”;字符串的下标值(索引值)只能是整数类型,起始值为0。

php怎么根据年月日判断是一年的第几天php怎么根据年月日判断是一年的第几天Apr 22, 2022 pm 05:02 PM

判断方法:1、使用“strtotime("年-月-日")”语句将给定的年月日转换为时间戳格式;2、用“date("z",时间戳)+1”语句计算指定时间戳是一年的第几天。date()返回的天数是从0开始计算的,因此真实天数需要在此基础上加1。

php怎么读取字符串后几个字符php怎么读取字符串后几个字符Apr 22, 2022 pm 08:31 PM

在php中,可以使用substr()函数来读取字符串后几个字符,只需要将该函数的第二个参数设置为负值,第三个参数省略即可;语法为“substr(字符串,-n)”,表示读取从字符串结尾处向前数第n个字符开始,直到字符串结尾的全部字符。

php怎么替换nbsp空格符php怎么替换nbsp空格符Apr 24, 2022 pm 02:55 PM

方法:1、用“str_replace("&nbsp;","其他字符",$str)”语句,可将nbsp符替换为其他字符;2、用“preg_replace("/(\s|\&nbsp\;||\xc2\xa0)/","其他字符",$str)”语句。

php怎么查找字符串是第几位php怎么查找字符串是第几位Apr 22, 2022 pm 06:48 PM

查找方法:1、用strpos(),语法“strpos("字符串值","查找子串")+1”;2、用stripos(),语法“strpos("字符串值","查找子串")+1”。因为字符串是从0开始计数的,因此两个函数获取的位置需要进行加1处理。

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Tools

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

Safe Exam Browser

Safe Exam Browser

Safe Exam Browser is a secure browser environment for taking online exams securely. This software turns any computer into a secure workstation. It controls access to any utility and prevents students from using unauthorized resources.