简体   繁体   中英

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.

At score.php, use that ID to match and pull out student's name from database, and display it here.

Then, below the name, there is an input field, for adding 1 score to database for this student.

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".

Any help would be appreciated. Thank you.

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) | ---------------------------------------- 

Try to remove the quotation marks around the student id in the update query. 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']."'")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM