-1

Okay, I changed my code for a few reasons which are irrelevant, but now I need your help again, since I'm very close on finishing one of first ''bigger'' projects in php and mysql, and I've changed my code to this:

<?php

if (isset($_POST['submitted'])) {


}

include('connect-with-mysql.php'); 

$Test1 = $_POST['test1']; 
$Test2 = $_POST['test2']; 
$sqlinsert = "INSERT INTO vanille"



?>
<html>
<head>
<title>Datenbank</title>
</head>
<body>

<h1>Überschrift</h1>
<form method="post" action="action.php">
<input type="hidden" name="submitted" value="true" />
<fieldset>
 <legend>New Ppl</legend>
 <label>Test1:<input type="text" name="test1" /></br>
 <label>Test2:<input type="text" name="test2" /></br>
</fieldset>
<br />
<input type="submit" value="add new informations" />    

</form>

</body>
</html>

EDIT:

Here's the content of the connect-with-mysql.php file:

<?php

DEFINE ('DB_USER', 'root'); 
DEFINE ('DB_PSWD', ''); 
DEFINE ('DB_HOST', 'localhost'); 
DEFINE ('DB_NAME', 'vanille'); 

$dbcon = mysqli_connect(DB_HOST, DB_USER, DB_PSWD, DB_NAME); 

?>

And then I'll get this error line:

Notice: Undefined index: test1 on line 11 (same with line 10)

DarkYagami
  • 93
  • 7
  • `$_POST` will only be available if anything posted. – Sougata Bose Oct 30 '15 at 10:14
  • Possible duplicate of [PHP: "Notice: Undefined variable" and "Notice: Undefined index"](http://stackoverflow.com/questions/4261133/php-notice-undefined-variable-and-notice-undefined-index) – gpinkas Oct 30 '15 at 10:29

2 Answers2

1

you have closed the if()

Your database query, is open for sql injection and more over you will get errors if you run this query.

<?php

 if (isset($_POST['submitted'])) {

 include('connect-with-mysql.php'); // 

     $Test1 = $_POST['test1']; 
     $Test2 = $_POST['test2']; 
     $sqlinsert = "INSERT INTO vanille"
 }

?>

Query should look like,

$sqlinsert = "INSERT INTO vanille (`test1`,`test2`) values ($Test1,$Test2)";

mysqli_query($dbcon,"INSERT INTO vanille (`test1`,`test2`) values ($Test1,$Test2)");

See this for syntax

I have shown the query for your reference, but make sure you use PDO

Niranjan N Raju
  • 12,047
  • 4
  • 22
  • 41
0

Once you request the page at initially stage it is not a post request. So, It will be get request and you are accessing array of $_POST which is not exist. So, you got this error.Check that post array is present then go for accessing it's index.

<?php

 if (!empty($_POST)) {

     include('connect-with-mysql.php'); 

     $Test1 = $_POST['test1']; 
     $Test2 = $_POST['test2']; 
     $sqlinsert = "INSERT INTO vanille"
 }

?>
sandeepsure
  • 1,113
  • 1
  • 10
  • 17