当前位置:首页 » 编程语言 » php增删改查

php增删改查

发布时间: 2022-01-11 23:01:59

‘壹’ php封装一个类能实现mysql数据库的增删改查

楼主整理代码会好一点

‘贰’ php封装一个class类,实现mysql数据库的增删改查怎么操做

class
sqlHelper{
public
$conn;
public
$dbname="数据库名称";
public
$username="数据库用户名";
public
$password="数据库密码";
public
$host="localhost";
//连接数据库
public
function
__construct(){
$this->conn=mysql_connect($this->host,$this->username,$this->password);
if(!$this->conn){
die("连接失败".mysql_error());
}
mysql_select_db($this->dbname,$this->conn);
}
//执行查询语句
public
function
execute_dql($sql){
$res=mysql_query($sql,$this->conn);
return
$res;
}
//执行增填改语句
public
function
execute_dml($sql){
$b=mysql_query($sql,$this->conn);
if(!$b){
return
3;
}else{
if(mysql_affected_rows($this->conn)){
return
1;//表示OK
}else{
return
2;//表示没有行收到影响
}
}
}
}

‘叁’ 如何用PHP代码实现MySQL数据库的增删改查

<?php
$con = mysql_connect("localhost:3306","root","");

if (!$con) {
die('Could not connect: ' . mysql_error());
}

mysql_select_db("test", $con);

$result = mysql_query("SELECT * FROM user");

echo "<table border='1'>
<tr>
<th>Username</th>
<th>Password</th>
</tr>";

while($row = mysql_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['username'] . "</td>";
echo "<td>" . $row['password'] . "</td>";
echo "</tr>";
}
echo "</table>";

mysql_close($con);

?>

从服务器中获取用户所有信息(SQL SELECT语句)并以表格形式出现
<?php
$con = mysql_connect("localhost","root","");
if (!$con) {
die('Could not connect: ' . mysql_error());
}

mysql_select_db("test", $con);

mysql_query("DELETE FROM user WHERE username = '$_POST[username]'");

mysql_close($con);
?>

删除该用户所有信息delete.php
<?php
$con = mysql_connect("localhost:3306","root","");

if (!$con) {
die('Could not connect: ' . mysql_error());
}

mysql_select_db("test", $con);

$sql = "INSERT INTO user (username,password)
VALUES
('$_POST[username]','$_POST[password]')";

if (!mysql_query($sql,$con)) {
die('Error: ' . mysql_error());
}

echo "1 record added";

mysql_close($con);

?>

注册一个新用户insert.php
<?php

$con = mysql_connect("localhost","root","");
if (!$con) {
die('Could not connect: ' . mysql_error());
}

mysql_select_db("test", $con);

mysql_query("UPDATE user SET password = '$_POST[password]' WHERE username = '$_POST[username]'");

mysql_close($con);
?>

修改一个用户密码update.php
<html>
<head>
<title>FORM</title>
</head>
<body>
<br />
<h1>Insert:</h1>
<form action="insert.php" method="post">
username:<input type="name" name="username"/>
<br />
password:<input type="password" name="password"/>
<input type="submit" value="submit"/>
</form>
<br /><hr /><br />
<h1>Delete</h1>
<form action="delete.php" method="post">
username:<input type="name" name="username" />
<br />
Are you sure?<input type="submit" value="sure" />
</form>
<br /><hr /><br />
<h1>Update</h1>
<form action="update.php" method="post">
username:<input type="name" name="username"/>
<br />
You want to change your password into:<input type="password" name="password"/>
<input type="submit" value="submit"/>
</form>
<br /><hr /><br />
</body>
</html>

以上三个功能的提交源Operate.html

‘肆’ 需要一个php的前端ajax增删改查接口

ajax使用很简单,他属于异步传输。也就是你将以from以post或者get形式提交,换成ajax形式了。
取消from,使用ajax内的get或者post方法将当前页的所需数据传递到另一个执行页面。
jquery不错的框架,搜索下ajax就明白了。

‘伍’ php增删改查的添加怎么添加性别代码

方法/步骤

链接数据库方法:conn.php 。
代码如下:
<?php
$link=mysql_connect('localhost','root','tianqing')or die('数据库连接错误');
mysql_select_db('studyexam',$link)or die('数据库访问错误');
mysql_query("set names 'utf8'");
?>

增加 add.php
代码如下:
<?php
include("conn.php");//引入链接数据库
if(!empty($_POST['tj'])){
$title=$_POST['title'];
$con=$_POST['con'];
echo $sql="insert into news(id,title,dates,contents) value (null,'$title',now(),'$con')" ;
mysql_query($sql);
echo"插入成功";
}
?>
<form action="add.php" method="post">
标题: <input type="text" name="title"><br /><br />
内容: <textarea rows="6" cols="55" name="con"></textarea><hr>
<input type="submit" name="tj" value="提交">
</form>

删除del.php
代码如下:
<?php
include("conn.php");//引入链接数据库<pre name="code" class="html"><?php
include("conn.php");//引入链接数据库
if(!empty ($_GET['id'])){
$sql="select * from news where id='".$_GET['id']."'";
$query=mysql_query($sql);
$rs=mysql_fetch_array($query);
}
if(!empty($_POST['sub'])){
$title=$_POST['title'];
$con=$_POST['con'];
$hid=$_POST['hid'];
$sql="update news set title='$title',contents='$con' where id='$hid' limit 1 ";
mysql_query($sql);
echo "<script> alert('更新成功'); location.href='index.php'</script>";
echo"更新成功";
}
?>
<form action="edit.php" method="post">
<input type="hidden" name="hid" value="<?php echo $rs['id']?>"/>
标题: <input type="text" name="title" value="<?php echo $rs['title']?>"><br>
内容: <textarea rows="5" cols="50" name="con"><?php echo $rs['contents']?></textarea><br>
<input type="submit" name="sub" value="发表">
</form></pre><br>
<?php
if(!empty($_GET['del'])){ $d=$_GET['del']; $sql="delete from news where id ='$d'"; } $query=mysql_query($sql); echo "删除成功"; ?><p></p>
<pre></pre>
<br>

改 edit.php页面
代码如下:
<?php
include("conn.php");//引入链接数据库
if(!empty ($_GET['id'])){
$sql="select * from news where id='".$_GET['id']."'";
$query=mysql_query($sql);
$rs=mysql_fetch_array($query);
}
if(!empty($_POST['sub'])){
$title=$_POST['title'];
$con=$_POST['con'];
$hid=$_POST['hid'];
$sql="update news set title='$title',contents='$con' where id='$hid' limit 1 ";
mysql_query($sql);
echo "<script> alert('更新成功'); location.href='index.php'</script>";
echo"更新成功";
}
?>
<form action="edit.php" method="post">
<input type="hidden" name="hid" value="<?php echo $rs['id']?>"/>
标题: <input type="text" name="title" value="<?php echo $rs['title']?>"><br>
内容: <textarea rows="5" cols="50" name="con"><?php echo $rs['contents']?></textarea><br>
<input type="submit" name="sub" value="发表">
</form></pre><br>

查询select.php页面
代码如下:
<?php
include("conn.php");
$sql="select * from entries";//entries这是数据库名词,大家自己改
if($result=mysql_query($sql)){
/*
$num=mysql_num_rows($result)
for($i=1;$i<=$num;$i++);
*/
while($array=mysql_fetch_assoc($result)){
//$biaoti=$array['title'];
$neirong=$array['entry'];
$id=$array['entry_id'];
echo "<p>{$array['title']}</p><p>$neirong</p><p><a href='edit.php?id=$id'>编辑</a>
<a href='delete.php?id=$id'>删除</a></p><hr />";
}
}
?>

‘陆’ PHP如何把前端用户的增删改查操做记录写进数据库表

做个日志记录表 每次增删改查后 更新表 如

//记录
$sql = "insert into `h_log_point2` set ";
$sql .= "h_userName = '" . $rss['h_buyUserName'] . "', ";
$sql .= "h_price = '" . $num . "', ";
$sql .= "h_type = '交易完成', ";
$sql .= "h_about = '交易ID:" . $id . "', ";
$sql .= "h_addTime = '" . date('Y-m-d H:i:s') . "', ";
$sql .= "h_actIP = '" . getUserIP() . "' ";
$db->query($sql);

echo '修改成功';

‘柒’ PHP怎么对多维数据进行增删改查

你可以嵌套遍历 在第二层遍历的时候做一下数组的建名 当等于你的目标数组建名再做下一层的遍历 为了代码的高效 你也可以对该建名的数组判断是否为空 如果为空就跳到下一次循环

‘捌’ php中一个html页面实现增删改查

增加:insert into 表名(字段1,字段2,...) values('值1','值2',....) where 条件;
删除:delete 表名
修改:update 表名 set 字段名='值' where 条件;
查询:select 字段名 from 表名 where 条件;

‘玖’ PHP如何实现增删改查,具体应该看什么课程

连接数据啊,通过php语句来操作数据库,从而实现增删查改
主要要看mysql的课程,以及php入门的课程,之后主攻php操作数据库这一块

‘拾’ 一个php文件怎么写上增删改查 功能

PHP个人博客开发,发邮件回复原程序2252065614

热点内容
学考密码重置要求的证件是什么 发布:2024-09-20 07:19:46 浏览:477
电脑主服务器怎么开机 发布:2024-09-20 07:19:07 浏览:728
2022款瑞虎升级哪些配置 发布:2024-09-20 06:59:07 浏览:264
数据库与asp 发布:2024-09-20 06:55:25 浏览:727
python解释编译 发布:2024-09-20 06:52:57 浏览:648
舞蹈丰收脚本 发布:2024-09-20 06:36:26 浏览:595
linux进程端口号 发布:2024-09-20 06:36:11 浏览:79
派派怎么改密码忘了 发布:2024-09-20 06:25:49 浏览:780
linux虚拟地址物理地址 发布:2024-09-20 06:23:29 浏览:564
大华监控云存储 发布:2024-09-20 06:13:24 浏览:597