PHP和MySQL中的变量索引未定义

时间:2018-01-20 07:46:48

标签: php mysql

您好我是PHP和MySQL的新手,我已经绞尽脑汁想出我做错了什么。

有人可以查看我的代码吗?

我理解错误,但我似乎无法找到解决方案。

  

注意:未定义的索引:cid in   /Applications/XAMPP/xamppfiles/htdocs/ibelievein/editcomments.php on   第19行

environment.inc.php:

<?php
include'dbh.inc.php';

function setComments($conn) { 
if(isset($_POST['commentSubmit'])) { 


   $uid = $_POST['uid'];
   $date = $_POST['date'];
   $message = $_POST['message'];

    $sql = "INSERT INTO environments (uid, date, message) VALUES 
('$uid','$date', '$message' )";

    $result = mysqli_query($conn, $sql); 
}


}

function getComments($conn) { 
$sql = "SELECT * FROM environments"; 
$result = mysqli_query($conn, $sql);
while($row = $result->fetch_assoc() ) {
        echo "<div><p>";
        echo $row['uid']."<br>";
        echo $row['date']."<br>";
        echo nl2br($row['message']);
        echo "</p>
            <form method = 'POST' action='editcomments.php'> 
                <input type = 'hidden' name = 'cid' value = ' 
".$row['cid']."'>
                <input type = 'hidden' name = 'uid' value = 
'".$row['uid']."'>
                <input type = 'hidden' name = 'date' value = 
'".$row['date']."'>
                <input type = 'hidden' name = 'message' value = 
'".$row['message']."'>
                <button>Edit</button> 
            </form> 
        <div>";


}

}

editcomments.php:

<?php
date_default_timezone_set("America/New_York"); 
include'dbh.inc.php';
include'environment.inc.php';
 ?> 

<!DOCTYPE html>
<html lang="">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title></title>
<link rel = "stylesheet" type = "text/css" href = "style.css">
</head>

<body>
<?php
   $cid = $_POST['cid'];
   $uid = $_POST['uid'];
   $date = $_POST['date'];
   $message = $_POST['message'];
echo "<form method = 'POST' action = '".editComments($conn)."'>
    <input type = 'hidden' name = 'uid' value = '".$uid."'>
    <input type = 'hidden' name = 'date' value = '" .$date."'>
    <textarea name ='message'>".$message."</textarea>
    <button type = 'submit' name ='commentSubmit'>Edit</button>
</form>";
?>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

尝试:

$cid = isset($_POST['cid']) ? $_POST['cid'] : "";
$uid = isset($_POST['uid']) ? $_POST['uid'] : "";
$date = isset($_POST['date']) ? $_POST['date'] : "";
$message = isset($_POST['message']) ? $_POST['message'] : "";