Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/298.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
PHP PDO MYSQL-使用内爆()更新列中的单元格具有多个值,使用旧值获取新值_Php_Mysql - Fatal编程技术网

PHP PDO MYSQL-使用内爆()更新列中的单元格具有多个值,使用旧值获取新值

PHP PDO MYSQL-使用内爆()更新列中的单元格具有多个值,使用旧值获取新值,php,mysql,Php,Mysql,我使用PDO UPDATE,我试图更新一个列有多个值,我使用implode()函数在值之间进行分离,但是更新后的值添加了新值和旧值,这变成了重复值。数据库中的列quali_id:BC,MSc <?php $pdo = new PDO("mysql:host=localhost;dbname=myDB",'username','pass'); // Define variables and initialize with empty values $qual = ""; $qual_err

我使用PDO UPDATE,我试图更新一个列有多个值,我使用implode()函数在值之间进行分离,但是更新后的值添加了新值和旧值,这变成了重复值。数据库中的列
quali_id
BC,MSc

<?php
$pdo = new PDO("mysql:host=localhost;dbname=myDB",'username','pass');
// Define variables and initialize with empty values
$qual = "";
$qual_err ="";
// Processing form data when form is submitted
if (isset($_POST["id"]) && !empty($_POST["id"])) {
  // Get hidden input value
  $id = $_POST["id"];
  // Validate Qualification
  $input_qual = ($_POST["qual"]);
  if (empty($input_qual)) {
      $qual_err = "Please enter the Qualification amount.";
  } else {
      $qual = implode('، ', $input_qual);
  }
  // Check input errors before inserting in database
  if (empty($qual_err) && empty($spec_err) && empty($asse_err)) {
      // Prepare an update statement
//$sql_clear = "INSERT INTO application_hr (id, qualif_id) VALUES (:id,:qual) ON DUPLICATE KEY UPDATE id=:id, qualif_id=:qual";
      $sql_update = "UPDATE application_hr SET qualif_id=:qual  WHERE id=:id";
      if ($stmt = $pdo->prepare($sql_update)) {
          // Bind variables to the prepared statement as parameters
          $stmt->bindParam(":qual", $qual);
          $stmt->bindParam(":id", $id);
          // Attempt to execute the prepared statement
          if ($stmt->execute()) {
              header("location: index.php");
              exit();
          } else {
              echo "Something went wrong. Please try again later.";
          }
      }
      // Close statement
      unset($stmt);
  }
} else {
  // Check existence of id parameter before processing further
  if (isset($_GET["id"]) && !empty(trim($_GET["id"]))) {
      // Get URL parameter
      $id = trim($_GET["id"]);
 // Prepare a select statement
      $sql = "SELECT * FROM application_hr WHERE id = :id";
      if ($stmt = $pdo->prepare($sql)) {
          // Bind variables to the prepared statement as parameters
          $stmt->bindParam(":id", $param_id);
          // Set parameters
          $param_id = $id;
          // Attempt to execute the prepared statement
          if ($stmt->execute()) {
              if ($stmt->rowCount() == 1) {
                  /* Fetch result row as an associative array. Since the result set contains only one row, we don't need to use while loop */
                  $row = $stmt->fetch(PDO::FETCH_ASSOC);
                  //retrieve from DB to array variable using explode function;
                  $qual = explode('، ', $row["qualif_id"]);
                  print_r($qual);
              } else {
                  // URL doesn't contain valid id. Redirect to error page
                  header("location: error.php");
                  exit();
              }
          } else {
              echo "Oops! Something went wrong. Please try again later.";
          }
      }
      // Close statement
      unset($stmt);
  } else {
      // URL doesn't contain id parameter. Redirect to error page
      header("location: error.php");
      exit();
  }
}
?> 

从可以使用的阵列中删除重复项。在你的情况下,你应该在你的内爆声明之前就这样做

$input_qual = array_unique($input_qual);
$qual = implode(',', $input_qual);

在update语句之前,
qual
中有什么作为值?
qual
=Array([0]=>BC[1]=>MSc)的问题是,您希望在
$qual
中每个项目有一个DB行(例如一行
quali\u id
='BC'和一行
quali\u id
='MSc'),或者您希望一行的
quali_id
='BC,MSc,PhD'只包含任何给定字符串的一个实例?
$input_qual = array_unique($input_qual);
$qual = implode(',', $input_qual);