//连接数据库
$conn = mysqli_connect('localhost','root','root','exercise');
if(!$conn){
echo mysqli_error();
}
// mysqli_set_charset($conn,utf8);
?>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>电话本title>
<style type="text/css">
body{
padding:50px;
}
a{
padding:10px;
color:black;
text-decoration:none;
font-size:20px;
background-color:#F2F2F2;
margin:40px;
}
table{
margin:50px;
}
style>
head>
<body>
<a href="phone_add.php">添加联系人a>
header('content-type:text/html;charset=utf-8');
require_once ('connect.php');
function showTable($conn,$table_name){
$sel = "select * from telephone";
$query = mysqli_query($conn,$sel);
if(!$query){
echo mysqli_error();
}
//获取表头
echo " ";
while($field = mysqli_fetch_field($query)){
echo "{$field->name} ";
}
echo " ";
//循环取出数据
while ($row=mysqli_fetch_row($query)){
echo "";
foreach($row as $value){
echo "$value ";
}
$rowid=($row[0]);
echo "编辑 ";
echo "删除 ";
echo " ";
}
echo "
";
}
showTable($conn,"telephone");
?>
body>
html>
/**********************************************************/
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Documenttitle>
<style type="text/css">
form{
margin:50px;
}
div{
margin:50px;
}
style>
head>
<body>
<form id="form1" name="form1" method="post" action="phone_add_handle.php">
<div>
<p>姓 名:p><input type="text" name="na" id="na">input><br/>
div>
<div>
<p>电话号:p><input type="text" name="phone" id="phone">input>
div>
<div>
<input type="submit" name="button" id="button" value="提交">
div>
form>
body>
html>
'content-type:text/html;charset=utf-8');
require_once ('connect.php');
//将传递过来的数据入库
// $num=$_POST['num'];
$na=$_POST['na'];
$phone=$_POST['phone'];
$insertsql = "insert into telephone(name,phone) values ('$na','$phone')";
if(mysqli_query($conn,$insertsql)){
echo "";
}
else {
echo "";
}
?>
/*********************************************************/
header('content-type:text/html;charset=utf-8');
require_once ('connect.php');
//读取旧信息
$id = $_GET['id'];
$sel = "select * from telephone where id='$id'";
$query = mysqli_query($conn,$sel);
if(!$query){
echo mysqli_error();
}
$data = mysqli_fetch_assoc($query);
if(!$data){
echo mysqli_error();
}
?>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="Generator" content="EditPlus®">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
<title>Documenttitle>
<style type="text/css">
form{
margin:50px;
}
div{
margin:50px;
}
style>
head>
<body>
<form id="form1" name="form1" method="post" action="phone_modify_handle.php">
<input type="hidden" name='id' value="">
<div>
<input name="num" id="num" type="hidden" value="">
div>
<div>
<p>姓 名:p><input type="text" name="na" id="na" value="">
div>
<div>
<p>电话号:p><input type="text" name="phone" id="phone" value="">
div>
<div>
<input type="submit" name="button" id="button" value="提交">
div>
form>
body>
html>
'content-type:text/html;charset=utf-8');
require_once ('connect.php');
$num=$_POST['num'];
$na=$_POST['na'];
$phone=$_POST['phone'];
$updatesql = "update telephone set name='$na',phone='$phone' where id='$num'";
if(mysqli_query($conn,$updatesql)){
echo "";
}
else {
echo "";
// echo mysqli_error();
}
?>
/*****************************************************************/
header('content-type:text/html;charset=utf-8');
require_once ('connect.php');
$id = $_GET['id'];
$deletesql = "delete from telephone where id='$id'";
if(mysqli_query($conn,$deletesql)){
echo "";
}
else{
echo "";
}
?>