簡體   English   中英

使用UPDATE(mysql)將數據庫遞增1。 我的代碼有什么問題?

[英]Increment database by 1, using UPDATE (mysql). What's wrong with my code?

我嘗試編寫代碼以在student_list.php中接收輸入的學生ID,並將該ID傳遞給score.php。

在score.php中,使用該ID進行匹配以從數據庫中提取學生的姓名,並在此處顯示它。

然后,在名稱下方,有一個輸入字段,用於為該學生的數據庫添加1分。

但是我收到此消息,“您的SQL語法有誤;請查看與您的MySQL服務器版本相對應的手冊,以找到在第1行的''附近使用正確的語法”。

任何幫助,將不勝感激。 謝謝。

student_list.php

 <?php include_once 'DBconnect.php'; ?> <html> <head>Student List</head> <body> <form method="post" action="score.php"> <?php $result = mysql_query("SELECT * FROM term3") or die(mysql_error()); echo "<table border='1'>"; echo "<tr> <th>Student ID</th> <th>First Name</th> <th> Button </th> </tr>"; while($row = mysql_fetch_array( $result )) { echo "<tr>"; echo '<td>' . $row['student_id'] . ' </td> '; echo '<td>' . $row['student_fname'] . ' </td>'; echo '<td> <button type="submit" name="btn_student_id" value=" ' . $row['student_id'] . ' " >Select</button> </td>'; echo '</tr>'; } echo "</table>"; ?> </form> </body> </html> 

score.php

 <?php include_once 'database_connect.php'; ?> <html> <head>Add Score</head> <body> <?php $student_id = $_POST["btn_student_id"]; $result = mysql_query("SELECT * FROM term3 WHERE student_id=".$_POST['btn_student_id']) or die(mysql_error()); echo "<table border='1'>"; echo "<tr> <th>Student ID</th> <th>First Name</th> </tr>"; while($row = mysql_fetch_array( $result )) { echo "<tr>"; echo '<td>' . $row['student_id'] . ' </td> '; echo '<td>' . $row['student_fname'] . ' </td>'; echo '</tr>'; } echo "</table>"; if(isset($_POST['btn_add_score'])) { $score = $_POST['score']; mysql_query ("UPDATE term3 SET score = score + 1 WHERE student_id = ' ".$_POST['btn_student_id']. " ' "); } ?> <form method="post"> <table> <tr> <td>Score</td> <td> <input type="number" name="score" size="8"> <button type="submit" name="btn_add_score" >Add</button> </td> </tr> </table> </form> </body> </html> 

  ---------------------------------------- Student ID | Name | Select | ---------------------------------------- 10001 | Pat | Button (submit) | ---------------------------------------- 10002 | Jess | Button (submit) | ---------------------------------------- 

嘗試刪除更新查詢中學生ID周圍的引號。 這是多余的

UPDATE term3 SET score = score + 1 WHERE student_id = ".$_POST['btn_student_id']); 

更新資料

$result = mysql_query("SELECT * FROM term3 WHERE student_id=".$_POST['btn_student_id'])

$result = mysql_query("SELECT * FROM term3 WHERE student_id='".$_POST['btn_student_id']."'")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM