PHP更新页面 - 信息/图像未更新

时间:2017-01-28 00:48:15

标签: php mysql file-upload sql-update

我不知道为了让它发挥作用我需要做些什么。在单击更新按钮后,此页面应更新联系人的信息。它将id号输入页面,它从数据库中读取信息并将其作为值放入输入中。然后根据需要更新输入,并将其更新到服务器。但由于某种原因,它没有执行Update语句,也没有任何变化。它还应该能够上传图像并使其成为更新的一部分。

<?php

    session_start();

    $_SESSION['message'] = '<div class="message">Your contact has been updated</div>';

    $dbh = new PDO('mysql:host=localhost;dbname=ssl;port=8889', 'root', 'root');

    $contactid = $_GET['id'];

    $stmt=$dbh->prepare("SELECT * FROM contacts where contactid = :contactid");
    $stmt->bindParam(':contactid', $contactid);
    $stmt->execute();
    $result = $stmt->fetchAll();

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

      $firstname = $_POST['firstname'];
      $lastname = $_POST['lastname'];
      $phone = $_POST['phone'];
      $email = $_POST['email'];
      $address = $_POST['address'];
      $city = $_POST['city'];
      $state = $_POST['state'];

      $target_file = "uploads/" . basename($_FILES['picture']['name']);
      $picture = $_FILES['picture']['name'];

      if (move_uploaded_file($_FILES['picture']['tmp_name'], $target_file)) {
          echo "<div class='message2'>File Uploaded.</div>";
      }
      //SQL statement to update data
      $stmt=$dbh->prepare("UPDATE contacts SET firstname='" . $firstname . "',
                                               lastname='" . $lastname . "',
                                               phone='" . $phone . "',
                                               email='" . $email . "',
                                               address='" . $address . "',
                                               city='" . $city . "',
                                               state='" . $state . "',
                                               picture='" . $picture . "',
                                              WHERE contactid='" . $contactid . "';");
      $stmt->execute();

      echo "<div class='message'>Your contact has been added!</div>";

      header("location: index.php");
      die();
    }
 ?>

<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Update</title>
        <link rel="stylesheet" href="css/style.css">
    </head>
    <body>
         <nav>
           <h1>Update A Fruit</h1>
         </nav>
         <form id="form" action="index.php" method="post">
             <p>Edit the following:</p> <br />
             First Name: <input type="text" name="firstname" value="<?php echo $result[0]['firstname']; ?>" /><br />
             Last Name: <input type="text" name="lastname" value="<?php echo $result[0]['lastname']; ?>" /><br />
             Phone: <input type="text" name="phone" value="<?php echo $result[0]['phone']; ?>" /><br />
             Email: <input type="text" name="email" value="<?php echo $result[0]['email']; ?>" /><br />
             Address: <input type="text" name="address" value="<?php echo $result[0]['address']; ?>" /><br />
             City: <input type="text" name="city" value="<?php echo $result[0]['city']; ?>" /><br />
             State: <select id="state" type="text" name="state" value="">
                                                 <option value="" selected>Select a State</option>
                                                 <option value="AL">Alabama</option>
                                                 <option value="AK">Alaska</option>
                                                 <option value="AZ">Arizona</option>
                                                 <option value="AR">Arkansas</option>
                                                 <option value="CA">California</option>
                                                 <option value="CO">Colorado</option>
                                                 <option value="CT">Connecticut</option>
                                                 <option value="DE">Delaware</option>
                                                 <option value="DC">District Of Columbia</option>
                                                 <option value="FL">Florida</option>
                                                 <option value="GA">Georgia</option>
                                                 <option value="HI">Hawaii</option>
                                                 <option value="ID">Idaho</option>
                                                 <option value="IL">Illinois</option>
                                                 <option value="IN">Indiana</option>
                                                 <option value="IA">Iowa</option>
                                                 <option value="KS">Kansas</option>
                                                 <option value="KY">Kentucky</option>
                                                 <option value="LA">Louisiana</option>
                                                 <option value="ME">Maine</option>
                                                 <option value="MD">Maryland</option>
                                                 <option value="MA">Massachusetts</option>
                                                 <option value="MI">Michigan</option>
                                                 <option value="MN">Minnesota</option>
                                                 <option value="MS">Mississippi</option>
                                                 <option value="MO">Missouri</option>
                                                 <option value="MT">Montana</option>
                                                 <option value="NE">Nebraska</option>
                                                 <option value="NV">Nevada</option>
                                                 <option value="NH">New Hampshire</option>
                                                 <option value="NJ">New Jersey</option>
                                                 <option value="NM">New Mexico</option>
                                                 <option value="NY">New York</option>
                                                 <option value="NC">North Carolina</option>
                                                 <option value="ND">North Dakota</option>
                                                 <option value="OH">Ohio</option>
                                                 <option value="OK">Oklahoma</option>
                                                 <option value="OR">Oregon</option>
                                                 <option value="PA">Pennsylvania</option>
                                                 <option value="RI">Rhode Island</option>
                                                 <option value="SC">South Carolina</option>
                                                 <option value="SD">South Dakota</option>
                                                 <option value="TN">Tennessee</option>
                                                 <option value="TX">Texas</option>
                                                 <option value="UT">Utah</option>
                                                 <option value="VT">Vermont</option>
                                                 <option value="VA">Virginia</option>
                                                 <option value="WA">Washington</option>
                                                 <option value="WV">West Virginia</option>
                                                 <option value="WI">Wisconsin</option>
                                                 <option value="WY">Wyoming</option>
                                             </select><br>
             <input type="file" name="picture" value="<?php echo $result[0]['picture']; ?>"><br />
             <input type="submit" name="submit" value="Update">
         </form>
         <a href="index.php">Go Back</a>

         <pre>
           <?php var_dump($st); ?>
         </pre>
         <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.1.0/jquery.min.js"></script>
         <script src="https://ajax.googleapis.com/ajax/libs/jqueryui/1.12.1/jquery-ui.min.js"></script>
         <script>

             $('.message').fadeIn(500).delay(3000).fadeOut(600);

        </script>
    </body>
</html>

1 个答案:

答案 0 :(得分:0)

您需要在代码中更改一些内容,例如:

  • 按以下方式更改action的{​​{1}},

    <form>
  • 除非您在action="index.php?id=<?php echo $contactid; ?>" 中加入enctype="multipart/form-data",否则您无法上传包含表单的任何文件。

    因此,基于以上两个陈述,您的<form>元素将如下所示:

    <form>
  • <form id="form" action="index.php?id=<?php echo $contactid; ?>" method="post" enctype="multipart/form-data"> 准备好的陈述中使用占位符。

  • UPDATE操作之前执行UPDATE操作 ,否则您将无法在SELECT字段中获取更新值你更新它们。