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

时间:2016-01-03 16:56:39

标签: php mysql

嗨亲爱的朋友昨天我询问了一个教师评级系统但是没有任何想法请帮助我。我正在建立一个教师评级系统,我想通过在每个教师的名字前面设置文本框来评价每个教师。用户将在特定教师的文本框中添加的数字将被添加到当前的速率老师。但代码不能正常工作。当我通过文本框添加值时,它会将最后一个文本框中的值添加到所有行。例如,我要添加2的第一个教师,第二个我要添加4对于第3位老师我想评分3.所以不是2和3而是所有教师的所有评级都增加3.如何解决问题?请帮助我给2页代码m.php和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 个答案:

答案 0 :(得分:0)

像这样......给出表中带有id的评级名称数组,并用正确的值和id更新记录......但是对于用户名和密码情况,不要这样做...

<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");
}