将id从表单传递到后端脚本并将其值保存在数据库中

时间:2015-01-23 12:13:27

标签: php mysql sql mysqli reference

我有一个表单,其中为用户显示的任务很少,每个任务都有自己的taskid。随着每个任务的进行,用户需要填写的要求很少。我还希望随着用户的回复,每个任务的ID也应该被携带并保存在数据库中

<?php
    require 'connection.php';

    $result = mysqli_query($con,"SELECT * FROM task where status='active'");
    while($row = mysqli_fetch_array($result))
        {
            $taskid=$row['id'];
                echo "<form class='form-horizontal' role='form' action='add_user_task.php' enctype='multipart/form-data' method='post'>";
                    echo "<div class='col-md-12'>";
                        echo"<h3>".$row['task_title']."</h3><br>";
                            echo "<div class='col-md-9'>";
                                echo "<p>".$row['task_desc']."</p>";
                            echo "</div>";

                            echo "<div class='col-md-1'></div>";
                                echo "<div class='col-md-2'>";
                                    echo "<p>".$row['task_points']."</p>";
                                echo "</div>";
                            echo "</div>";

                            echo "<p>&nbsp;</p>";
                            echo "<div class='col-md-9'>";
                                echo "<p> Please provide a verification link where ever required </p>";
                            echo "</div>";

                            echo "<p>&nbsp;</p>";
                            echo "<div class='col-md-12'>";
                                echo "<div class='col-md-10'>";
                                    echo "<label class='col-lg-3'> Link One:</label>";
                                            echo "<input class='co-g-7' name='facebook_pro_link'>";
                                echo "</div>";
                                echo "<div class='col-md-2'>";
                                    echo "<input type='checkbox' name='facebook_pro_complete' value='complete'>Completed<br>";
                                echo "</div>";
                            echo "</div>";
                            echo "<center><input class='btn btn-primary' value='Save Changes' type='submit' name='submit'></center>";
                echo "</form>";
        }
    ?>  

在这个脚本中,我希望添加从表单中携带的taskid,并将其保存在数据库中

add_user_task.php

<?php
session_start(); 
require 'connection.php';
if($_POST)
    {
        // escape variables for security

        $facebook_pro_link = mysqli_real_escape_string($con, $_POST['facebook_pro_link']);
        $facebook_pro_complete = mysqli_real_escape_string($con, $_POST['facebook_pro_complete']);

        $sql="INSERT INTO user_task (task_status_by_user,task_link) VALUES  ('$facebook_pro_complete','$facebook_pro_link')";

        if (!mysqli_query($con,$sql)) 
            {
                die('Error: ' . mysqli_error($con));
            }
    }
?>

任何人都可以告诉我如何做到这一点

2 个答案:

答案 0 :(得分:0)

添加隐藏字段:echo "<input type='hidden' name='id' value='" . $row['id'] . "' />';它将与发布请求一起发送。

您还可以更改操作action='add_user_task.php?id=" . $row['id'] . "'并使用$_GET['id']

获取值

答案 1 :(得分:0)

这是您更新的视图文件AND And in add_user_task.php

    <?php
            require 'connection.php';

            $result = mysqli_query($con,"SELECT * FROM task where status='active'");
            while($row = mysqli_fetch_array($result))
                {
                    $taskid=$row['id'];
                        echo "<form class='form-horizontal' role='form' action='add_user_task.php' enctype='multipart/form-data' method='post'>";
                            echo "<div class='col-md-12'>";
                                echo"<h3>".$row['task_title']."</h3><br>";
                                    echo "<div class='col-md-9'>";
                                        echo "<p>".$row['task_desc']."</p>";
                                    echo "</div>";

                                    echo "<div class='col-md-1'></div>";
                                        echo "<div class='col-md-2'>";
                                            echo "<p>".$row['task_points']."</p>";
                                        echo "</div>";
                                    echo "</div>";

                                    echo "<p>&nbsp;</p>";
                                    echo "<div class='col-md-9'>";
                                        echo "<p> Please provide a verification link where ever required </p>";
                                    echo "</div>";

                                    echo "<p>&nbsp;</p>";
                                    echo "<div class='col-md-12'>";
                                        echo "<div class='col-md-10'>";
                                            echo "<label class='col-lg-3'> Link One:</label>";
                                                    echo "<input class='co-g-7' name='facebook_pro_link'>";
                                                    echo "<input class='co-g-7' name='task_id' value='" . $row['id'] . "'>";

                                        echo "</div>";
                                        echo "<div class='col-md-2'>";
                                            echo "<input type='checkbox' name='facebook_pro_complete' value='complete'>Completed<br>";
                                        echo "</div>";
                                    echo "</div>";
                                    echo "<center><input class='btn btn-primary' value='Save Changes' type='submit' name='submit'></center>";
                        echo "</form>";
                }
            ?>  



<?php
session_start(); 
require 'connection.php';
if($_POST)
    {
        // escape variables for security

        $facebook_pro_link = mysqli_real_escape_string($con, $_POST['facebook_pro_link']);
        $facebook_pro_complete = mysqli_real_escape_string($con, $_POST['facebook_pro_complete']);
        $task_id = mysqli_real_escape_string($con, $_POST['task_id']);

        $sql="INSERT INTO user_task (task_id,task_status_by_user,task_link) VALUES  ('$task_id','$facebook_pro_complete','$facebook_pro_link')";

        if (!mysqli_query($con,$sql)) 
            {
                die('Error: ' . mysqli_error($con));
            }
    }
?>