html 與php+MySQL完成前後端互動
(1)php建立資料庫【連線】
語法:Object mysqli_connect("域名","DB賬號","DB密碼","DB庫名")
例子:$con = mysqli_connect('localhost','root','','frankdb');
(2)向DB中插入資料時包含中文出現亂碼的解決方案
語法:mysqli_query($con,"set names utf8");
說明:設定成功會返回1,根據實際情況並不一定必須儲存返回結果。
(3)設定client端和server端保持字元編碼一致
語法:mysqli_query($con,"set character_set_client=utf8");
mysqli_query($con,"set character_set_results=utf8");
(4)執行sql語句
語法:$結果 = $DB連線->query(sql語句);
例子:var_dump($result = $con->query($sql));
2.使用sql語句基本【模板】
a.建立連線
b.判斷是否連線
c.設定編碼
d.建立sql語句
e執行結果條數
g.拼湊結果
h.json化返回
<?php
//a.sql 查詢語句
無條件查詢,即直接寫1即可
//$sql='select * from 哪張表 where 條件';
有條件查詢,在where後面寫出查詢條件,如果多個條件需要用and 或or 來連線。
//$sql="select stuName from stud where stuScore='100' and stuGender='female'";
//$sql="select stuName from stud where stuScore='100' or stuGender='female'";
$con=mysqli_connect('localhost','root','','studb')
if($con){
echo'<pre>';
echo'資料庫連線成功,等待指令...';
mysqli_query($con,'set names utf8');
mysqli_query($con,'set character_set_client=utf8');
mysqli_query($con,'set character_set_results=utf8');
$sql="select * from stud where 1";
$result=$con->query($sql);
if($result>num_rows>0){
$info=[];
for($i=0;$row=$result->fet_assoc();$i++){
$info[$i]=$row;
}
echo json_encode($info);
}
}else{
echo'<pre>';
echo'資料連線失敗,請重新連線‘;
}
b.插入語句(新增語句)
兩種寫法:(1)$sql="insert into 表名(欄位1,欄位2,...) values(值1,值2,...)";
(2)$sql='insert into 表名('值1’,'值2',...)";
$con=mysqli_connect('localhost','root','','studb');
if($icon){
echo'<pre>';
echo'資料庫連線成功,等待指令...';
mysqli_query($con, 'set names utf8');
mysqli_query($con, 'set character_set_client=utf8');
mysqli_query($con, 'set character_set_result=utf8');
$sql="insert into stud stuName,stuGender,stuAge,stuNum,stuScore)values('lucy','female','14','123456789','90')";
$sql="insert into stud values('lucy','female','14','123456789','90')";
$result=$con->query($sql);
if($result){
echo'新增成功';
}else{
echo'新增失敗';
}
c.修改語句(更新語句)update
$sql="update 表名 set 欄位1=‘新值1’,欄位2=‘新值2’,... where 條件“;
$con=mysqli_connect('localhost','root','','studb');
if($con){
echo "<pre>";
echo "資料庫連線成功,等待指令...";
mysqli_query($con, 'set names utf8');
mysqli_query($con, 'set character_set_client=utf8');
mysqli_query($con, 'set character_set_results=utf8');
$sql="update stud set stuScore='100' where stuName='lily'";
$result=$con->query($sql);
var_dump($result);
}else{
echo "資料庫連線失敗!!!";
}
d. 刪除語句delete
$sql="delete from 表名 where 條件“;
$con=mysqli_connect('localhost','toot','';'studb' );
if($con){
echo "<pre>";
echo "資料庫連線成功,等待指令...";
//
mysqli_query($con, 'set names utf8');
mysqli_query($con, 'set character_set_client=utf8');
mysqli_query($con, 'set character_set_results=utf8');
//
$sql = "delete from stud where stuName='lucy'";
$result = $con->query($sql);
var_dump($result);
}else{
echo "資料庫連線失敗!!!";
}
?>
ajax & php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<span>姓名:</span><input type="text" class="stuName"><br/>
<span>性別:</span><input type="text" class="stuGender"><br/>
<span>年齡:</span><input type="text" class="stuAge"><br/>
<span>手機:</span><input type="text" class="stuNumber"><br/>
<span>分數:</span><input type="text" class="stuScore"><br/>
<button>增:將上述資訊新增至資料庫</button><br/>
<button>刪:根據姓名刪除指定的內容</button><br/>
<button>改:根據姓名修改指定的內容</button><br/>
<button>查:查詢資料庫中所有的內容</button>
<!-- 1.引入jquery框架 -->
<script src="jquery-1.12.3.min.js"></script>
<script>
$('button:eq(0)').click(function(){
//2.在想要和後臺互動的時候,呼叫$.ajax()方法
//引數是JSON格式
$.ajax({
//3.ajax結構需要一系列引數支援
type:'post',//get|post
url:'http://127.0.0.1/day3/code/lesson6_ajax&php/lesson6_ajax&php.php',
dataType:'json',
data:{
//data是post請求獨有的,因為post請求才需要攜帶資料給後臺
stuName:$('.stuName').val(),
stuGender:$('.stuGender').val(),
stuAge:$('.stuAge').val(),
stuNumber:$('.stuNumber').val(),
stuScore:$('.stuScore').val(),
},
//請求完成後,若收到後臺返回的資料(收到響應response),這個函式會被自動執行。
success:function(data){
console.log(data);
}
});
});
$('button:eq(1)').click(function(){
});
$('button:eq(2)').click(function(){
});
$('button:eq(3)').click(function(){
});
</script>
</body>
</html>
<?php
/*
1.內建物件
在php中內建了兩個物件用來接收,前端發來的資訊。
$_GET 和 $_POST
$_GET用於獲取在前端通過get請求發來的資訊
$_POST用於獲取在前端通過post請求發來的資訊
*/
echo json_encode($_POST);
?>
ajax select&php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
</head>
<body>
<span>姓名:</span><input type="text" class="stuName"><br/>
<span>性別:</span><input type="text" class="stuGender"><br/>
<span>年齡:</span><input type="text" class="stuAge"><br/>
<span>手機:</span><input type="text" class="stuNumber"><br/>
<span>分數:</span><input type="text" class="stuScore"><br/>
<button>增:將上述資訊新增至資料庫</button><br/>
<button>刪:根據姓名刪除指定的內容</button><br/>
<button>改:根據姓名修改指定的內容</button><br/>
<button>查:查詢資料庫中所有的內容</button>
<script src="jquery-1.12.3.min.js"></script>
<script>
$('button:eq(0)').click(function(){
//判空操作
var stuNameValue = $('.stuName').val();
var stuGenderValue = $('.stuGender').val();
var stuAgeValue = $('.stuAge').val();
var stuNumberValue = $('.stuNumber').val();
var stuScoreValue = $('.stuScore').val();
if(
stuNameValue.length==0||
stuGenderValue.length==0||
stuAgeValue.length==0||
stuNumberValue.length==0||
stuScoreValue.length==0){
alert('任意某一個輸入資訊都不能為空!');
return;
}
$.ajax({
type:'post',
url:'http://127.0.0.1/day3/code/lesson7_html&php&mysql/lesson7_html&php&mysql.php',
dataType:'json',
data:{
stuName:$('.stuName').val(),
stuGender:$('.stuGender').val(),
stuAge:$('.stuAge').val(),
stuNumber:$('.stuNumber').val(),
stuScore:$('.stuScore').val(),
},
success:function(data){
console.log(data);
if(data.msg=='add success'){
alert('新增成功');
}else{
alert('新增失敗');
}
}
});
});
$('button:eq(1)').click(function(){
});
$('button:eq(2)').click(function(){
});
$('button:eq(3)').click(function(){
$.ajax({
type:'get',
url:'http://127.0.0.1/day3/code/lesson7_html&php&mysql/lesson7_select.php',
dataType:'json',
success:function (data){
console.log(data);
},
error: function (err){
console.log(err);
}
});
});
</script>
</body>
</html>
<?php
//查詢功能的後臺php檔案
$success = array('status' => 'success');
$error = array('status' => 'error');
$con = mysqli_connect('localhost','root','','studb');
if($con){
mysqli_query($con, 'set names utf8');
mysqli_query($con, 'set character_set_client=utf8');
mysqli_query($con, 'set character_set_results=utf8');
$sql = "select * from gradeonesheet where 1";
$result = $con->query($sql);
if($result->num_rows>0){
$info = [];
for($i=0; $row=$result->fetch_assoc(); $i++){
$info[$i] = $row;
}
//
echo json_encode($info);
}
}
?>
<?php
//新增資料的
$success = array('status' => 'OK');
$error = array('status' => 'error');
$con = mysqli_connect('localhost','root','','studb');
if($con){
mysqli_query($con, 'set names utf8');
mysqli_query($con, 'set character_set_client=utf8');
mysqli_query($con, 'set character_set_results=utf8');
//
$stuName = $_POST['stuName'];
$stuGender = $_POST['stuGender'];
$stuAge = $_POST['stuAge'];
$stuNumber = $_POST['stuNumber'];
$stuScore = $_POST['stuScore'];
$sql = "insert into gradeonesheet values('$stuName','$stuGender','$stuAge','$stuNumber','$stuScore')";
$result = $con->query($sql);
if($result){
$success['msg'] = 'add success';
echo json_encode($success);
}else{
$error['msg'] = 'add failed';
echo json_encode($error);
}
}else{
$error['msg'] = 'database connect failed';
echo json_encode($error);
}
?>