在更新多行之前验证foreach循环中的输入

时间:2019-03-25 14:04:26

标签: php

我有多个表行输出,其中的值是从数据库填充的。

void first(Mat img, Vec3b magenta)
{
    int H = 255;
    int W = 255;
    int n = 255;
    int aria=0;

    for (int r = 0; r < H - 1; r++){
        for (int c = 0; c < W - 1; c++){
            int pixel = img.at<uchar>(r, c);
            for (int i = 0; i < n; i++){
                if (pixel = magenta(i))
                    aria++;


            }

        }
    }
    printf("Aria obiectului este:%d \n", aria);
}

void MyCallBackFunc1(int event, int x, int y, int flags, void* param)
{
    //More examples: http://opencvexamples.blogspot.com/2014/01/detect-mouse-clicks-and-moves-on-image.html
    Mat* src = (Mat*)param;
//  int aria;
    if (event == CV_EVENT_LBUTTONDOWN)
    {
        printf("Pos(x,y): %d,%d  Color(RGB): %d,%d,%d\n",
            x, y,


            (int)(*src).at<Vec3b>(y, x)[2],
            (int)(*src).at<Vec3b>(y, x)[1],
            (int)(*src).at<Vec3b>(y, x)[0]);
        Vec3b magenta;
        magenta[0] = (int)(*src).at<Vec3b>(y, x)[0];
        magenta[1] = (int)(*src).at<Vec3b>(y, x)[1];
        magenta[2] = (int)(*src).at<Vec3b>(y, x)[2];
        Mat img = ((Mat*)param)->clone();
        first(img, magenta);

    }
}


void testMouseClick1()
{
    Mat src;
    // Read image from file 
    char fname[MAX_PATH];
    while (openFileDlg(fname))
    {
        src = imread(fname);
        //Create a window
        namedWindow("Window", 1);

        //set the callback function for any mouse event
        setMouseCallback("Window", MyCallBackFunc1, &src);

        //show the image

        imshow("Window", src);

        // Wait until user press some key
        waitKey(0);
    }
}

用户可以更改每个行字段上的值,然后提交使用新值更新数据库的表单。

<tr>
    <td>
        <select name='edit_ativity[]'>
           <option value="11" selected="selected">Aerobic Dancing - Low Impact</option>
           <option value="12">Aerobic Dancing - Strenuously</option>
           <option value="13">Aerobics - High Impact</option>
           <option value="19">Skipping - Slow</option>
        </select>
     </td>
     <td>
          <input type='text' name='edit_duration[]' value='100' />
     </td>
     <td>
          <input type='text' class='datepicker' name='edit_datepicker[]' value='2019-03-01' id=''>
     </td>
</tr>
An other Row 
An other Row 

我需要帮助的只是执行更新命令,没有一个字段为空。

2 个答案:

答案 0 :(得分:2)

只需对要在循环内不为空的所有字段再次执行isset()

if(isset($_POST['edit'])) {
    foreach ($_POST['edit_ativity'] as $key=>$value) {
        if (isset($_POST['edit_ativity'][$key],
                  $_POST['edit_duration'][$key],
                  $_POST['distance_id'][$key], 
                  $_POST['edit_datepicker'][$key])
        ){
            $dist_activity_id = $_POST['edit_ativity'][$key];
            $dist_activity_duration = $_POST['edit_duration'][$key];
            $dist_id = $_POST['distance_id'][$key];
            $dist_date = $_POST['edit_datepicker'][$key];                   
            $stmt = $conn->prepare("UPDATE distance SET dist_activity_id=$dist_activity_id, dist_activity_duration=$dist_activity_duration, dist_date='$dist_date' WHERE id=$dist_id");
            $stmt->execute();
        }
    }

    $stmt->close();
    mysqli_close($conn);
}
  

我还应该指出,您的脚本对SQL Injection Attack是开放的   甚至if you are escaping inputs, its not safe!   在MYSQLI_PDO API中使用prepared parameterized statements

假设您正在使用mysqli_ API

此代码可以保护您免受SQL注入的侵扰,并且更易于阅读。

if(isset($_POST['edit'])) {
    foreach ($_POST['edit_ativity'] as $key=>$value) {
        if (isset($_POST['edit_ativity'][$key], 
                  $_POST['edit_duration'][$key],
                  $_POST['distance_id'][$key], 
                  $_POST['edit_datepicker'][$key])
        ){

            $stmt = $conn->prepare("UPDATE distance SET 
                                        dist_activity_id=?,
                                        dist_activity_duration=?, 
                                        dist_date=? 
                                    WHERE id=?");

            $stmt->bind_param('iisi',$_POST['edit_ativity'][$key],
                                     $_POST['edit_duration'][$key],
                                     $_POST['edit_datepicker'][$key],
                                     $_POST['distance_id'][$key]
                            );
            $stmt->execute();
        }
    }

    $stmt->close();
    mysqli_close($conn);
}

答案 1 :(得分:0)

如果您尝试在更新值之前验证所有输入,则可以通过以下方式实现:

if(isset($_POST['edit'])) {
    foreach ($_POST['edit_ativity'] as $key=>$value) {
        $dist_activity_id = $_POST['edit_ativity'][$key];
        $dist_activity_duration = $_POST['edit_duration'][$key];
        $dist_id = $_POST['distance_id'][$key];
        $dist_date = $_POST['edit_datepicker'][$key];   
        // Validation//
            // if one or more inputs are empty inside the if wont run//
        if (!empty($dist_activity_id) && !empty($dist_activity_duration) && !empty($dist_id) && !empty($dist_date)) {
            $stmt = $conn->prepare("UPDATE distance SET dist_activity_id=$dist_activity_id, dist_activity_duration=$dist_activity_duration, dist_date='$dist_date' WHERE id=$dist_id");
            $stmt->execute();
        }              

    }

    $stmt->close();
    mysqli_close($conn);
}