简体   繁体   English

如何解决未定义的偏移:php 中的错误

[英]how to solve Undefined offset: error in php

I have a html code where I create a table and two radio button on each row.我有一个 html 代码,我在其中创建了一个表格和每行两个单选按钮。 for giving different name to radio button on each row I used while loop and I++ way.为了给每一行的单选按钮赋予不同的名称,我使用了 while 循环和 I++ 方式。 that is那是

<form action = "submit_varification.php" method = "POST" onclick = "return validate()">

                        <div style="position: absolute; left: 50px; top: 90px;">
                        <label class="right"><font color="white">Date:</font></label>
                        <input type="text" id = "frmDate"  /><br>
                        <p id="date"></p>
                        </div>
                        <div style="position: absolute; left: 250px; top: 91px;">
                        <label class="right"><font color="white">V-ID:</font></label>
                        <input type="text" id = "myText" name = "reviewer" value = ""/><br>
                        </div>
                        <div style="position: absolute; left: 900px; top: 91px;">
                        <button type="button" name="show" id="show" onclick = "" >History</button>
                        </div>
                        <div style="position: absolute; left: 900px;">
                        <input type="submit" name="test" id="test" value="Submit"  /><br/>
                        </div>

                    <script>
                    var date = new Date();

                    document.getElementById("frmDate").value = (date.getMonth() + 1) + '/' + date.getDate() + '/' + date.getFullYear();

                    </script>
                </body>
                <table style="position: absolute; width:95%; left: 20px; top: 150px;" id = "table1">
                    <?php
                    $myDate = date('m/d/Y');
                    ?>
                        <tr>
                        <th>Date</th>
                        <th>Time</th> 
                        <th>Alias</th>
                        <th>Machine_Name</th>
                        <th>Build_Name</th>
                        <th>Build_version</th>
                        <th>WinDBG</th>
                        <th>.Net_Framework</th>
                        <th>Status</th>
                        </tr>
                        <?php
                            //get records from database
                            $sql3 = "SELECT * FROM data WHERE `Date` = '".$myDate."' ORDER BY id DESC";
                            $query = $conn->query($sql3);
                            if($query->num_rows > 0){
                            $i = 0 ;
                            while($row = $query->fetch_assoc()){ ?>
                            <tr> 
                            <td><?php echo $row['Date']; ?></td>
                            <td><?php echo $row['Time']; ?></td>
                            <td><?php echo $row['Alias']; ?></td>
                            <td><?php echo $row['Machine_Name']; ?></td>
                            <td><?php echo $row['Build_Name']; ?></td>
                            <td><?php echo $row['Build_Version']; ?></td>
                            <td><?php echo $row['WinDBG']; ?></td>
                            <td><?php echo $row['.NET_Framework']; ?></td> 
                            <td style='white-space: nowrap'><form><label class = "ready"><input type="radio" name="[<?php $i ?>]" value = "Ready">Ready</label><label class = "notready"><input type="radio" name="[<?php $i ?>]" value = "Not Ready" >Not Ready</label></form></td>
                            </tr> 

                            <?php $i++ ; } } ?>


                        </table>


                        </form>

and I want to store the value of each row to a database.我想将每一行的值存储到数据库中。 so I used POST method on the form.所以我在表单上使用了 POST 方法。 and I am trying to take the value of radio button to database but I am facing a error like我正在尝试将单选按钮的值输入数据库,但我遇到了类似的错误

 Notice: Undefined offset: 0 in C:\wamp64\www\submit_varification.php on line 33
Notice: Undefined offset: 1 in C:\wamp64\www\submit_varification.php on line 33
Notice: Undefined offset: 2 in C:\wamp64\www\submit_varification.php on line 33

and the PHP code:和 PHP 代码:

$query = $conn->query($sql4);
                        if($query->num_rows > 0){
                        $i = 0 ;    
                        while($row = $query->fetch_assoc()){


                            //...insert into your DB.
                        $row1 = $row["Date"];
                        $row2 = $row["Time"];
                        $row3 = $row["Alias"];
                        $row4 = $row["Machine_Name"];
                        $row5 = $row["Build_Name"];
                        $row6 = $row['Build_Version'];
                        $row7 = $row["WinDBG"];
                        $row8 = $row[".NET_Framework"];

                        $status = $_POST["$i"]; // this is the error


                        $sql5 = "INSERT INTO history (`Date`, `Time`, `Alias`, `Machine_Name`, `Build_Name`, `Build_version`, `WinDBG`, `.NET_Framework`, `Status`, `Reviewed_By`) 
                        VALUES ('".$row1."','".$row2."','".$row3."','".$row4."','".$row5."','".$row6."','".$row7."','".$row8."', '".$status."', '".$reviewer."') ";
                        if ($conn->query($sql5) === TRUE) {
                        //echo "New record created successfully";
                        //echo nl2br("\n");
                        echo "";

                        } else {
                        echo "Error: " . $sql5 . "<br>" . $conn->error;
                        }

                        $i++ ; 
                        }
                        }   

is anyone have any solution for this?有人对此有任何解决方案吗?

Dump the array that populates $i for each loop.转储为每个循环填充 $i 的数组。 I would wager the array does not have the array key that the name="[<?php $i?>]" is expecting.我敢打赌该数组没有name="[<?php $i?>]"所期望的数组键。

Can you remove the brackets in the name attribute and see if its working correct.您能否删除名称属性中的括号并查看其是否正常工作。 Below is the code:下面是代码:

<td style='white-space: nowrap'>
<form><label class = "ready">
    <input type="radio" name="<?php $i ?>" value = "Ready">Ready</label>
    <label class = "notready"><input type="radio" name="<?php $i ?>" value = "Not Ready" >Not Ready</label>
</form>

Plus it would be better if you have some static text appended to the name column and in server side, you can loop it:另外,如果您在名称列和服务器端附加一些 static 文本会更好,您可以循环它:

<td style='white-space: nowrap'>
<form><label class = "ready">
    <input type="radio" name="status_<?php $i ?>" value = "Ready">Ready</label>
    <label class = "notready"><input type="radio" name="status_<?php $i ?>" value = "Not Ready" >Not Ready</label>
</form>

Let me know if this helps..让我知道这是否有帮助..

You make nested forms.您制作嵌套的 forms。 replace the line换行

<td style='white-space: nowrap'><form><label class = "ready"><input type="radio" name="[<?php $i ?>]" value = "Ready">Ready</label><label class = "notready"><input type="radio" name="[<?php $i ?>]" value = "Not Ready" >Not Ready</label></form></td>

with this:有了这个:

<td style='white-space: nowrap'><label class = "ready"><input type="radio" name="<?php $i ?>" value = "Ready">Ready</label><label class = "notready"><input type="radio" name="<?php $i ?>" value = "Not Ready" >Not Ready</label></td>

Also remove the brackets, like gopal panadi mentioned.还要删除括号,就像提到的 gopal panadi 一样。
Furthermore I am not sure if you can have input tags inside labels.此外,我不确定您是否可以在标签添加输入标签。 shouldn't they be next to each other?他们不应该挨着吗?

Edit: after that if it fails, dump the $_POST array and see how/if the radio values are in $_POST.编辑:之后如果失败,转储 $_POST 数组并查看单选值如何/是否在 $_POST 中。 var_dump($_POST);die();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM