gpt4 book ai didi

php:使用
标签将新值与 php 中的现有值相加

转载 作者:行者123 更新时间:2023-11-29 22:17:02 25 4
gpt4 key购买 nike

我正在创建一个类似于积分系统的网页。它由一个带有名称和点列的表组成。用户输入一个数字,然后将该值添加到表中的现有数字中。我的问题是如何添加这两个值并更新表(数据库)?

<?php
$con = mysql_connect("xxx, xxx, xxx);
if (!$con) {
die("can not connect:" . mysql_error());
}

mysql_select_db("points", $con);

if(isset($_POST['update'])){
$UpdateQuery = "UPDATE coach_tbl set coachscore = coachscore + '$add' WHERE coach_score = '$_POST[hidden]'";
mysql_query($UpdateQuery, $con);
};

if (isset($_POST['submit'])){
$AddQuery = "INSERT INTO coach_tbl (coach_name, coach_score) VALUES('$_POST[name]', '$_POST[score]')";
mysql_query($AddQuery, $con);

};
$sql = "SELECT * FROM coach_tbl ORDER BY coach_score DESC";

echo "<table border=1>
<tr>
<th>NAME</th>
<th>Score</th>
</tr>";

$myData = mysql_query($sql, $con);
while($record = mysql_fetch_array($myData)) {
echo "<form action=index.php method=post>";
echo "<tr>";
echo "<td><input type=text name=coachname value='" . $record['coach_name'] . "'> </td>";
echo "<td><input type=text name=coachscore value='" . $record['coach_score'] . "'> </td>";
echo "<td><input type=hidden name=hidden value='" . $record['coach_score'] . "'> </td>";
echo "<td><input type=submit name=update value=update'" . "'> </td>";
echo "<td><input type=number min="1" max="10" name=add value=add'" . "'> </td>";
echo "</tr>";
echo "</form>";

}
echo "</table>";



mysql_close($con);

?>

如果有任何问题,我很乐意详细说明。我对 php 也很陌生。

最佳答案

假设您有一个如下所示的 html 表单:

<form action="update.php" method="POST">
<input type="number" name="updateData" value=""/>
<input type="submit">
</form>

和 update.php:

<?php 
//assuming you want 1-10 points max to be added each time
if( isset($_POST['updateData']) && $_POST['updateData'] >=1 && $_POST['updateData'] >=10){

//set to user inputed value
$insertData = $_POST['updateData'];

$sql = "UPDATE point_table SET points = points + $insertData WHERE id = 1";
//you will need to finish off the query by checking connecting with your database.
}

?>

关于php:使用 <form> 标签将新值与 php 中的现有值相加,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31130116/

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