抱歉我的每日回復上限已到,無法進行回復
我的問題卡在:
course的input欄位可以正常查詢,
但是teacher和semaster查詢後
顯示出來的資料是class資料表全部的資料
而不是我要的條件式查詢的資料,
大家好
我撰寫了一個簡易的課程搜尋程式(seacrch.php)
但是中文與英文依然還是無法傳遞給seacrch.php做搜尋的動作
我有參考網路上的一些寫法,修正了我的程式碼
但是為什麼仍然還是無法傳遞中文與英文給seacrch.php做搜尋的動作
我的前端index.js
$(document).ready(function () {
const web=`<h3>109學年度學校課程查詢系統</h3>
<table>
<tr>
<td>課程名稱:</td>
<td><input type="text" name="course" id="cousrse"></td>
<td>授課教師:</td>
<td><input type="text" name="teacher" id="teacher"></td>
<td>開課學期:</td>
<td><input type="text" name="semaster" id="semaster"></td>
<td><input type="button" id="querycheck" value="查詢"></td>
</tr>
</table>
<br>
<div id="ans"></div>`
$("#root").html(web);
$("#querycheck").click(function () {
let data={
"course":$("#course").val(),
"teacher":$("#teacher").val(),
"semaster":$("#semaster").val()
};
axios.post("../admin/index.php?action=seacrch",Qs.stringify(data))
.then(res => {
let response=res["data"];
let ans=response["result"]
let str=`<table>`;
ans.forEach(element => {
str+=`<tr>`;
str+=`<td>`+element["course"]+`</td>`;
str+=`<td>`+element["teacher"]+`</td>`;
str+=`<td>`+element["classroom"]+`</td>`;
str+=`<td>`+element["semaster"]+`</td>`;
});
str+=`</tr>`;
str+=`</table>`
$("#ans").html(str);
});
});
});
seacrch.php
<?php
require_once './mysql.inc.php';
function seacrch(){
$response = openDB();
if($response['status']==200) {
$conn=$response['result'];
if(isset($_POST['course'])){
$course=$_POST['course'];
$sql = "SELECT * FROM `class` WHERE `course` LIKE '%$course%'";
$stmt = $conn->prepare($sql);
$result = $stmt->execute();
}
elseif(isset($_POST['teacher'])){
$teacher=$_POST['teacher'];
$sql = "SELECT * FROM `class` WHERE `teacher` LIKE '%$teacher%'";
$stmt = $conn->prepare($sql);
$result = $stmt->execute();
}
elseif(isset($_POST['semaster'])){
$semaster=$_POST["semaster"];
$sql = "SELECT * FROM `class` WHERE `semaster` LIKE '%$semaster%'";
$stmt = $conn->prepare($sql);
$result = $stmt->execute();
}
else{
$sql = "SELECT * FROM `class`";
$stmt = $conn->prepare($sql);
$result = $stmt->execute();
}
if($result) {
$ans = $stmt->fetchAll(PDO::FETCH_ASSOC);
$response['status'] = 200;
$response['message'] = "查詢成功";
$response['result'] = $ans;
}
else {
$response['status'] = 500;
$response['message'] = "查詢失敗";
$ans=$response['result'];
$response['result'] = $ans;
}
}
echo json_encode ($response);
}
?>
不只一個地方怪怪的
先把標示 //delete 的兩列刪掉試試看
也許就 ok 了也說不定...
<?php
require_once './mysql.inc.php';
function seacrch(){//delete
$response = openDB();
if($response['status']==200) {
$conn=$response['result'];
if(isset($_POST['course'])){
$course=$_POST['course'];
$sql = "SELECT * FROM `class` WHERE `course` LIKE '%$course%'";
$stmt = $conn->prepare($sql);
$result = $stmt->execute();
}
elseif(isset($_POST['teacher'])){
$teacher=$_POST['teacher'];
$sql = "SELECT * FROM `class` WHERE `teacher` LIKE '%$teacher%'";
$stmt = $conn->prepare($sql);
$result = $stmt->execute();
}
elseif(isset($_POST['semaster'])){
$semaster=$_POST["semaster"];
$sql = "SELECT * FROM `class` WHERE `semaster` LIKE '%$semaster%'";
$stmt = $conn->prepare($sql);
$result = $stmt->execute();
}
else{
$sql = "SELECT * FROM `class`";
$stmt = $conn->prepare($sql);
$result = $stmt->execute();
}
if($result) {
$ans = $stmt->fetchAll(PDO::FETCH_ASSOC);
$response['status'] = 200;
$response['message'] = "查詢成功";
$response['result'] = $ans;
}
else {
$response['status'] = 500;
$response['message'] = "查詢失敗";
$ans=$response['result'];
$response['result'] = $ans;
}
}//delete
echo json_encode ($response);
}
?>
我前面的方式需要以
index.php?action=searach的方式來做包裝
<?php
if(isset($_GET['action']))
$action=$_GET['action'];
else
$action='_no_action';
switch ($action) {
case 'seacrch':
require_once "seacrch.php";
$response = seacrch();
break;
default:
$response['status'] = 404;
$response['message'] = "找不到網頁!!";
break;
}
return($response);
?>
我有補上我的前端網頁撰寫語法
index.php?action=searach的方式來做包裝
如果是這樣
那在那裡傳送 course,teacher,semaster 的 _POST
參數?
我的前端網頁有
course,teacher,semaster三個inqut type=text
需要透過axios post傳送到index.php?action=searach進行查詢的動作
哦我看懂了
那在 seacrch.php 裡把$_POST['course']
顯示出來
如果是空值
就修改 function seacrch() 改成
function seacrch($postVars)
if (isset($postVars['course'])) {
...
試看看
如果不行
那就看別的高手的回答囉...
輸出了錯誤訊息
Fatal error: Uncaught ArgumentCountError: Too few arguments to function seacrch(), 0 passed in C:\xampp\htdocs\web2\admin\index.php on line 10 and exactly 1 expected in C:\xampp\htdocs\web2\admin\seacrch.php:3 Stack trace: #0 C:\xampp\htdocs\web2\admin\index.php(10): seacrch() #1 {main} thrown in C:\xampp\htdocs\web2\admin\seacrch.php on line 3
index.php 裡的 $response = seacrch();
要改成$response = seacrch($_POST);
現在我的input: course可正常傳遞字串了
但是現在卡在teacher和semaster無法正常傳遞字串
course 可以而 teacher,semaster 不行?
teacher 可能是因為這裡的 typo
$("#querycheck").click(function () {
let data={
"course":$("#course").val(),
"tecaher
":$("#teacher").val(),
"semaster":$("#semaster").val()
};
至於 semaster
我就看不出來了...
teacher我修改成正確的了
但是還是卡在semaster和tecaher無法傳送字串
course 可以而 teacher,semaster 不行?
由於 course 和 teacher/semaster 的寫法都一樣(程式)
差別就剩下(資料)的部份了
可以寫出三個資料嗎?
或者三個欄位都輸入和 course 一模一樣的資料試看看
if(isset($_POST['course'])){
...
}
elseif(isset($_POST['teacher'])){
...
}
elseif(isset($_POST['semaster'])){
...
}
終於看出來了
這種寫法是 or 的寫法,就是三選一的寫法
應該是 $course 被當成字串
$sql = "SELECT * FROM `class` WHERE `course` LIKE '%".$course."%'";
$stmt = $conn->prepare($sql);
or
// prepare and bind
$stmt = $conn->prepare("SELECT * FROM `class` WHERE `course` LIKE '%?%'");
$stmt->bind_param("s", $course);
$stmt->execute();
i - integer
d - double
s - string
b - BLOB
https://www.w3schools.com/php/php_mysql_prepared_statements.asp
<?php
require_once './mysql.inc.php';
function seacrch(){
$response = openDB();
if($response['status']==200) {
$conn=$response['result'];
if(isset($_POST['course'])) {
$course=$_POST['course'];
$sql = "SELECT * FROM `class` WHERE `course` LIKE '%".$course."%'";
$stmt = $conn->prepare($sql);
$result = $stmt->execute();
}
elseif(isset($_POST['teacher'])){
$teacher=$_POST['teacher'];
$sql = "SELECT * FROM `class` WHERE `teacher` LIKE '%".$teacher."%'";
$stmt = $conn->prepare($sql);
$result = $stmt->execute();
}
elseif(isset($_POST['semaster'])){
$semaster=$_POST["semaster"];
$sql = "SELECT * FROM `class` WHERE `semaster` LIKE '%".$semaster."%'";
$stmt = $conn->prepare($sql);
$result = $stmt->execute();
}
else{
$sql = "SELECT * FROM `class`";
$stmt = $conn->prepare($sql);
$result = $stmt->execute();
}
if($result) {
$ans = $stmt->fetchAll(PDO::FETCH_ASSOC);
$response['status'] = 200;
$response['message'] = "查詢成功";
$response['result'] = $ans;
}
else {
$response['status'] = 500;
$response['message'] = "查詢失敗";
$ans=$response['result'];
$response['result'] = $ans;
}
}
echo json_encode ($response);
}
?>
還是沒辦法
id="cousrse" => id="course"
這行先console.log看一下有沒有都有資料 F12 開發人員選項CONSOLE那邊看LOG
let data={
"course":$("#course").val(),
"tecaher":$("#teacher").val(),
"semaster":$("#semaster").val()
};
console.log(data);
如果都有了才能做下一步非同步
另外看看有沒有紅字
沒有顯示紅色字 錯誤訊息
現在我的input: course可正常傳遞字串了
但是現在卡在teacher和semaster無法正常傳遞字串
我發現這個也拼錯了 "tecaher"=>"teacher"
let data={
"course":$("#course").val(),
"tecaher": => "teacher"
請問一下剛剛 console.log(data); 的結果
然後你寫的這個
// 如果你的course有東西就不會跑那兩個
// if elseif 是其一正確而執行 沒有就跑 else
if(isset($_POST['course'])){}
elseif(isset($_POST['teacher'])){}
elseif(isset($_POST['semaster'])){}
else{}
所以你如果三種都要搜尋 你的if就獨立三個
if(isset($_POST['course'])){}
if(isset($_POST['teacher'])){}
if(isset($_POST['semaster'])){}
但是結果回來又要整理一次3個sql出來的資料,
如果你要搜尋的是三個結果都有的課程何不訪使用一個sql去搜尋
SELECT * from `class` WHERE `course` LIKE '%$course%' OR
`teacher` LIKE '%$teacher%' OR `semaster` LIKE '%$semaster%'
還有 seacrch 跟 search ... 各種粗心