0

I'm trying to write a php code (I'm at entry level in php).

<html>
<body>

<?php 
session_start();

$_SESSION['Name'] = $_POST['Name'];

$_SESSION['Email'] = $_POST['Email'];

$_SESSION['Password']= $_POST['Password'];

$con = mysqli_connect("localhost","root"," ","Details");
if(!$con)
{
    echo("hello");
}
mysqli_select_db( $con,"Details" );
$value = isset($_POST['Name']) ? $_POST['Name'] : '';
$value1 = isset($_POST['Email']) ? $_POST['Email'] : '';
$value2 = isset($_POST['Password']) ? $_POST['password'] : '';
$sql = "insert into details1 ( Name, Email, Password ) values ( '$Name', '$Email',     '$Password' )";

echo "1 record is added";
mysqli_close($con); 
 ?>

I'm getting the following notices:

Notice: Undefined index: Name in C:\wamp\www\newlogin\data1.php on line 7

Notice: Undefined index: Email in C:\wamp\www\newlogin\data1.php on line 9

Notice: Undefined index: Password in C:\wamp\www\newlogin\data1.php on line 11

Notice: Undefined variable: Name in C:\wamp\www\newlogin\data1.php on line 22

Notice: Undefined variable: Email in C:\wamp\www\newlogin\data1.php on line 22

Notice: Undefined variable: Password in C:\wamp\www\newlogin\data1.php on line 22
1 record is added

How to resolve these notices?

codemania
  • 1,098
  • 1
  • 9
  • 26
brainReader
  • 145
  • 2
  • 11

1 Answers1

0

How to resolve these notices?

Easy. Code to make sure errors are caught.

First you are not checking if the $_POST value is actually set when setting $_SESSION so I have reworked your code to factor that in as well. I also optimized it so the code rolls through an array of $_POST values and sets the session appropriately.

Then in the MySQL area you are setting $value, $value1 & $value2 but then you are using $Name, $Email & $Password. Why? Just set those to be $Name, $Email & $Password. I have also set that to roll through an array of $_POST values to set values via $$post_value.

<?php 
session_start();

// Set an array of post values.    
$post_array = array('Name','Email','Password');

// Roll through the array of post values and set the session as appropriate.
foreach ($post_array as $post_value) {
  $_SESSION[$post_value] = isset($_POST[$post_value]) ? $_POST[$post_value] : null;
}

$con = mysqli_connect("localhost","root"," ","Details");
if(!$con) {
  echo("hello");
}
mysqli_select_db( $con,"Details" );

// Roll through the array of post values and set the session as appropriate.
foreach ($post_array as $post_value) {
  $$post_value = isset($_POST[$post_value]) ? $_POST[$post_value] : null;
}

$sql = "insert into details1 ( Name, Email, Password ) values ( '$Name', '$Email',     '$Password' )";

echo "1 record is added";
mysqli_close($con); 
?>
Giacomo1968
  • 25,759
  • 11
  • 71
  • 103
  • How about checking the return value of `session_start`, because I bet you it's returning `false` here – Elias Van Ootegem May 21 '14 at 12:12
  • @EliasVanOotegem Why? The original poster is setting the values of `$_SESSION` and not reading them. Overall the code has many issues only the original poster can tackle so there’s just so much I can do to help. – Giacomo1968 May 21 '14 at 12:16