插入语句不起作用,未定义变量

时间:2018-01-18 21:09:17

标签: php

我试图在php中创建一个工作更新页面。我相信我的语法没有问题,但我一直收到错误"未定义的变量"对于我在输入字段中输入的值。请帮忙!

<?php
  require("connect.php");

  if (isset($_POST['submit'])) {
    $udpdateQuery = "UPDATE league SET `LeagueName` = :name, `LeagueCountry` = :country, `LeagueSize` = :size WHERE `LeagueID` = :leagueID";
    $stmt3 = $con->prepare($udpdateQuery);
    $stmt3->execute(array('name' => $_POST['l_name'], 'country' => $_POST['l_country'], 'size' => $_POST['l_size']));
    $stmt4->closeCursor();
  }

  if (isset($_GET['LeagueID'])) {
    $valuesQuery = "SELECT `LeagueName`, `LeagueCountry`, `LeagueSize` FROM league WHERE `LeagueID` = :leagueID";
    $stmt4 = $con->prepare($valuesQuery);
    $stmt4->execute(array('leagueID' => $_GET['LeagueID']));
    $result = $stmt4->fetchAll();
    $stmt4->closeCursor();

    foreach ($result as $r) {
      $name = $r['LeagueName'];
      $country = $r['LeagueCountry'];
      $size = $r['LeagueSize'];
    }
  }
  ?>
<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8">
    <title>League</title>
    <link rel="stylesheet" type="text/css" href="css/style.css">
    <link href="https://fonts.googleapis.com/css?family=Roboto:300,400,500,700" rel="stylesheet">
  </head>
  <body>
        <div class="wrapper">
        <form class="add" action="" method="post">
          <label>League Name</label>
          <input type="text" name="l_name" value="<?=$name?>" required><br>

          <label>League Country</label>
          <input type="text" name="l_country" value="<?=$country?>" required><br>

          <label>League Size</label>
          <input type="text" name="l_size" value="<?=$size?>" required><br>

          <input id="submit" name="submit" type="submit" value="Submit">
        </form>

        <?php include('home.php'); ?>

      </div>
      <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.1/jquery.min.js"></script>
      <script type="text/javascript" src="resources/js/main.js"></script>
  </body>
</html>

2 个答案:

答案 0 :(得分:0)

第一次运行代码时,它只会进入$_GET部分。 提交后,它只会进入$_POST部分。这就是它显示错误的原因,因为没有定义变量。

你需要:

  1. 为您的LeagueID添加隐藏字段:

    <input type="hidden" name="l_id" value="<?=$id?>"><br>
    
  2. 修正您的更新:

    $stmt3->execute(array('name' => $_POST['l_name'], 'country' => $_POST['l_country'], 'size' => $_POST['l_size'], 'leagueID' => $_POST['l_id']));
    
  3. 根据获取或发布的内容正确创建变量:

    if (isset($_POST['submit'])) {
        $id = $_POST['l_id'];
        $name = $_POST['l_name'];
        $country = $_POST['l_country'];
        $size = $_POST['l_size'];
    
    ...
    
    if (isset($_GET['LeagueID'])) {
    ...
        foreach ($result as $r) {
            $id = $_GET['LeagueID'];
            $name = $r['LeagueName'];
            $country = $r['LeagueCountry'];
            $size = $r['LeagueSize'];
        }
    

答案 1 :(得分:0)

看起来你可能会遇到一些问题。

  1. 在您的第一个if语句中,您正在关闭错误的光标:

    $stmt4->closeCursor();
    

    这个尚未定义;这应该是$stmt3

  2. 表单中的变量$name$country$size仅在第二个if块中定义(对于$_GET,< em> if 有一个LeagueID集合并且有结果())。如果这些都不是真的,那么变量就不会被定义 - 当你提交表格时,两者都不是真的。

    为了帮助防止这种情况,请在if块之外定义它们:

    $name = '';
    $country = '';
    $size = '';
    if (isset($_GET['LeagueID'])) {
    

    然后,更新您的表单,以便在提交时传递LeagueID

    <form action="/path?LeagueID=<?= htmlentities($_GET['LeagueID']) ?>" method="post" class="add">