php之mvc框架演进过程详解
1) /********** php与html混编完成增删改查功能**************************/
1.设计思路:
根据平时练习一个增删改查的功能进行,即在一个php文件中完成,对数据库的连接操作
及在php文件中展示html代码。html提交到当前页面的php部分进行处理入库动作。
$_SERVER['SCRIPT_FILENAME']包含当前脚本的路径。
这在页面需要指向自己时非常有用。
区别于__FILE__常量包含当前脚本(例如包含文件)的完整路径和文件名。
第一个类:增加商品文件:addProduct.php
<?php
if(isset($_POST['submit'])){
$name= $_POST['name'];
$price= $_POST['price'];
$description= $_POST['description'];
//连接数据库操作
mysql_connect('localhost','root','master');
mysql_select_db('test');
mysql_query('set names gbk');
$query= "insert into product values(null,'$name','$price','$description')";
mysql_query($query);
mysql_close();
}
?>
<formmethod="post"action='<?php $_SERVER['SCRIPT_FILENAME']?>'>
产品名称<inputtype="text"name="name"/><br>
产品价格<inputtype="text"name="price"/><br>
产品描述<inputtype="text"name="description"/><br>
<inputtype="submit"name='submit'value="提交"/>
</form>
思路:都是在同一文件(当前文件中)操作数据库和展示:
Mysql_fetch_assoc 返回数组,一般需要放到一个空数组内组成一个二维数组返回到页面 mysql_num_rows返回行数
查询数据一般常用:
Php中
$result = Mysql_query(“select * fromproduct order by id desc”);
$data = array();
While ($row = Mysql_fecth_assoc($result)){
$data[]= $row;
}
Html中:
<?php foreach($data as $row) : ?>
<tr>
<td><?php echo $row[‘id’];?></td>
……
</tr>
<?php endforeach; ?>
查询商品文件: listProduct.php
<?php
//连接数据库操作
mysql_connect('localhost','root','master');
mysql_select_db('test');
mysql_query('set names gbk');
$query = "select * from product order by id desc";
$result = mysql_query($query);
$data = array();
while($row = mysql_fetch_assoc($result)){
$data[]= $row;
}
?>
<tableborder=1>
<tr>
<td>编号</td>
<td>名称</td>
<td>价格</td>
<td>描述</td>
<td>删除</td>
<td>更新</td>
</tr>
<?php
foreach($dataas$row):
?>
<tr>
<td><?phpecho$row['id'];?></td>
<td><?phpecho$row['name'];?></td>
<td><?phpecho$row['price'];?></td>
<td><?phpecho$row['description'];?></td>
<td><ahref="delProduct.php?id=<?phpecho$row['id'];?>">删除</a></td>
<td><ahref="updateProduct.php?id=<?phpecho$row['id'];?>">更新</a></td>
</tr>
<?phpendforeach;?>
</table>
思路:接下来就进行删除和更新操作,同样是请求到新的php一个文件中来处理删除和更新操作。新建php文件:delProduct.php
delProduct.php:
<?php
//连接数据库操作
mysql_connect('localhost','root','master');
mysql_select_db('test');
mysql_query('set names gbk');
$id = $_GET['id'];
$query = "delete from product where id = '$id'";
mysql_query($query);
mysql_close();
//同时跳到listProduct展示结果
header('location:listProduct.php');
更新操作:
updateProduct.php(以下类只是作为展示,提交修改还需要一个php文件,为了不再增加一个文件,修改提到本页面,在action中增加一个参数区别展示和提交修改的操作)
<?php
//连接数据库操作
mysql_connect('localhost','root','master');
mysql_select_db('test');
mysql_query('set names gbk');
$id = $_GET['id'];
$query = "select * from product where id = '$id'";
$result = mysql_query($query);
$row = mysql_fetch_row($result);
?>
<formmethod="post"action='updateProduct.php'>
<inputtype="hidden"value="<?phpecho$row[0];?>"/>
产品名称<inputtype="text"name="name"value="<?phpecho$row[1];?>"/><br>
产品价格<inputtype="text"name="price"value="<?phpecho$row[2];?>"/><br>
产品描述<inputtype="text"name="description"value="<?phpecho$row[3];?>"/><br>
<inputtype="submit"name='submit'value="修改"/>
</form>
修后类内容如下:
<?php
//连接数据库操作
mysql_connect('localhost','root','master');
mysql_select_db('test');
mysql_query('set names gbk');
if(isset($_REQUEST['flag'])){
$id= $_POST['id'];
$name= $_POST['name'];
$price= $_POST['price'];
$description= $_POST['description'];
$query= "update product set name = '$name', price = '$price' , description = '$description' where id='$id'";
mysql_query($query);
mysql_close();
header('location:l
补充:Web开发 , php ,