如何将当前项目的ID传递给请求表

时间:2019-06-24 08:00:43

标签: php mysql database

我有一个应用程序,用户可以在其中将请求编辑发送给管理员,现在的问题是如何将请求资产的ID从user_asset表存储到请求表中,以便我可以将其显示在管理员页面上,并提供完整的详细信息资产

当用户单击请求编辑时,他会得到一个带有可编辑字段的表单,其中填充了当前信息,但是我如何存储此资产的 id ,这样我就可以将两者的信息都提取到管理员的表中表((用户资产,请求)

我有 user_asset表

asset_id
asset_category
code
title
userid

和请求表

id
reason
assetid
user_id

这是我到目前为止所做的

    if(isset($_POST['submit'])){

    // get all values from input with no special charactere

   $code = mysqli_real_escape_string($conn, $_POST['code']);

   $asset_id =  mysqli_real_escape_string($conn, $_GET['id']); 
   $reason =  mysqli_real_escape_string($conn, $_POST['reason']);

    if (!$error) {


      if (!$error) {
         // execute the sql insert 
            if(mysqli_query($conn, "INSERT INTO `requests`(id,reason,assetid, user_id) 
                 VALUES( null, '" . $reason . "', '". $asset_id ."','" .$_SESSION['user_id'] . "')")) {
              // if the insert result was true (OK)
              $success_message = "req was successfully added ! ";
           } else {
              // if the insert result was false (KO)
              $error_message = "Error in data...Please try again later!";
           }
        }
   }


}

else{
   if(isset($_GET['idedit']) ){
       $result = mysqli_query($conn, "SELECT * from user_asset WHERE asset_id=".$_GET['idedit']);
       $project = mysqli_fetch_array($result);
   }

}

   ?>

这是我的表格

            <form method="post" action="req_ade.php" id="adding_new_assets">


                                    <div class="control-group">
                                       <label  for="basicinput">الکود : </label>
                                       <div class="controls">
                                          <input type="number" id="basicinput" value="<?php echo $project['code']; ?>" placeholder="الكود" name="code" class="span8">
                                       </div>
                                    </div>

                                    <div class="control-group">
                                       <label  for="basicinput">التفاصيل : </label>
                                       <div class="controls">
                                          <input type="text" id="basicinput" value="<?php echo $project['title']; ?>" placeholder="التفاصيل" name="title" class="span8">
                                       </div>
                                    </div>

                                    <div>
                             <label style="color:black">السبب</label>
                             <textarea rows="8" cols="8" name="reason" class="form-control" placeholder="اذكر سبب التعديل ..." ></textarea>

                            </div>

                                    <div class="control-group">
                                       <div class="controls">
                                          <button type="submit" name="submit" class="btn">طلب تعديل</button>
                                       </div>
                                    </div>
                                 </form>

这些是我得到的错误

Notice: Undefined index: id in D:\wamp64\www\Caprabia-test\req_ade.php on line 28

Fatal error: Uncaught exception 'mysqli_sql_exception' with message 'Incorrect integer value: '' for column 'assetid' at row 1' in D:\wamp64\www\Caprabia-test\req_ade.php on line 37

( ! ) mysqli_sql_exception: Incorrect integer value: '' for column 'assetid' at row 1 in D:\wamp64\www\Caprabia-test\req_ade.php on line 37

2 个答案:

答案 0 :(得分:0)

注意:未定义索引:第28行的D:\ wamp64 \ www \ Caprabia-test \ req_ade.php中的ID

$ _ GET数组中没有“ id”。因此,您的$ asset_id变量将为空,并且空字符串不是有效的整数。您应该在查询中添加(int)

mysqli_query($conn, "INSERT INTO `requests`(id,reason,assetid, user_id) 
             VALUES( null, '" . $reason . "', '". (int)$asset_id ."','" .$_SESSION['user_id'] . "')")

或者最好在使用$ _GET数组之前检查一下。像这样:

If(isset($_GET['id'])) 
{
     $asset_id =  mysqli_real_escape_string($conn, $_GET['id']);
}
else 
{
     ...
}

答案 1 :(得分:0)

感谢您的所有建议。

尝试了很多建议并使用了代码后,我找到了解决方案。

if(isset($_POST['submit'])){

   // get all values from input with no special charactere

  $code = mysqli_real_escape_string($conn, $_POST['code']);

  $asset_id =  mysqli_real_escape_string($conn, $_POST['asset_id']); 
  $reason =  mysqli_real_escape_string($conn, $_POST['reason']);

   if (!$error) {


     if (!$error) {
        // execute the sql insert 
           if(mysqli_query($conn, "INSERT INTO `requests1`(id,reason,assetid, user_id) 
                VALUES( null, '" . $reason . "', '". $asset_id ."','" .$_SESSION['user_id'] . "')")) {
             // if the insert result was true (OK)
             $success_message = "req was successfully added ! ";
          } else {
             // if the insert result was false (KO)
             $error_message = "Error in data...Please try again later!";
          }
       }
  }


}

else{
   if(isset($_GET['idedit']) ){
       $result = mysqli_query($conn, "SELECT * from user_asset WHERE asset_id=".$_GET['idedit']);
       $project = mysqli_fetch_array($result);
   }

}

这是我以隐藏类型发布 asset_id 的表格

  <form method="post" action="req_ade1.php" id="adding_new_assets">


                                    <div class="control-group">
                                       <label  for="basicinput">الکود : </label>
                                       <div class="controls">
                                       <input type="hidden" value="<?php echo $project['asset_id'];?>" name="asset_id" />
                                       <input type="number" id="basicinput" value="<?php echo $project['code']; ?>" placeholder="الكود" name="code" class="span8">
                                       </div>
                                    </div>

                                    <div class="control-group">
                                       <label  for="basicinput">التفاصيل : </label>
                                       <div class="controls">
                                          <input type="text" id="basicinput" value="<?php echo $project['title']; ?>" placeholder="التفاصيل" name="title" class="span8">
                                       </div>
                                    </div>

                                    <div>
                             <label style="color:black">السبب</label>
                             <textarea rows="8" cols="8" name="reason" class="form-control" placeholder="اذكر سبب التعديل ..." ></textarea>

                            </div>

                                    <div class="control-group">
                                       <div class="controls">
                                          <button type="submit" name="submit" class="btn">طلب تعديل</button>
                                       </div>
                                    </div>
                                 </form>