-1

I have this code and I have two problem the one is this line:

Notice: Undefined index: elm1 in C:\xampp\htdocs\art-legend\12\edit\index.php on line 65

line 65 in my local is in this page this line ( $editor = $_POST['elm1'];)

and the other error: when I submitted the code go to no page (Object not found!) and this is the form code in the same page:

<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'];

    }

?>

the line in this code

$editor = $_POST['elm1'];
Cœur
  • 37,241
  • 25
  • 195
  • 267
Foda
  • 58
  • 1
  • 7

2 Answers2

1

Try this:

<?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'];

}



?>
Hackerman
  • 12,139
  • 2
  • 34
  • 45
0

First time the page is loaded there will be no post variables. Try wrapping your code inside:

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

 }

Also read about How can I prevent SQL injection in PHP?

Community
  • 1
  • 1
andrew
  • 9,313
  • 7
  • 30
  • 61