douwen9540 2016-01-03 16:56
浏览 32

如何使用MySQL为现有值增加价值?

hi dear friends yesterday I asked about a teachers rating system but did not get any idea please help me. i am making a teacher rating system where i want to rate every teacher by making text boxes in front of the name of each teacher .the number that the user will add in text box for a specific teacher is to be added to a current rate of teacher.but the code is not working properly.when i add values through text boxes it adds the value from the last text box to all rows.for example for the first teacher i want to add 2 ,for the second i want to add 4 and for the 3rd teacher i want to rate 3. so instead of 2 and 3 all the rating of all teachers are incremented by 3.how to solve the problem?please help i am giving code of 2 pages m.php and n.php

m.php

<form action="n.php" method="post" enctype="multipart/form-data">
<table width="642" height="215" border="10" align="left" cellspacing="0"  >
    <tr>
        <th class="style5">Teacher ID</th>
        <th width="90" class="style5">Teacher Name</th>
        <th width="127" class="style5">Teacher Registration</th>
        <th width="135" class="style5">Teacher Qualification</th>
        <th width="92" class="style5">Teacher Subject</th>
        <th width="92" class="style5">Action</th>
      </tr>
    <?php
    include 'conn.php';

    $sql = "SELECT * FROM teacher ";
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {
        while($row = $result->fetch_array()){
        $id=$row['tid'];
        ?>
        <tr>
            <td height="50" align="center" class="style5"><?php echo $row['tid'];?></td>
            <td align="center" class="style5"><?php echo $row['tname'];?></td>
            <td align="center" class="style5"><?php echo $row['treg'];?></td>
            <td align="center" class="style5"><?php echo $row['qualification'];?></td>
            <td align="center" class="style5"><?php echo $row['subject'];?></td>
             <td align="center"> <input type="text" name="rating">
          </td>
        </tr>
        <?php
        }   
    }else{
        echo "<center><p><font size=10/> No Records</p></center>";
    }

    $conn->close();
    ?><tr><td colspan="6">
    <input type="submit" name="submit" value="Enter"></td></tr>
  </table>
</form>

n.php

<?php
mysql_connect("localhost","root","") or die ("couldnt connnect to server");
mysql_select_db("project")  or die ("couldnt connnect to database");


include 'conn.php';
if(isset($_POST['submit']))
{
    $sql = "SELECT * FROM teacher";
    $result = $conn->query($sql);
    if($result->num_rows > 0){
        while($row = $result->fetch_array()){
            $id=$row['tid'];
            $newvalue=$_POST['rating'];
            $sql_update="UPDATE teacher set hits =  hits +$newvalue where tid=".$id.""; 
            mysql_query($sql_update) or die(mysql_error());
            header("location:m.php");
        }
    }
}
  • 写回答

1条回答 默认 最新

  • douzheyo2617 2016-01-12 04:04
    关注

    Like this ... give rating name array with id from table and update the record with the right value and id ... but never do this way for username and password case ...

    <form action="n.php" method="post" enctype="multipart/form-data">
    <table width="642" height="215" border="10" align="left" cellspacing="0"  >
    <tr>
        <th class="style5">Teacher ID</th>
        <th width="90" class="style5">Teacher Name</th>
        <th width="127" class="style5">Teacher Registration</th>
        <th width="135" class="style5">Teacher Qualification</th>
        <th width="92" class="style5">Teacher Subject</th>
        <th width="92" class="style5">Action</th>
    </tr>
    <?php
    include 'conn.php';
    $sql = "SELECT * FROM teacher ";
    $result = $conn->query($sql);
    if ($result->num_rows > 0) {
        while($row = $result->fetch_array()){
        $id=$row['tid'];
        ?>
        <tr>
            <td height="50" align="center" class="style5"><?php echo $row['tid'];?></td>
            <td align="center" class="style5"><?php echo $row['tname'];?></td>
            <td align="center" class="style5"><?php echo $row['treg'];?></td>
            <td align="center" class="style5"><?php echo $row['qualification'];?></td>
            <td align="center" class="style5"><?php echo $row['subject'];?></td>
             <td align="center"> <input type="text" name="rating[<?php echo $id; ?>]">
          </td>
        </tr>
        <?php
        }   
    }else{
        echo "<center><p><font size=10/> No Records</p></center>";
    }
    
    $conn->close();
    ?><tr><td colspan="6">
    <input type="submit" name="submit" value="Enter"></td></tr>
    </table>
    </form>
    

    <?php
    mysql_connect("localhost","root","") or die ("couldnt connnect to server");
    mysql_select_db("project")  or die ("couldnt connnect to database");
    include 'conn.php';
    if(isset($_POST['submit']))
    {
    $sql = "SELECT * FROM teacher";
    $result = $conn->query($sql);
    if($result->num_rows > 0){
        while($row = $result->fetch_array()){
            $id=$row['tid'];
            $newvalue=$_POST['rating'];
            if(isset($newvalue[$id])) {
                $new_temp_value = $newvalue[$id];
                $sql_update="UPDATE teacher set hits =  hits +$new_temp_value where tid=".$id."";
                mysql_query($sql_update) or die(mysql_error());
            }
        }
    }
    
    header("location:m.php");
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)