简体   繁体   English

我收到一个未定义的变量,但是我的变量是在从另一个页面接收主变量(myID)的同时定义的

[英]I am receiving an undefined variable but my variables are defined while receiving main variable (myID) from another page

I keep getting an undefined variable error when my variable has been defined. 定义变量后,我不断收到未定义的变量错误。 I have tried multiple different suggestions from Stack Overflow but none of them are working for me. 我尝试了来自Stack Overflow的多种不同建议,但是没有一个对我有用。 The purpose of this page is to edit content in a PHP table. 该页面的目的是编辑PHP表中的内容。

I have tried redefining my variable in different ways suggested on this website. 我尝试以本网站建议的不同方式重新定义变量。 For example, I've tried adding "mysql_fetch_object" for "myID". 例如,我尝试为“ myID”添加“ mysql_fetch_object”。 I also have tried using a new variable to define "myID" ($newID = mysql_fetch_object($myID)) . 我还尝试使用新变量定义“ ($newID = mysql_fetch_object($myID))($newID = mysql_fetch_object($myID))

<?php
// Server credentials
$servername = "***";
$username = "***";
$password = "***";
$dbname = "***";

// Creating mysql connection
$conn = new mysqli($servername, $username, $password, $dbname);

// Checking mysql connection
if ($conn->connect_error) {
  die("Connection failed: " . $conn->connect_error);
}

// Writing a mysql query to retrieve data
// ERROR HERE
$myID = $_GET['Event_id'];


$sql = "SELECT Event_id, title, event_date, location, description, status FROM events WHERE Event_id = $myID";
$result = $conn->query($sql);

//if ($result->num_rows == 1) {
// ERROR HERE
//if ($result) {
  // Show each data returned by mysql
  while($row = $result->fetch_assoc()) {




      if(isset($_POST['submitc'])) {
          changeform();
      } else{
          header('location: events.php');
      }

          function changeform(){
          $title= mysqli_real_escape_string($conn, $_POST['title']);
          $event_date = mysqli_real_escape_string($conn, $_POST['event_date']);
          $location = mysqli_real_escape_string($conn, $_POST['location']);
          $description = mysqli_real_escape_string($conn, $_POST['description']);

         if(!empty($title)) {
             mysqli_query($conn, "UPDATE events SET title = '$title' WHERE Event_id = '$myID'");
         } else{

         }

          if(!empty($event_date)) {
              mysqli_query($conn, "UPDATE events SET event_date = '$event_date' WHERE Event_id = '$myID'");
          } else{

          }
          if(!empty($location)) {
              mysqli_query($conn, "UPDATE events SET location = '$location' WHERE Event_id = '$myID'");
          } else{

          }

          if(!empty($description)) {
              mysqli_query($conn, "UPDATE events SET description = '$description' WHERE Event_id = '$myID'");
          } else{

          }
      }


?>

<html lang="en">

<head>
    <meta charset="utf-8">
    <title>I491 MakeUp</title>
    <link rel="stylesheet" href="eventcss.css">
</head>
<header>

    <div id="text">

<!-- script for header animation-->
    <script class="logo" type="text/javascript">
    var i=0, text;
    text = "I491 Make-Up"

    function typing(){
        if(i<text.length){
            document.getElementById("text").innerHTML += text.charAt(i);
            i++;
            setTimeout(typing,50);
        }
    }
    typing();
</script>
</div>

</header>

<body>

    <div class="edit_part">
    <form method="post" action="editevent.php">
    <p> Title: <?php echo $row["title"]; ?>
        <input type="text" placeholder="Title" name="title" value="<?php echo $title; ?>">
    </p>
<p> Date: <?php echo $row["event_date"]; ?>
    <input type="text" placeholder="Date" name="date" value="<?php echo $event_date; ?>">
</p>

<p> Location: <?php echo $row["location"]; ?>
    <input type="text" placeholder="Location" name="location" value="<?php echo $location; ?>">
</p>

<p>
Description: <?php echo $row["description"]; ?>
    <input type="text" placeholder="Description" name="description" value="<?php echo $description; ?>">
</p>

<button type="submit" name="submitc" value="1-or-anything">
     Submit Changes
 </button>
</form>

</div>

</body>


<?php
  }
 //}
//  else {
//   echo "0 results";
// }


// Closing mysql connection
$conn->close();

My expectation is to be able to change the PHP table to the input entered by the user by clicking the submit button. 我的期望是能够通过单击提交按钮将PHP表更改为用户输入的输入。 But also not changing the input if the user enters nothing. 但是,如果用户什么也不输入,也不要更改输入。

You're defining function() : 您正在定义function()

function changeform() {
    //  Your func body, where yo're using $myID now
}

Your function can't see variable $myID outside body. 您的函数在体外看不到变量$myID Functions can see variables, if variable was defined in function body: 如果变量在函数主体中定义,则函数可以查看变量:

funciton changeForm() {
    $myId = 1;
}

OR you are transmitting from another scope into function parameters: 或者,您正在从另一个范围传输到函数参数:

$myId = 1;

function changeForm($myId) {
    $myId++;
}

changeForm($myId);

I recommend you to read the article about variable scope 我建议您阅读有关变量范围的文章

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

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