gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 02:20:47 26 4
gpt4 key购买 nike

当我的变量被定义时,我总是收到一个 undefined variable 错误。我尝试了 Stack Overflow 的多种不同建议,但没有一个对我有用。此页面的目的是编辑 PHP 表格中的内容。

我曾尝试以本网站上建议的不同方式重新定义我的变量。例如,我尝试为“myID”添加“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();

我的期望是能够将 PHP 表更改为用户通过单击提交按钮输入的输入。但如果用户未输入任何内容,也不会更改输入。

最佳答案

您正在定义 function():

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

您的函数无法在函数体外部看到变量 $myID。函数可以看到变量,如果变量定义在函数体中:

funciton changeForm() {
$myId = 1;
}

或者您正在从另一个作用域传输到函数参数:

$myId = 1;

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

changeForm($myId);

我建议您阅读有关 variable scope 的文章

关于php - 我收到一个 undefined variable ,但我的变量是在从另一个页面接收主变量 (myID) 时定义的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57815664/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com