大橙子网站建设,新征程启航
为企业提供网站建设、域名注册、服务器等服务
举例如下:
创新互联2013年至今,先为大邑县等服务建站,大邑县等地企业,进行企业商务咨询服务。为大邑县企业网站制作PC+手机+微官网三网同步一站式服务解决您的所有建站问题。
创建userinfo_update.php页面用于查询用户信息,先显示信息,在修改:
先通过GET获取用户编号查询用户信息:
$sql = "select * from user_info where user_id='".$_GET['userId']."'";
$result = mysql_query($sql,$con);
if($row = mysql_fetch_array($result)){
}
页面效果:
创建update.php文件,用于修改用户信息:
使用到了mysql_affected_rows() 函数返回前一次 MySQL 操作所影响的记录行数。
//通过post获取页面提交数据信息
$userId = $_POST[userId];
$userName = $_POST[userName];
$userAge = $_POST[userAge];
$sql = "update user_info set user_name='".$userName."',user_age=".$userAge." where user_id='".$userId."'";
mysql_query($sql,$conn);//执行SQL
$mark = mysql_affected_rows();//返回影响行数
$url = "userinf_select.php";
运行结果
创建delete.php文件,完成删除用户信息功能:
$userId = $_GET['userId'];
include 'connection.php';
$sql = "delete from user_info where user_id='".$userId."'";
mysql_query($sql,$con);
$mark = mysql_affected_rows();//返回影响行数
if($mark0){
echo "删除成功";
}else{
echo "删除失败";
}
mysql_close($con);
运行结果:
//第一种
?php
$conn = mysql_connect("主机名","用户名","密码");
mysql_select_db("数据库名",$conn);
$sql = "select * from user";
$result = mysql_query($sql);
$data="";
while($row = mysql_fetch_row($result)){
foreach($row as $v){
$data .=$v."|"; //加个树线隔开字段
}
nl2br($data); //读完一行回车
}
file_put_contents("abc.txt",$data);
//第二种
?php
$filename = "abc.txt";
$res = fopen($filename,"r");
while(!feof($res)){
$buffer = fgets($res);
$buffer = str_replace(' ', '|', $buffer);
$array = explode('|', $buffer);
foreach($array as $v){
if(strlen($v)0){
$arr[] = $v;
}
}
}
fclose($res);
//组装数组插入数据库就可以了
$rs = mysql_select_db($dbname,$conn);
if(!$rs){
$rs = mysql_query("CREATE DATABASE `$dbname`; ",$conn);
if(!$rs){
$errstr = GetBackAlert("数据库 {$dbname} 不存在,也没权限创建新的数据库!");
echo $errstr;
exit();
}else{
$rs = mysql_select_db($dbname,$conn);
if(!$rs){
$errstr = GetBackAlert("你对数据库 {$dbname} 没权限!");
echo $errstr;
exit();
}else{
$ctStr = 'CREATE TABLE `wish` (
`id` int(11) NOT NULL auto_increment,
`pname` varchar(30) default NULL,
`pqq` varchar(10) default NULL,
`plocal` varchar(50) default NULL,
`pemail` varchar(30) default NULL,
`context` tinytext,
`addDate` datetime default NULL,
`biz1` varchar(250) default NULL,
`biz2` varchar(250) default NULL,
`biz3` varchar(250) default NULL,
`biz4` varchar(250) default NULL,
`biz5` varchar(250) default NULL,
`biz6` int(11) default NULL,
`biz7` int(11) default NULL,
`biz8` int(11) default NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=gbk;';
mysql_query($ctStr,$conn);
}
}
数据库有很多种类:mysql,oracle,mssql,db2等等。PHP操作数据库的时候,要保证该类型数据库的扩展已开启。这里连接的数据库以mysql为例:
?php
//数据库服务器地址
$host="localhost";
//连接数据库用户名
$uname="root";
//连接数据库密码
$upass="";
//连接数据库
$conn=mysql_connect($host, $uname,$upass);
//判断连接
if(!$conn){
die("连接数据库失败!").mysql_errno();
}
//连接成功,其他操作省略
?
!--表单文件,拷入index.php--
!DOCTYPE html
html
head
style
label{display:inline-block;width:100px;margin-bottom:10px;}
/style
titleAdd students/title
/head
body
!-- 数据库用mysqli 面向过程调用方法--
form method="post" action="write2db.php"
!--数据库用mysqli 面向过程调用方法
form method="post" action="write2db_sqlio.php"
--
!--数据库用PDO调用方法
form method="post" action="write2db_pdo.php"
--
labelFirst Name/label
input type="text" name="first_name" /
br /
labelLast Name/label
input type="text" name="last_name" /
br /
labeldepartment/label
input type="text" name="department" /
br /
labelEmail/label
input type="text" name="email" /
br /
input type="submit" value="Add students"
/form
/body
/html
------------------------------
?php
//拷贝命名为write2db.php,数据库用mysqli 面向过程调用方法
//print_r($_POST);
// create a variable
$first_name=$_POST['first_name'];
$last_name=$_POST['last_name'];
$department=$_POST['department'];
$email=$_POST['email'];
//调试用
echo "Your input: ";
echo $first_name;
echo 'br /';
echo $last_name;
echo 'br /';
echo $department;
echo 'br /';
echo $email;
echo 'br /';
$servername = "localhost";
//Your database username and password
//$username = "username";
//$password = "password";
$username = "tester";
$password = "testerPassword";
//your database name
$dbname = "test";
$tablename ="student";
// Create connection
$connect = mysqli_connect($servername, $username, $password, $dbname);
if (!$connect) {
die("Connection failed: " . mysqli_connect_error());
}
//Execute the query
$sql="INSERT INTO $tablename (first_name,last_name,department,email)
VALUES('$first_name','$last_name','$department','$email')";
if (mysqli_query($connect, $sql)) {
echo "Hooray! New record is inserted to database successfully. Please check database.";
} else {
echo "Error: " . $sql . "br /" . mysqli_error($connect);
}
mysqli_close($connect);
?
?php
//拷贝命名为write2db_sqlio.php,数据库用mysqli 面向对象调用方法
//print_r($_POST);
// create a variable
$first_name=$_POST['first_name'];
$last_name=$_POST['last_name'];
$department=$_POST['department'];
$email=$_POST['email'];
//调试用
echo "Your input: ";
echo $first_name;
echo 'br /';
echo $last_name;
echo 'br /';
echo $department;
echo 'br /';
echo $email;
echo 'br /';
$servername = "localhost";
//Your database username and password
//$username = "username";
//$password = "password";
$username = "tester";
$password = "testerPassword";
//database name
$dbname = "test";
$tablename ="student";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn-connect_error) {
die("Connection failed: " . $conn-connect_error);
}
$sql="INSERT INTO $tablename (first_name,last_name,department,email)
VALUES('$first_name','$last_name','$department','$email')";
if ($conn-query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "br" . $conn-error;
}
$conn-close();
?
?php
//拷贝为文件write2db_pdo.php,数据库用PDO调用方法
//print_r($_POST);
a variable
$first_name=$_POST['first_name'];
$last_name=$_POST['last_name'];
$department=$_POST['department'];
$email=$_POST['email'];
//调试用
echo "Your input: ";
echo $first_name;
echo 'br /';
echo $last_name;
echo 'br /';
echo $department;
echo 'br /';
echo $email;
echo 'br /';
$servername = "localhost";
//Your database username and password
//$username = "username";
//$password = "password";
$username = "tester";
$password = "testerPassword";
//your database name
$dbname = "test";
$tablename ="student";
// Create connection
try {
$conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
// set the PDO error mode to exception
$conn-setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$sql="INSERT INTO $tablename (first_name,last_name,department,email)
VALUES('$first_name','$last_name','$department','$email')";
// use exec()
$conn-exec($sql);
echo "New record created successfully";
}
catch(PDOException $e)
{
echo $sql . "br" . $e-getMessage();
}
$conn = null;
?
--创建数据库test, 将此文件存为test.sql 导入数据库,或者手动创建表结构
-- phpMyAdmin SQL Dump
-- version 4.7.4
--
--
-- Host: 127.0.0.1:3306
-- Generation Time: Mar 12, 2018 at 04:04 AM
-- Server version: 5.7.19
-- PHP Version: 7.1.9
SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO";
SET AUTOCOMMIT = 0;
START TRANSACTION;
SET time_zone = "+00:00";
/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8mb4 */;
--
-- Database: `test`
--
-- --------------------------------------------------------
--
-- Table structure for table `student`
--
DROP TABLE IF EXISTS `student`;
CREATE TABLE IF NOT EXISTS `student` (
`id` tinyint(3) UNSIGNED NOT NULL AUTO_INCREMENT,
`first_name` varchar(20) NOT NULL,
`last_name` varchar(20) NOT NULL,
`department` varchar(50) NOT NULL,
`email` varchar(50) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8;
--
-- Dumping data for table `student`
--
INSERT INTO `student` (`id`, `first_name`, `last_name`, `department`, `email`) VALUES
(1, 'first1', 'last1', 'cs', '1985@qq.com');
COMMIT;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
因为你少写了一个大括号的结尾。
修改后:
table id="DataList1" cellspacing="0" border="0" width="100%"
?php
$db = mysqli_connect('localhost', 'root','root');//连接MySQL服务器
mysqli_select_db($db,'ticket');//选择数据库文件
if(mysqli_connect_errno()){
echo "Error:Could not connect to mysql ticket"; exit;
}
$q="select * from news";//设置查询指令
$result=mysql_query($db,$q);//执行查询
while($row=mysql_fetch_assoc($result)) //将result结果集中查询结果取出一条
{
echo"trtd".$row["news"]."/tdtd".$row["date"]."/td/tr"; exit;
}
} // 就是这样?
?
/table
谢谢谢谢谢谢码密。的入输要需所面。界理管台后器。由路陆登是。码密理管。廖仲庭。器由。路。