数组更新导致错误的结果

时间:2016-10-04 13:42:03

标签: php arrays mysqli

我的桌子上有10条记录和2条输入。 我为它做了帖子并使用了数组,但是我做错了什么但是无法弄清楚是什么。我想做的是什么时候更新很多行! 我还在学习php数组

POST

    if($source == 'hoursDayNight'){
    $value      = htmlentities($_POST['value'], ENT_QUOTES, "UTF-8");
    $workcid      = htmlentities($_POST['workcid'][$i], ENT_QUOTES, "UTF-8");
    $worknight      = htmlentities($_POST['worknight'][$i], ENT_QUOTES, "UTF-8");
    $workday      = htmlentities($_POST['workday'][$i], ENT_QUOTES, "UTF-8");
    include('inc.php');     

    //$current_dateedit = date('Y-m-d == H:i:s');
    $current_dateedit = date('Y-m-d');
$i=NULL;
$i=1;

    $queryie = "UPDATE works SET workday='".$workday[$i]."', worknight='".$worknight[$i]."', type='1'  WHERE id='" . $workcid[$i] . "'";
    mysqli_query($conn, $queryie);

}

和表

                        <form name="hoursDayNight" method="POST" action="" >
                <input type="hidden" name="source" value="hoursDayNight"/>
                    <table class="ui small sortable celled collapsing table">
                        <thead> 
                            <tr>
                                <th>Worker</th>
                                <th>Day h</th>
                                <th>Night h</th>
                            </tr>
                        </thead>
                    <tbody>';

            require('inc.php');     
            while ($row = mysqli_fetch_array($result)) {
                $sql = "SELECT DISTINCT * FROM works WHERE worker = '".$row['person_code']."'";
                    $i=null;
                    $i++;

                      echo '<tr>
                            <input type="text" name="" disabled value="' . $row['worker'] . '">
                                <input type="hidden" name="workcid['.$i.']" value="' . $row['id'] . '">

                            <input type="text" style="width: 25px; border: 1px solid #C8CFD7; height: 16px; line-height:14px; text-align: center; font-size:10pt; color:#817e7f;" name="workday['.$i.']" value="' . $row['workday'] . '">



                            <td>


                            <input type="text" style="width: 25px; border: 1px solid #C8CFD7; height: 16px; line-height:14px; text-align: center; font-size:10pt; color:#817e7f;" name="worknight['.$i.']" value="' . $row['worknight'] . '">

                            </div>

                            </form>

3 个答案:

答案 0 :(得分:0)

终于搞定了:)

代码:

if($source == 'hoursDayNight'){
$value      = htmlentities($_POST['value'], ENT_QUOTES, "UTF-8");
$records=htmlentities($_POST['records'], ENT_QUOTES, "UTF-8");
include('inc.php'); 
$i = null;      
while ($i <= $records){
$i++;
$workday = $worknight = $workcid = null;
$workcid      = htmlentities($_POST['workcid'][$i], ENT_QUOTES, "UTF-8");
$worknight      = htmlentities($_POST['worknight'][$i], ENT_QUOTES, "UTF-8");
$workday      = htmlentities($_POST['workday'][$i], ENT_QUOTES, "UTF-8");

$queryie = UPDATE works SET workday='".$workday[$i]."', worknight='".$worknight[$i]."', type='1'  WHERE id='" . $workcid[$i] . "'";
mysqli_query($conn, $queryie);

}
}

表:

                        <form name="hoursDayNight" method="POST" action="" >
        <input type="hidden" name="source" value="hoursDayNight"/>
                <table class="ui small sortable celled collapsing table">
                    <thead> 
                        <tr>
                        <th>Worker</th>
                        <th>Day h</th>
                        <th>Night h</th>
                        </tr>
                    </thead>
                <tbody>';


                                        require('inc.php');
                $i=null;                
                while ($row = mysqli_fetch_array($result)) {
                $sql = "SELECT DISTINCT f_name, l_name FROM n_employees WHERE navWorkerID = '".$row['person_code']."'";
                $query = mysqli_query($conn, $sql);
                $rs = mysqli_fetch_array($query, MYSQLI_ASSOC );
                if ($view){$workeris1="&worker=".$row['person_code']."";}else{$workeris1 = NULL;}

        echo '<tr>';




        $i++;                               

        <input type="hidden" name="records" value="'.$i.'"/>
        <input type="hidden" name="workcid['.$i.']" value="' . $row['id'] . '">

        <input type="text" style="width: 25px; border: 1px solid #C8CFD7; height: 16px; line-height:14px; text-align: center; font-size:10pt; color:#817e7f;" name="workday['.$i.']" value="' . $row['workday'] . '">
        <td>

        <input type="text" style="width: 25px; border: 1px solid #C8CFD7; height: 16px; line-height:14px; text-align: center; font-size:10pt; color:#817e7f;" name="worknight['.$i.']" value="' . $row['worknight'] . '">

        </div>

        </form>

对不起仍然学会把风格放在一切:D

答案 1 :(得分:-1)

我认为你不得不把工作日&#39;工作日&#39;和&#39; worknight&#39; name属性作为在输入标记中包含键的数组。我认为这就是问题所在。尝试<input name="workid[]" .. /><input name="workday[]".. />之类的内容。此外,$ row [&#39; worker&#39;]的name属性中没有任何内容可以阻止您获取该值。

也可以使用:

if(isset($_POST['hoursDayNight'])){ 
$i=0;
//process form
$++;
}

处理表格的内容:

if($source == 'hoursDayNight'){
//process form
}

答案 2 :(得分:-1)

如果没有

的记录
  

id = $ workcid [1]

然后mysql没有任何更新,这没有错误。