在$ _POST中使用传递的变量从$ _GET数据库中获取

时间:2017-01-11 19:19:08

标签: php mysql

以下是我要做的事情 - 我在MySql表中有一个AUTO-INC列,即$job_numb。我以为我可以将变量与表单操作一起传递,并在表单操作文件中选择它。

以下是表单操作代码:

 <form method="post" action="form_action2.php?attachment1=<?=$attachment1;?>, job_numb=<?=$job_numb;?>" enctype="multipart/form-data">

      <div class="form-group">
           <label for="due_date">Show Date</label><br>
           <input type="date" class="form-control" style="max-width: 200px;" id="show_date" name="show_date" value="<?php echo date("n/j/Y", strtotime("$show_date"))?>">
      </div>

      <button type="submit" class="btn btn-primary" value="submit" name="submit" id="submit">Update Record</button>
 </form>

然后我想使用这些变量来选择UPDATE语句中的行,这样我 a)可以使用$job_numb选择正确的记录 b)使用同时在脚本中执行mail操作。

我知道$_POST[]$_GET[]是两个不同的功能,但这似乎对我有意义 - 开发人员如何做到这一点。

$_POST的脚本:

 <?php
      echo($_GET['job_numb']);
      echo($_GET['attachment1']);

      $servername = "localhost";
      $username = "XXXXXX";
      $password = "XXXXXX";
  try {
      $conn = new PDO("mysql:host=$servername;dbname=jobs_users", $username, $password);
      // set the PDO error mode to exception
     $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
     echo "Connected successfully";

      if(isset($_POST['submit']) && !empty($_POST['submit'])) {
            $job_numb = $_POST['job_numb'];
            $job_name = $_POST['job_name'];
            $job_name_clean = htmlspecialchars($job_name, ENT_QUOTES);
            $comments = $_POST['comments'];
            $comments_clean = htmlspecialchars($comments, ENT_QUOTES);
            $attachment2=$_POST['attachment2'];
            $due_date=$_POST['due_date'];
            $show_date=$_POST['show_date'];
            $AE=$_POST['AE'];
            $status=$_POST['status'];
            $assignee=$_POST['assignee'];
            $assign_email=$_POST['assign_email'];
            $property=$_POST['property'];
            }

      if(isset($_FILES['attachment2'])){
            $errors= array();
            $file_name = $_FILES['attachment2']['name'];
            $file_size = $_FILES['attachment2']['size'];
            $file_tmp = $_FILES['attachment2']['tmp_name'];
            $file_type = $_FILES['attachment2']['type'];
            $file_ext=strtolower(end(explode('.',$_FILES['attachment2']['name'])));

            $expensions= array("jpeg","jpg","png","doc","pdf","docx","tif","tiff");

            if(in_array($file_ext,$expensions)=== false){
                  $errors[]="extension not allowed, please choose a JPEG, PNG, PDF, DOC, DOCX or TIF file.";
  }

  if ( ! $errors ) {
     move_uploaded_file($file_tmp,"uploads/".$file_name);
     echo "Success";
     echo $file_name;
  }else{
     print_r($errors);
  }
    }

 $newdate = date("Y-m-d",strtotime($due_date));
 $showdate = date("Y-m-d",strtotime($show_date));
 $sql = "UPDATE `jobs_canjobs` SET `job_name`='$job_name_clean',`comments`='$comments_clean',`attachment2`='$file_name',`due_date`='$newdate', `show_date`='$showdate',`status`='$status',`assignee`='$assignee',`assign_email`='$assign_email',`AE`='$AE',`Property`='$property' WHERE job_numb = $job_numb";

  $conn->exec($sql);
     }
  catch(PDOException $e)
     {
  echo "Connection failed: " . $e->getMessage();
     }
  $conn = null;
 ?>

我知道变量会传递给执行文件,但我无法将它们添加到整个操作中。我错过了一步?我没有做正确的事吗?

我还想指出,我觉得我的代码很不稳定,我知道我没有使用预备语句。它会变得更好,而且会越来越好 - 我读到并不断练习。谢谢。

1 个答案:

答案 0 :(得分:0)

我将此添加到顶部,并能够提取$url字符串:

 $attachment1 = $_GET['attachment1'];
 $job_numb = $_GET['job_numb'];

我还必须编辑表单操作:

<form method="post" action="form_action2.php?attachment1=<?=$attachment1;?>&job_numb=<?=$job_numb;?>" enctype="multipart/form-data">

就是这样。