duanou9758 2015-06-30 05:19
浏览 25
已采纳

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

I am creating a webpage that is similar to a points system. It consists of a table with name and points columns. The user inputs a number, which then adds that value to the existing number in the table. My question is how would I be able to add those two values and update the table(database)?

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

?>

If there are any questions I will gladly elaborate on anything. I am also fairly new to php.

  • 写回答

1条回答 默认 最新

  • doukuilian8365 2015-06-30 05:45
    关注

    Let's say you have an html form like so:

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

    and an 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.
    }
    
    ?>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里