bp6am,bp6ampass
show.php
<?php
//由try.html跳转的
if(isset($_POST['user'])){
//查询是否选中了某个checkbox
if(array_key_exists('table1',$_POST))
$table1 = $_POST['table1'];
else
$table1 = false;
if(array_key_exists('table2',$_POST))
$table2 = $_POST['table2'];
else
$table2 = false;
//echo $_POST['user'];
//echo $_POST['pw'];
//连接数据库
//$name = 'bp6am';
//$password = 'bp6ampass';
$name = $_POST['user'];
$host = 'localhost';
$password = $_POST['pw'];
}else{
//由add.php跳转的
$name = $_COOKIE['name'];
$host = 'localhost';
$password = $_COOKIE['pw'];
$table1 = false;
$table2 = $_COOKIE['table2'];
}
$db = mysql_connect($host,$name,$password) or die('unable connect');
$query = 'use meinv';
mysql_query($query,$db) or die(mysql_error($db));
//如果2个都没选
if($table1 == false && $table2 == false)
echo "no table selected";
//如果只选中其中一个
if($table1 != false && $table2 == false){
$query = 'select * from '.$table1;
$result = mysql_query($query,$db) or die(mysql_error($db));
//建立表格头部
echo <<<End
<table border="1">
<tr>
<th>id</th>
<th>name</th>
<th>age</th>
</tr>
End;
while($row = mysql_fetch_assoc($result)){
echo "<tr>";
foreach($row as $value){
echo "<td>".$value."</td>";
}
echo "</tr>";
}
}
echo "</table>";
if($table1 == false && $table2 != false){
$query = 'select * from '.$table2;
$result = mysql_query($query,$db) or die(mysql_error($db));
//建立表格头部
echo <<<End
<table id="table" border="1">
<tr>
<th>id</th>
<th>name</th>
<th>moviename</th>
<th></th>
<th></th>
</tr>
End;
while($row = mysql_fetch_assoc($result)){
echo "<tr>";
foreach($row as $value){
echo "<td>".$value."</td>";
}
//编辑项
echo <<<end
<td><button type="button">编辑</button></td>
<td><button type="button">删除</button></td>
end;
echo "</tr>";
}
}
echo "</table>";
echo "<button id='insert' type='button'>插入</button>";
if($table1 != false && $table2 != false){
$query = "select $table1.name,$table1.age,$table2.moviename
from $table1 left join $table2 on $table1.name = $table2.name";
$result = mysql_query($query,$db) or die(mysql_error($db));
//建立表格头部
echo <<<End
<table border="1">
<tr>
<th>name</th>
<th>age</th>
<th>moviename</th>
</tr>
End;
while($row = mysql_fetch_assoc($result)){
echo "<tr>";
foreach($row as $value){
echo "<td>".$value."</td>";
}
echo "</tr>";
}
}
echo "</table>";
//返回按钮
echo <<< eod
<a href="./try.html">返回</a>
eod;
?>
<script src="./scripts/jquery2.min.js"></script>
<script>
//在#table上监事事件
$("#table").bind("click",function(event){
//获取数据库中的ID
//alert($(event.target).parent().parent().children(":first").html());
var id = $(event.target).parent().parent().children(":first").html();
if($(event.target).html() == "删除"){
//删除 delete.php
var url = './delete.php?id=' + id;
location.href = url;
}
if($(event.target).html() == "编辑"){
//编辑 edit.php
//被选栏变色
$(event.target).parent().parent().css("background","yellow");
//增加表单
var html = "<form action='edit.php?id="+id+"' method='post'>name:<input type='text' name='name'/></br>moviename:<input type='text' name='moviename'/><br/><input type='submit'/> </form>"
$(document.body).append(html);
}
});
//在#insert插入按钮上绑定事件
$("#insert").bind("click",function(event){
var html = "<form action='add.php' method='post'>name:<input type='text' name='name'/></br>moviename:<input type='text' name='moviename'/><br/><input type='submit'/> </form>"
$(event.target).after(html);
//alert("j");
});
</script>
edit.php
使用url传递数据
<?php
//echo $_POST['name'];
//echo $_POST['moviename'];
//echo $_GET['id'];
//向数据库插入内容
$name = 'bp6am';
$password = 'bp6ampass';
$host = 'localhost';
$db = mysql_connect($host,$name,$password) or die('unable connect');
$query = 'use meinv';
mysql_query($query,$db) or die(mysql_error($db));
//{} 和 ' !!!!!!!!!!!!!!!!!!!
$id = $_GET['id'];
$query = "update movie set name = '{$_POST['name']}',moviename = '{$_POST['moviename']}' where id = $id";
mysql_query($query,$db) or die(mysql_error($db));;
//传递数据
setcookie('pw',$password);
setcookie('name',$name);
setcookie('table2','movie');
//url重定向
$url ="./show.php";
Header("Location: $url");
?>
add.php
注意{} 和 ' !!!!!!!!!!!!!!!!!!!
<?php
//echo $_POST['name'];
//echo $_POST['moviename'];
//echo $_GET['id'];
//向数据库插入内容
$name = 'bp6am';
$password = 'bp6ampass';
$host = 'localhost';
$db = mysql_connect($host,$name,$password) or die('unable connect');
$query = 'use meinv';
mysql_query($query,$db) or die(mysql_error($db));
//{} 和 ' !!!!!!!!!!!!!!!!!!!
$query = "insert into movie (name,moviename) values ('{$_POST['name']}','{$_POST['moviename']}')";
mysql_query($query,$db) or die(mysql_error($db));;
//传递数据
setcookie('pw',$password);
setcookie('name',$name);
setcookie('table2','movie');
//url重定向
$url ="./show.php";
Header("Location: $url");
?>
delete.php
<?php
//echo $_POST['name'];
//echo $_POST['moviename'];
//echo $_GET['id'];
//向数据库插入内容
$name = 'bp6am';
$password = 'bp6ampass';
$host = 'localhost';
$db = mysql_connect($host,$name,$password) or die('unable connect');
$query = 'use meinv';
mysql_query($query,$db) or die(mysql_error($db));
$id = $_GET['id'];
//{} 和 ' !!!!!!!!!!!!!!!!!!!
$query = "delete from movie where id = $id";
mysql_query($query,$db) or die(mysql_error($db));;
//传递数据
setcookie('pw',$password);
setcookie('name',$name);
setcookie('table2','movie');
//url重定向
$url ="./show.php";
Header("Location: $url");
?>
转载于:https://blog.51cto.com/bysowhat/1574558