【问题标题】:something wrong with array updating multiply results数组更新乘法结果有问题
【发布时间】:2016-10-04 13:42:02
【问题描述】:

我有一个表,每个表有 10 条记录和 2 个输入。 我为它发了帖子并使用了数组,但我做错了但不知道是什么。我想做的是一次更新很多行! 我还在学习php数组

发布

    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>

【问题讨论】:

  • 如果您能准确解释问题所在,会更容易为您提供帮助。但我能看到的一件事是,您在代码中明确设置了$i = 1,所以只有那一行会得到更新。
  • 它不做任何事情。没有错误,也没有保存数据
  • 更具体。您的 PHP 代码是否被调用?如果是这样,请检查传递给它的参数是什么——它们应该是什么?为您的代码添加一些调试,以查看您生成的查询是什么样的 - 它们看起来好吗?当您直接在数据库中尝试它们时它们会运行吗?
  • 我回答了我的问题!谢谢大家帮忙:)

标签: php arrays mysqli


【解决方案1】:

终于搞定了:)

代码:

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

【讨论】:

    【解决方案2】:

    我认为您不必将“workid”、“workday”和“worknight”名称属性作为输入标签中带有键的数组。我认为这就是问题所在。试试&lt;input name="workid[]" .. /&gt;,或&lt;input name="workday[]".. /&gt;。并且 $row['worker'] 的 name 属性中也没有任何内容可以阻止您获取值。

    同时使用:

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

    处理该表格而不是:

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

    【讨论】:

      【解决方案3】:

      如果没有记录与

      id = $workcid[1]

      那么mysql没有什么要更新的,这没有错误。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2020-09-10
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多