显示基于在同一表单上选中的复选框的数据库中的值

时间:2017-03-18 08:42:15

标签: javascript php mysql database checkbox

我正在尝试根据选中的复选框填充数据库中名字的值。

下面是表单,它填充数据库中添加的所有表数据。通过选中任何一个复选框并单击更新,它应显示一个警告,显示从该行id的数据库查询的第一个名称,然后重定向到另一个页面

我尝试使用method = get in form。

现在我被困在如何修复我得到的以下错误:

<Undefined index: users on line 98>

这是我的代码

<!DOCTYPE html>
<!--[if lt IE 7 ]><html class="ie ie6" lang="en"> <![endif]-->
<!--[if IE 7 ]><html class="ie ie7" lang="en"> <![endif]-->
<!--[if IE 8 ]><html class="ie ie8" lang="en"> <![endif]-->
<!--[if (gte IE 9)|!(IE)]><!--><html lang="en"> <!--<![endif]-->
<head>
   <style>
        html {
            background-image: url("WDF_2493417.jpg");
            background-size: 100% 100%;
            background-repeat: no-repeat;
            background-position: center;
        }

        html {
            margin: 0;
            padding: 0;
            height: 100%;
        }
    </style>
</head>
<body>
<?php
    // php populate html table from mysql database
    $conn = mysqli_connect("localhost","root","");
    mysqli_select_db($conn, "my_db");
    $result = mysqli_query($conn,"SELECT * FROM ijob ORDER BY userId DESC");
?>

    <div class="form-style-1">
        <form name="frmUser" method="post" action="">
            <div style="width:1200px;">
                <table border="0" cellpadding="10" cellspacing="1" width="1200" class="tblListForm" align="center">
                    <tr class="listheader">
                        <td></td>
                        <th>first name</th>
                        <th>last name</th>
                        <th></th>
                    </tr>
                    <?php
                        $i=0;
                        $j=1;
                        while($row = mysqli_fetch_array($result)) {
                            if($i%2==0){
                                $classname="evenRow";
                            } else {
                                 $classname="oddRow";
                            }
                    ?>
                            <tr class="<?php if(isset($classname)) { echo $classname; }?>">
                                <td>
                                    <input type="checkbox" name="users" value="<?php echo $row["userId"]; ?>" >
                                </td>
                                <td><?php echo $row["fname"]; ?></td>
                                <td><?php echo $row["lname"]; ?></td>
                                <th colspan="4">
                                    <input type="button" name="update" class="button" value="Apply" onClick="checkAddress(this);" />
                                </th>
                            </tr>
                    <?php 
                            $i++;
                            $j=$i+1;
                        }
                        $conn->close();
                    ?>
                </table>
            </div>
        </form>
    </div>
</body>
<script>
    function checkAddress()
     {
        if (document.querySelector('form[name="frmUser"] input[type="checkbox"]:checked')) {

            if(!empty($_POST['users'])){
            <?php
                $servername = "localhost";
                $username = "root";
                $password = "";
                $dbname = "my_db";

                // Create connection
                $conn = new mysqli($servername, $username, $password, $dbname);
                // Check connection
                if ($conn->connect_error) {
                    die("Connection failed: " . $conn->connect_error);
                } 
                $num_var= " ";
                $num_var = mysqli_real_escape_string($conn,$_POST['users']); // getting error on this Line.

                $query = "SELECT fname FROM ijob WHERE userId='$num_var'";
                $result = mysqli_query($conn,$query);
                if (!$result) {
                    echo 'MySQL Error: ' . mysqli_error($conn);
                    exit;
                }

                if ($result->num_rows > 0) {
                    // output data of each row
                    while($row = $result->fetch_assoc()) {
                        $fnames[] = $row['fname'];
            ?>
            alert("Hello <?php echo $fnames;?>");
            <?php
                    }
                }
                $conn->close();

            ?>
            }

            document.frmUser.action = "form.php";
            document.frmUser.submit();
        } else {
            alert("Pl select checkbox which You wish to Apply ");
        }
    }
</script>
</html>

1 个答案:

答案 0 :(得分:1)

<script></script>中的PHP开始和结束标记不在正确的位置。 1.在此if语句后面有一个PHP开始标记(<?php

if(!empty($_POST['users'])){
   <?php

应该在if语句之前

<?php
if(!empty($_POST['users'])){
  1. 您的PHP结束标记(?>)位于上一个}

    之前 ?

    &GT;
    }