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

报告相同问题?

悬赏问题

  • ¥20 易康econgnition精度验证
  • ¥15 线程问题判断多次进入
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致