简体   繁体   English

使用UPDATE(mysql)将数据库递增1。 我的代码有什么问题?

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

I try to write code that receive a student ID as an input in student_list.php, and pass this ID to score.php. 我尝试编写代码以在student_list.php中接收输入的学生ID,并将该ID传递给score.php。

At score.php, use that ID to match and pull out student's name from database, and display it here. 在score.php中,使用该ID进行匹配以从数据库中提取学生的姓名,并在此处显示它。

Then, below the name, there is an input field, for adding 1 score to database for this student. 然后,在名称下方,有一个输入字段,用于为该学生的数据库添加1分。

But I got this message, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '' at line 1". 但是我收到此消息,“您的SQL语法有误;请查看与您的MySQL服务器版本相对应的手册,以找到在第1行的''附近使用正确的语法”。

Any help would be appreciated. 任何帮助,将不胜感激。 Thank you. 谢谢。

student_list.php 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 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) | ---------------------------------------- 

Try to remove the quotation marks around the student id in the update query. 尝试删除更新查询中学生ID周围的引号。 It is redundant 这是多余的

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

Update 更新资料

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

to

$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