gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 23:54:30 24 4
gpt4 key购买 nike

我尝试编写代码以在 student_list.php 中接收学生 ID 作为输入,并将此 ID 传递给 score.php。

在 score.php 中,使用该 ID 匹配并从数据库中提取学生姓名,并将其显示在此处。

然后,在姓名下方,有一个输入字段,用于为该学生向数据库添加 1 个分数。

但我收到这条消息,“您的 SQL 语法有错误;请查看与您的 MySQL 服务器版本对应的手册,了解在第 1 行的 '' 附近使用的正确语法”。

如有任何帮助,我们将不胜感激。谢谢。

学生名单.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>

分数.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']); 

关于php - 使用 UPDATE (mysql) 将数据库增加 1。我的代码有什么问题?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33987958/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com