duanjiao7440 2018-02-05 17:48
浏览 86
已采纳

通过PHP更新SQL只是第一行的值

I have a problem with my script. So I have a table and I am fetching the values from the db into the html table with the help of php. Then I have the javascript code to display html form underneath each row of the table fetched from the db. I Have a Hidden input element in my form to get the unique id of the form submitted Then I use an ajax script which submits the request into an Php file.

The problem, IF I SUBMIT THE FORM FROM MY FIRST ROW the values get change for that particular row (Which works for me). But if I submit the form from the others row the value remains same for that particular row. I am trying to figure this out from the past 3 hours.Need Help! Thank you

I Have a php script:

<?php
foreach($results as $data){
    echo '<tbody>
            <tr class="dropDown">
                <td>1</td>
                <td>'.$data['Title'].'</td>
                <td>'.$data['criticality'].'</td>
                <td>'.$data['Priority'].'</td>
                <td>'.$data['Description'].'</td>
                <td>'.$data['Date_Submitted'].'</td>
                <td>'.$data['NO'].'</td>
                <td></td>
            </tr>
        </tbody>';
}
?>

And my Html form is in the same for loop

<form action="/action_page.php">
    <input type="text" name="rowID" value='.$data['NO'].'>
    <fieldset>
        <label>XYZ Questions </label><br>
        <label class="radio-inline">
            <input type="radio" name="optradio">
            <label>YES</label>
        </label>
        <label class="radio-inline left">
            <input type="radio" name="optradio">
            <label>NO</label>
        </label>
    </fieldset>
    <fieldset>
        <label>XYZ Questions </label><br>
        <label class="radio-inline">
            <input type="radio" name="optradio1">
            <label>YES</label>
        </label>
        <label class="radio-inline left">
            <input type="radio" name="optradio1">
            <label>NO</label>
        </label>
    </fieldset>
    <div class="checkbox">
        <label><input type="checkbox"> Remember me</label>
    </div>
    <button type="submit" class="btn btn-default">Submit</button>
</form>

My ajax script:

var launchAjax = function () { // event handler for button click
    $.get("php/inbetween.php/", {
        id: $("[name=rowID]").val(),      // getting the unique if of each form
        question: $("[name=optradio]:checked").val(),
        question1: $("[name=optradio1]:checked").val(),
    });
}
$("#no").click(launchAjax);

And my php and sql :

<?php
    include 'common.php';

    $id = filter_input(INPUT_GET, "id", FILTER_SANITIZE_NUMBER_INT);
    $question = filter_input(INPUT_GET, "question", FILTER_SANITIZE_STRING);
    $question1 = filter_input(INPUT_GET, "question1", FILTER_SANITIZE_STRING);

    function getMark($answer, $mark = 1) {
        $result = 0;
        if($answer == 'YES'){
            $result = $mark;
        }
        return $result;
    }
    $p = 0;
    $p += getMark($question, 1); // provide the answer and the mark
    $p += getMark($question1, .5);
    $c = 0;
    $c += getMark($question, 0.5); // provide the answer and the mark
    $c += getMark($question1, 1);

    $command1 = "UPDATE rating SET criticality = '$c' , Priority = '$p'
    WHERE no = '$id'";

    // prepare and executing
    $stmt1 = $dbh->prepare($command1);
    $result1 = $stmt1->execute();
?>
  • 写回答

1条回答 默认 最新

  • dongliu8559 2018-02-16 23:24
    关注

    You can include this input into your form

    <input type="hidden" name="rowId" value='.$data['NO']'>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题