错误“未定义的索引”与PHP中的表单

时间:2014-04-16 20:51:19

标签: php mysql

我有这个代码,我有两个问题 一个是这一行:

  

注意:未定义的索引:第65行的C:\ xampp \ htdocs \ art-legend \ 12 \ edit \ index.php中的elm1

我本地的第65行是在这一行的这一行 ($ editor = $ _POST ['elm1'];)

和另一个错误:当我提交代码时,转到没有页面(找不到对象!),这是同一页面中的表单代码:

<form method="post" action="<? echo $PHP_SELF; ?>">

                <div>
                <textarea id="elm1" name="elm1" rows="15" cols="80" style="width: 80%">
            ....
                </textarea>
            </div>
            <br />
        <input type="submit" value="gooooo" name="submit" />

    </form>
<?

$localhost = "localhost";
$user = "root";
$password ="adminpass"; 
$db = "im";

$connect = mysqli_connect($localhost,$user,$password,$db);
$editor = $_POST['elm1'];

$sql = "insert into images(name) values('$editor')";
$query = mysqli_query($connect,$sql);

$sql2 = "select images.name from images";
$query2 = mysqli_query($connect,$sql2);

while ($row = mysqli_fetch_array($query2,MYSQLI_ASSOC)){
    echo $row['name'];

    }

?>

此代码中的行

$editor = $_POST['elm1'];

2 个答案:

答案 0 :(得分:1)

试试这个:

<?php

$localhost = "localhost";
$user = "root";
$password ="adminpass"; 
$db = "im";


$connect = mysqli_connect($localhost,$user,$password,$db);

if(isset($_POST['elm1'])){ //This line
$editor = $_POST['elm1'];
$sql = "insert into images(name) values('".mysqli_real_escape_string($connect, $editor)."')";
$query = mysqli_query($connect,$sql);
}

$sql2 = "select images.name from images";
$query2 = mysqli_query($connect,$sql2);

while ($row = mysqli_fetch_array($query2,MYSQLI_ASSOC)){
echo $row['name'];

}



?>

答案 1 :(得分:0)

首次加载页面时,不会有帖子变量。尝试将代码包装在内:

 if ($_SERVER['REQUEST_METHOD'] == 'POST') {

 }

另请阅读How can I prevent SQL injection in PHP?