简体   繁体   English

需要有关使用PDO在PHP中更新表单的帮助

[英]Need an help for Updating a form in PHP using PDO

All columns are in TEXT format apart from the BDid column which is in int. 除int的BDid列外,所有列均采用TEXT格式。 Ive had the code running smoothly with un-preared statements but thought I would update it, with the same data in and the same table. Ive的代码使用未声明的语句可以平稳运行,但我想我将使用相同的数据和相同的表对其进行更新。

No errors are returned, but no rows are affected. 不返回任何错误,但不影响任何行。

The code is not getting any error but the MySQL is not updating 代码没有收到任何错误,但是MySQL没有更新

<?php 

  require 'database.php';

  $id = null;
  if ( !empty($_GET['id'])) {
    $id = $_REQUEST['id'];
  }

  if ( null==$id ) {
    header("Location: index.php");
  }

  if ( !empty($_POST)) {
    // keep track validation errors with form fields
    $first_nameError = null;
    $sur_nameError = null;
    $companyError = null;
    $locationError = null;
    $supervisorError = null;
    $departmentError = null;
    $job_descriptionError = null;
    $hire_dateError =null;
    $emailError = null;
    $messageError = null;

    // keep track post values
    $first_name = $_POST['first_name'];
    $sur_name = $_POST['sur_name'];
    $company = $_POST['company'];
    $location = $_POST['location'];
    $supervisor = $_POST['supervisor'];
    $department = $_POST['department'];
    $job_description = $_POST['job_description'];
    $hire_date = $_POST['hire_date'];
    $email = $_POST['email'];
    $message = $_POST['message'];

    // validate input for the field
    $valid = true;
    if (empty($first_name)) {
      $first_nameError = 'Please enter Name';
      $valid = false;
    }
    if (empty($sur_name)) {
      $sur_nameError = 'Please enter Name';
      $valid = false;
    }
    if (empty($company)) {
      $companyError = 'Please selest Company';
      $valid = false;
    }
    if (empty($location)) {
      $locationError = 'Please enter Location';
      $valid = false;
    }
    if (empty($department)) {
      $departmentError = 'Please enter Department';
      $valid = false;
    }
    if (empty($job_description)) {
      $job_descriptionError = 'Please enter Job Description';
      $valid = false;
    }
    if (empty($hire_date)) {
      $hire_dateError = 'Please enter Hire Date';
      $valid = false;
    }
    if (empty($email)) {
      $emailError = 'Please enter Email Address';
      $valid = false;
    } else if ( !filter_var($email,FILTER_VALIDATE_EMAIL) ) {
      $emailError = 'Please enter a valid Email Address';
      $valid = false;
    }

    if (empty($message)) {
      $messageError = 'Please enter Message';
      $valid = false;
    }

    // update data  the field in the database
    if ($valid) {
      $pdo = Database::connect();
      $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
      $sql = "UPDATE employee  SET first_name = ?,sur_name = ?, company = ?, location = ?, supervisor = ?, 
      department= ?, job_description =  ?, hire_date = ?, email = ?, message = ? WHERE id = ?";
      $q = $pdo->prepare($sql);
      $q->execute(array($id, $first_name, $sur_name, $company, $location, $supervisor, $department,$job_description, $hire_date, $email, $message));
      Database::disconnect();
      header("Location: index.php");
    }
  } else {
    $pdo = Database::connect();
    $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
    $sql = "SELECT * FROM employee where id = ?";
    $q = $pdo->prepare($sql);
    $q->execute(array($id));
    $data = $q->fetch(PDO::FETCH_ASSOC);
    $first_name = $data['first_name'];
    $sur_name = $data['sur_name'];
    $company = $data['company'];
    $location = $data['location'];
    $location = $data['supervisor'];
    $department = $data['department'];
    $job_description = $data['job_description'];
    $hire_date = $data['hire_date'];
    $email = $data['email'];
    $message = $data['message'];
    Database::disconnect();
  }
?>

This my Html code 这是我的HTML代码

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <link   href="css/bootstrap.min.css" rel="stylesheet">
    <script src="js/bootstrap.min.js"></script>
</head>

<body>
    <div class="container">

          <div class="span10 offset1">
            <div class="row">
              <h3>Update a Customer</h3>
            </div>
        This is my form 

            <form class="form-horizontal" action="update.php?id=<?php echo $id?>" method="post">
               <div class="control-group <?php echo !empty($first_nameError)?'error':'';?>">
     <label class="control-label"></label>
        <div class="controls">
     <input name="first_name" required placeholder="First Name" 
         title="First Name" value="<?php echo !empty($first_name)?$first_name:'';?>">
      <?php if (!empty($first_nameError)): ?>
        <span class="help-inline"><?php echo $first_nameError;?></span>
          <?php endif; ?>
        </div>
    </div>

  <div class="control-group <?php echo !empty($sur_nameError)?'error':'';?>">
  <label class="control-label"></label>
      <div class="controls">
  <input name="sur_name" required placeholder="Sur Name" title="Sur Name" 
      value="<?php echo !empty($sur_name)?$sur_name:'';?>">
   <?php if (!empty($sur_nameError)): ?>
        <span class="help-inline"><?php echo $sur_nameError;?></span>
          <?php endif; ?>
    </div>
  </div>



 <div class="control-group <?php echo !empty($companyError)?'error':'';?>">
  <label class="control-label"></label>
      <div class="controls">
  <select name="company" class="company" id="company" style="company" 
    title="Company" required placeholder="Company" value="<?php echo !empty($company)?$company:'';?>">
     <option value="Select.." selected>Select....</option>
          <option>HM</option>
          <option>HRA</option>
          <option>HSG</option>
          <option>HAL</option>
          <option>HBN</option>
          <option>HFR</option>
          <option>HPL</option>
          <option>HRD</option>
          <option>HERS</option>
          <option>HPS</option>
          <option>HEA</option>
        </select>
   <?php if (!empty($companyError)): ?>
        <span class="help-inline"><?php echo $companyeError;?></span>
          <?php endif; ?>
    </div>
  </div>
   <div class="control-group <?php echo !empty($locationError)?'error':'';?>">
  <label class="control-label"></label>
      <div class="controls">
  <input name="location" required placeholder="Location" title="Location" 
         value="<?php echo !empty($location)?$location:'';?>">
   <?php if (!empty($locationError)): ?>
        <span class="help-inline"><?php echo $location;?></span>
          <?php endif; ?>
    </div>
  </div>

   <div class="control-group <?php echo !empty($supervisorError)?'error':'';?>">
  <label class="control-label"></label>
      <div class="controls">
  <input name="supervisor" required placeholder="Supervisor" 
    title="Supervisor" value="<?php echo !empty($supervisor)?$supervisor:'';?>">
   <?php if (!empty($supervisorError)): ?>
        <span class="help-inline"><?php echo $supervisor;?></span>
          <?php endif; ?>
    </div>
  </div>
  <div class="control-group <?php echo !empty($departmentError)?'error':'';?>">
  <label class="control-label"></label>
      <div class="controls">
  <select name="department" size="1" class="department" id="department"  style="department" 
            title="Department " required placeholder="Department" value="<?php echo !empty($department)?$department:'';?>">
            <option value="Select" selected>Select</option>
              <option value="FI">Finace</option>
              <option value="CO">Controlling</option>
              <option value="PP">Production Planning</option>
              <option value="SD">Sale &amp; Distribution &amp;Customer Relationship manager</option>
              <option value="WM">Warehouse Managerment</option>
              <option value="MM">Material Managerment</option>
              <option value="HR">Human Resources</option>
              <option value="QM">Quality Management</option>
              <option value="PM">Plant Maintenance</option>
              <option value="IT">Information Technology</option>
              <option value="BO">Back Office</option>
              <option value="SAP">Administrative Group SAP User</option>
              <option value="SAPKEY">Administrative Group SAP Key User</option>
              <option value="ADMIN">Administrative Group Administrators</option>
              <option value="MGMT">Management</option>
            </select>
   <?php if (!empty($departmentError)): ?>
        <span class="help-inline"><?php echo $department;?></span>
          <?php endif; ?>
    </div>
  </div>


      <div class="control-group <?php echo !empty($job_descriptionError)?'error':'';?>">
  <label class="control-label"></label>
      <div class="controls">
  <input name="job_description" required placeholder="Job Description" 
    title="Job Description" value="<?php echo !empty($job_description)?$job_description:'';?>">
   <?php if (!empty($job_descriptionError)): ?>
        <span class="help-inline"><?php echo $job_description;?></span>
          <?php endif; ?>
    </div>
  </div>
  <div class="control-group <?php echo !empty($hire_dateError)?'error':'';?>">
  <label class="control-label"></label>
      <div class="controls">
  <input name= "hire_date" type="date" required placeholder="Hire Date" 
    title="Hire Date" value="<?php echo !empty($hire_date)?$hire_date:'';?>">
   <?php if (!empty($hire_dateError)): ?>
        <span class="help-inline"><?php echo $hire_date;?></span>
          <?php endif; ?>
    </div>
  </div>
  <div class="control-group <?php echo !empty($emailError)?'error':'';?>">
  <label class="control-label"></label>
      <div class="controls">
  <input name="email" type="email" required placeholder="Email" 
    title="Email ID" value="<?php echo !empty($email)?$email:'';?>">
   <?php if (!empty($emailError)): ?>
        <span class="help-inline"><?php echo $email;?></span>
          <?php endif; ?>
    </div>
  </div><div class="control-group <?php echo !empty($messageError)?'error':'';?>">
  <label class="control-label"></label>
      <div class="controls">
  <textarea name="message" cols="20" rows="5" required placeholder="Message" 
    title="Comments"value="<?php echo !empty($message)?$message:'';?>"></textarea>
   <?php if (!empty($messageError)): ?>
        <span class="help-inline"><?php echo $messager;?></span>
          <?php endif; ?>
    </div>

this is the action 
  </div> <div class="form-actions">
              <button type="submit" class="btn btn-success">Update</button>
              <a class="btn" href="index.php">Back</a>
            </div>
          </form>
        </div>

    </div> <!-- /container -->
  </body>
</html>

it looks like you need to change the execute line to this: 看来您需要将执行行更改为此:

$q->execute(array($first_name, $sur_name, $company, $location, $supervisor, $department,$job_description, $hire_date, $email, $message, $id)); $ q-> execute(array($ first_name,$ sur_name,$ company,$ location,$ supervisor,$ department,$ job_description,$ hire_date,$ email,$ message,$ id));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM