PHP 学习笔记
尼玛,我怎么什么都搞阿。
使用Apache服务器
下载的是:wampserver2.2d_x32_zol
编辑器:可以根据个人选择,NOTEPAD++,EDITPLUS,ZendStudio
放上代码跟着代码学习
一.添加功能
1.这是一个简单的HTML页面
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> </head> <body> <form action="doinsert.php" method="POST"> <p> ID:<br/> <input type="text" name="id" size="6" maxlength="6" /> </p> <p> 给自己起一个firstname:<br/> <input type="text" name="firstname" size="10" maxlength="10" /> </p> <p> 给自己设一个lastname:<br/> <input type="text" name="lastname" size="10" maxlength="10" value=""/> </p> <p> 你的电话:<br/> <input type="text" name="phone" size="20" maxlength="20" value=""/> </p> <p> 你的email地址:<br/> <input type="text" name="email" size="20" maxlength="20" value=""/> </p> <p> <input type="submit" name="submit" value="Submit!"/> </p> </form> </body> </html>
2.这是一个添加数据的方法页面
<?php $linkID=@mysql_connect("localhost","root","123");//连接数据库 if(!$linkID){ echo "没连接上"; } $ss=@mysql_select_db("testphp"); //选择数据库 if(!$ss){ echo "没找到数据库"; } $id = $_POST['id']; //字段名称 $firstname = $_POST['firstname']; $lastname = $_POST['lastname']; $email = $_POST['email']; $phone = $_POST['phone']; //添加语句,写法一 $query = "INSERT INTO users (id,firstname,lastname,phone,email) values( '$id','$firstname','$lastname','$phone','$email')"; //写法二。 //$query = "insert into member3 set // id='$id',name='$name',password='$password',email='$email'"; $result = mysql_query($query); //执行命令并返回结果 //判断结果 if($result) echo "<p>member successfuly insert!</p>"; else echo "<p>There was a problem inserting the member!</p>"; //关闭数据库 mysql_close(); include "insert.php" //返回并继续插入 ?>
3.数据库设置
# MySQL-Front 5.1 (Build 2.7) /*!40101 SET @OLD_SQL_MODE=@@SQL_MODE */; /*!40101 SET SQL_MODE='STRICT_TRANS_TABLES,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION' */; /*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES */; /*!40103 SET SQL_NOTES='ON' */; /*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS */; /*!40014 SET UNIQUE_CHECKS=0 */; /*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS */; /*!40014 SET FOREIGN_KEY_CHECKS=0 */; # Host: localhost Database: testphp # ------------------------------------------------------ # Server version 5.5.20 # # Source for table users # DROP TABLE IF EXISTS `users`; CREATE TABLE `users` ( `id` int(11) NOT NULL AUTO_INCREMENT, `firstname` varchar(50) DEFAULT NULL, `lastname` varchar(50) DEFAULT NULL, `phone` varchar(200) DEFAULT NULL, `email` varchar(200) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=1235 DEFAULT CHARSET=utf8; # # Dumping data for table users # LOCK TABLES `users` WRITE; /*!40000 ALTER TABLE `users` DISABLE KEYS */; INSERT INTO `users` VALUES (1,'fname11','lname1','(000)000-0000','name9@gmail.com'); INSERT INTO `users` VALUES (2,'fname12','lname2','(000)000-0000','name9@gmail.com'); INSERT INTO `users` VALUES (3,'fname1','lname1','(000)000-0000','name1@gmail.com'); INSERT INTO `users` VALUES (4,'fname2','lname2','(000)000-0000','name2@gmail.com'); INSERT INTO `users` VALUES (5,'fname3','lname3','(000)000-0000','name3@gmail.com'); INSERT INTO `users` VALUES (7,'fname4','lname4','(000)000-0000','name4@gmail.com'); INSERT INTO `users` VALUES (8,'fname5','lname5','(000)000-0000','name5@gmail.com'); INSERT INTO `users` VALUES (9,'fname6','lname6','(000)000-0000','name6@gmail.com'); INSERT INTO `users` VALUES (10,'fname7','lname7','(000)000-0000','name7@gmail.com'); INSERT INTO `users` VALUES (11,'fname8','lname8','(000)000-0000','name8@gmail.com'); INSERT INTO `users` VALUES (12,'fname9','lname9','(000)000-0000','name9@gmail.com'); INSERT INTO `users` VALUES (13,'fname13','lname13','(000)000-0000','name9@gmail.com'); INSERT INTO `users` VALUES (14,'sadsa','2323','12312','21@11.com'); INSERT INTO `users` VALUES (1234,'asd','sgds','12421','z@11.com'); /*!40000 ALTER TABLE `users` ENABLE KEYS */; UNLOCK TABLES; /*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; /*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; /*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; /*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
@mysql_connect("连接地址","用户名","密码");//数据库连接
@mysql_select_db("数据库"); //选择数据库
$_POST['参数名']; //获取页面发送请求所带的参数
mysql_query("SQL语句"); //执行SQL语句
二.查询,更新功能
1.查询页面,选择ID
<html> <head> </head> <body> <form action="modify.php" method="post"> <select name="rowID"> <option value="">choose a ID:</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> </select> <input type="submit" name = "submit" value="Submit!"/> </form> </body> </html>
2.根据ID去数据库查询数据
<?php $linkID=@mysql_connect("localhost","root","123"); $ss=@mysql_select_db("testphp"); $rowID = $_POST['rowID']; $query="select id,firstname,lastname,phone,email from users where id='$rowID'"; $result=mysql_query($query); if(!$result){ echo "没执行成功"; } //echo "$result"; list($id,$firstname,$lastname,$phone,$email)=mysql_fetch_row($result); //将查询返回的值映射到变量里??? //echo "$id"; //echo "$name"; //echo "$password"; //echo "$email"; mysql_close(); include "modifyform.php"; ?>
3.跳到修改页面,将查询到的数据填入表单中
<html> <head> </head> <body> <form action="update.php" method="POST"> <input type="hidden" name="rowID" value="<?php echo $rowID;?>"> <p> ID:<br/> <input type="text" name="id" size="6" maxlength="6" value="<?php echo $id; ?>" /> </p> <p> 你的第一名称:<br/> <input type="text" name="firstname" size="10" maxlength="10" value="<?php echo $firstname; ?>" /> </p> <p> 你的第二名称:<br/> <input type="text" name="lastname" size="8" maxlength="8" value="<?php echo $lastname; ?>" /> </p> <p> 你的电话:<br/> <input type="text" name="phone" size="8" maxlength="8" value="<?php echo $phone; ?>" /> </p> <p> 你的邮箱:<br/> <input type="text" name="email" size="20" maxlength="20" value="<?php echo $email; ?>" /> </p> <p> <input type="submit" name="submit" value="Submit!"/> </p> </form> </body> </html>
4.获取表单数据进行更新数据
<?php $linkID=@mysql_connect("localhost","root","123"); $db=@mysql_select_db("testphp"); $rowID=$_POST['rowID']; $id=$_POST['id']; $firstname=$_POST['firstname']; $lastname=$_POST['lastname']; $phone = $_POST['phone']; $email=$_POST['email']; //echo $rowID; $query=" UPDATE users SET id='$id',firstname='$firstname',lastname='$lastname',phone='$phone',email='$email'where id='$rowID' "; //echo $query; $result=mysql_query($query); if($result) echo "<p>member successfuly update!</p>"; else echo "<p>There was a problem updating the member!</p>"; mysql_close(); ?>
//mysql_fetch_row() 从和结果标识 data 关联的结果集中取得一行数据并作为数组返回。每个结果的列储存在一个数组的单元中,偏移量从 0 开始。
//也就是说返回一个数据集合
//开始第一个数组,将数组里下标为0,1,2,3,4的依次放入$id,$firstname,$lastname,$phone,$email里
mysql_fetch_row($result);
list($id,$firstname,$lastname,$phone,$email)=mysql_fetch_row($result);
三.删除
1.根据复选框删除多个
<html> <head> </head> <body> <form action="delete.php" method="post"> <input type="checkbox" name="kk[]" value="1" />1 zcl<br/> <input type="checkbox" name="kk[]" value="2" />2 lv<br/> <input type="checkbox" name="kk[]" value="3" />3 ed<br/> <input type="checkbox" name="kk[]" value="4" />4 d<br/> <input type="submit" name = "submit" value="Submit!"/> </form> </body> </html>
2.根据下拉多选删除多个
<html> <head> </head> <body> <form action="delete.php" method="post"> <select name="kk[]" multiple="multiple"> <option value="">choose a ID:</option> <option value="1">1</option> <option value="2">2</option> <option value="3">3</option> </select> <input type="submit" name = "submit" value="Submit!"/> </form> </body> </html>
3.删除操作
<?php $linkID=@mysql_connect("localhost","root","123"); $db=@mysql_select_db("testphp"); //echo $_POST['kk']; //echo array('1','2','3'); foreach($_POST['kk'] as $pp)//遍历数组,给每个赋值 { echo $pp; $rowID=$pp; $query="delete from users where id='$rowID'";//查询语句 $result=mysql_query($query);//放到数据库执行 if(!mysql_affected_rows()==0) echo "<p>选择的项".$rowID."已经成功的被删除!</p>" ; if(mysql_affected_rows()==0){ echo "<p>没有找到记录!</p>" ; } } ?>
mysql_affected_rows(); //取得前一次 MySQL 操作所影响的记录行数。
PHP MySql函数查询:http://www.w3school.com.cn/php/php_ref_mysql.asp
感谢CSDN:yuzhengyang0720 分享学习心得