【问题标题】:when updating a column row from a table then its fetching in all row of that column从表中更新列行时,然后在该列的所有行中获取
【发布时间】:2017-11-21 05:44:33
【问题描述】:

我做了一个属性代码很少的表

<table class="table table-hover table-striped">
  <thead>
          <tr>
           <th>Id</th>
           <th>File-name</th>
           <th>Purpose</th>
           <th>Recieved-By </th>
           <th>Processed-By</th>
           <th>Adress</th>
          <th>Contact-No</th>
          <th>Date</th>
           <th>Update</th>
           </tr>`

这里是从数据库表中获取除更新之外的所有列中的数据

                  <tbody>
                       <?php
                            if ( $search )
                            {
                                $p_query = "select * from files where recieved_by like '%$search%' or processed_by like  '%$search%' or   purpose like  '%$search%' or file_name like '%$search%' order by id desc limit $page_start_from, $total_num_page";
                            } 
                            else 
                            {

                                $p_query = "select * from files order by id desc limit $page_start_from, $total_num_page";
                            }
                          $p_run=mysqli_query($con,$p_query);
                            if(mysqli_num_rows($p_run)){
                             while($row=mysqli_fetch_array($p_run))
                             {
                                $c_id=$row['id'];
                                $file=$row['file_name'];
                                $purpose=$row['purpose'];
                                $recieve=$row['recieved_by'];
                                $processed=$row['processed_by'];
                                $address=$row['address'];
                                $contact=$row['contact_no'];
                                $date=$row['date'];
                                $show_status=$row['show_status'];


                            ?>
                                            <tr>

                        <td><a  href="post.php?post_id=<?php echo $c_id?>"><?php echo $c_id;?></a></td>
                        <td><a href="post.php?post_id=<?php echo $c_id?>"><?php echo $file;?></a></td>
                        <td><a href="post.php?post_id=<?php echo $c_id?>"><?php echo $purpose;?></a></td>
                        <td><a href="post.php?post_id=<?php echo $c_id?>"><?php echo $recieve;?></a></td>
                        <td><a href="post.php?post_id=<?php echo $c_id?>"><?php echo $processed;?></a></td>
                        <td><a href="post.php?post_id=<?php echo $c_id?>"><?php echo $address;?></a></td>
                        <td><a href="post.php?post_id=<?php echo $c_id?>"><?php echo $contact;?></a></td>
                        <td><a href="post.php?post_id=<?php echo $c_id?>"><?php echo $date;?></a></td>

现在在更新列中,当 文件表数据库更新 列中有值时,它会显示在更新列中,否则它将打开一个 按钮的动作这里有一个表单代码

                                                <td>
                                                    <?php if( !empty($show_status)){
                                                           echo $show_status;
                                                                                }
                                                           else
                                                             {
                                                    ?>
                                                    <div class="btn-group">
                                                      <button type="button" class="btn btn-default dropdown-toggle" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" style=" padding-top: 3px; padding-bottom: 3px;">
                                                        Action <span class="caret"></span>
                                                      </button>
                                                      <ul class="dropdown-menu" style="margin-left:-50px;">
                  <?php 
                      if(isset($_POST['submit-form']))
                      {
                          $update_date=$_POST['reason-date'];
                          $status=$_POST['reason'];
                          $p_id=$_POST['idvalue'];
                          $d_query="INSERT INTO update_table (id, reason, update_date) VALUES ('$p_id', '$status', '$update_date'); update files set show_status ='  $status' where id=' $p_id'";
                    if(mysqli_multi_query($con,$d_query))
                    {
                        $msg="file have been submitted ";
                        header('location:index.php');
                    }
                    else
                    {
                        $error_msg="Already updated this file";
                    }


}?>
                         <script type="text/javascript">
                                      function checkvalue()
                                        {
                                              var ureason=document.forms["sform"]["ureason"].value;
                                              var udate=document.forms["sform"]["udate"].value;
                                            if (ureason=="")
                                            {
                                                alert("Status Field is blank ");
                                                return false;
                                            }
                                             if (udate=="")
                                            {
                                                alert("Date Field is blank ");
                                                return false;
                                            }


                                        }
                            </script>
                            <form role="banner" class="actionform" action="index.php" method="POST" name="sform"  onsubmit=" return checkvalue()">
                                <input type="hidden" name="idvalue" value="<?php echo $c_id; ?>">
                                 <label  style="font-weight: normal;">Enter Reason*</label><br>
                                <input  type="text" name="reason" placeholder="call or letter or filed " id="ureason"><br>
                                <label style="font-weight: normal;">Update Date:*</label><br>
                                <input  type="date" name="reason-date" placeholder="enter date here" id="udate"> 
                                <br><br>
                                <input type="submit" name="submit-form" id="actionid" value="Submit" style="background: #3596e0; ">
                       <?php
                   if(isset($error_msg))
                   {
                   echo "<span style='color:red; margin-bottom:10px;'  class='pull-right'>$error_msg</span>"; 
                       exit();
                   }
                   else if(isset($msg))
                   {
                       echo "<span style='color:green;' class='pull-right'>$msg</span>";
                       exit();
                   }?>
                            </form>
                                                      </ul>
                                                    </div>
                                                    <?php 
                                                        } ?>

                                                </td>
                                            </tr>
                                            <?php 
                                      }
                                    }
                                            else {
                                                 echo "<h3> NO Related Table is Found Here </h3>";
                                                }
                                   ?>
                                        </tbody>
                                    </table>

现在的问题是,当我第一次填写更新操作表单时,它运行正常,但是当我进行第二次或第三次操作时,上面的行更新功能未填写,它一直显示检查表单列的错误为空,如何停止这个,当我填写一行更新列时,它应该只影响那个特定的行,请有人帮助我,我在这部分停留了两天。

【问题讨论】:

  • 我认为您正在使用INSERT 方法为UPDATE,使用UPDATE 方法更新已插入的数据
  • 如果您不确定数据集是否已经在表中,请使用INSERT ... ON DUPLICATE KEY UPDATE
  • @Marcus 在提交表单时插入表名 update_table 并更新更新列表单 files table ,上面的行表单出现问题未填写,我正在尝试提交下面的行表单,它一直显示表单值为空,因为它也在检查上面的行表单。

标签: php html mysql


【解决方案1】:

由于你使用的是INSERT方法,所以使用UPDATE方法进行更新。

第一次因为没有数据存在,你的INSERT方法可以按你的意愿工作,但之后你必须使用UPDATE方法进行更新。

有关更新,请参阅https://www.w3schools.com/sql/sql_update.asp

有关插入,请参阅https://www.w3schools.com/sql/sql_insert.asp

【讨论】:

  • @S N Tiwari 在提交表单时插入表名 update_table 并更新更新列表单文件表,当上面的行表单没有填写并且我试图提交下面的行表单时出现问题显示表单值是空的,因为它也在检查上面的行表单
【解决方案2】:

这是因为您的表单值不是唯一的表单,所以我更改了一些表单值:

<form role="banner" class="actionform" action="test.php" method="POST" name="sform"  **onsubmit=" return checkvalue(<?php echo $c_id ?>**)">

<input  type="text" name="reason" placeholder="call or letter or filed " id="ureason_<?php echo $c_id ?>">
<input  type="date" name="reason-date" placeholder="enter date here" id="udate_<?php echo $c_id ?>"> 

现在所有 id 都具有唯一值,现在 javascript 代码将是:

<script type="text/javascript">
                                      function checkvalue(id)
                                        {
                                            var id = id;
                                            alert(id);
                                              var ureason=document.getElementById('ureason_'+id).value;

                                              var udate=document.getElementById('udate_'+id).value;

                                            if (ureason=="")
                                            {
                                                alert("Status Field is blank ");
                                                return false;
                                            }
                                             if (udate=="")
                                            {
                                                alert("Date Field is blank ");
                                                return false;
                                            }


                                        }
                            </script>

还有一个人认为您错过了更新表格文件

在您插入更新表时同时运行此查询:

$d_query=$db->query("INSERT INTO update_table (id, reason, update_date) VALUES ('$p_id', '$status', '$update_date')");
                          $d_query=$db->query("Update files set show_status='ok' where id='$p_id' ");

希望对你有帮助。

【讨论】:

    猜你喜欢
    • 2019-02-04
    • 1970-01-01
    • 2013-04-10
    • 2019-10-16
    • 1970-01-01
    • 2022-11-17
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多