-4

I have two PHP files that I have set up on an online web hosting platform in order to integrate the database into my android application. Whenever I run the URL : https://wounding-pat.000webhostapp.com/register.php in order to test if the register.php code is running correctly, I get the following messages on my browser.

Connection Successful

Notice: Undefined index: name in /storage/ssd2/158/3185158/public_html/register.php on line 5

Notice: Undefined index: email in /storage/ssd2/158/3185158/public_html/register.php on line 6

Notice: Undefined index: password in /storage/ssd2/158/3185158/public_html/register.php on line 7

Notice: Undefined index: subject in /storage/ssd2/158/3185158/public_html/register.php on line 8

Notice: Undefined index: counsellor in /storage/ssd2/158/3185158/public_html/register.php on line 9

Notice: Undefined variable: connection in /storage/ssd2/158/3185158/public_html/register.php on line 13

Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /storage/ssd2/158/3185158/public_html/register.php on line 13

Error

The following is my init.php code

<?php

$host = "localhost";
$database = "id3185158_csetestonlinedb";
$username = "id3185158_root";
$password = "admin";

//$con = mysqli_connect($host, $username, $password, $database);

if (mysqli_connect($host, $username, $password, $database)) {

    echo "Connection Successful";

} else {

    echo "Connection Unsuccessful";

}

?>

The following is my register.php code

<?php

require "init.php";

$name = $_POST["name"];
$email = $_POST["email"];
$password = $_POST["password"];
$subject = $_POST["subject"];
$counsellor = $_POST["counsellor"];

$sql_query = "insert into teachers values('$name', '$email', '$password', '$subject', '$counsellor');";

if (mysqli_query($connection, $sql_query)) {

    echo " Data Inserted";

} else {

    echo " Error";

}

?>

  • 2
    You should develop on a local development environment with error reporting turned on. init.php doesn't create the $connection variable you're trying to use in register.php. You can't access register.php directly without getting errors as it expects a post request (form data). You should be using prepared and parameterized queries, atm you're open to sql injection hacks. – JimL Oct 08 '17 at 08:56
  • 3
    That code is _wide open_ to sql injections. You need to fix that! Read about the benefits of using "prepared statements" in combination with "parameter binding" when setting up sql queries. – arkascha Oct 08 '17 at 08:58
  • 1
    About the issue at hand: are you sure you perform a http post request? If so, what does a dump of `$_POST` reveal as content? – arkascha Oct 08 '17 at 08:59
  • 2
    All the error strings are there which are self explanatory. You should debug it on your local env. first, if it works there and does not work on online hosting, that will be the real question. – Sachin G. Oct 08 '17 at 09:01
  • 1
    Are you trying with $_GET instead $_POST, or just use $_REQUEST for both – Niklesh Raut Oct 08 '17 at 09:02
  • 1
    **Danger**: "Not hashing at all" is [an unsuitable hashing algorithm](http://php.net/manual/en/faq.passwords.php); you need to [take better care](https://www.owasp.org/index.php/Password_Storage_Cheat_Sheet) of your users' passwords. – Quentin Oct 08 '17 at 09:05
  • This question has been answered soo many times. Definitely a duplicate. The notices you get are beyond self explanatory. A quick Google Search would have fixed this. Also it's very rude to post questions and not even reply to any comments posted. Now we feel like the OP here.. – Rotimi Oct 08 '17 at 09:07
  • 1
    Definite duplicate of [PHP: "Notice: Undefined variable", "Notice: Undefined index", and "Notice: Undefined offset"](https://stackoverflow.com/questions/4261133/php-notice-undefined-variable-notice-undefined-index-and-notice-undef) – Rotimi Oct 08 '17 at 09:10
  • Why did you comment out the connection? And your variablenames don't match what they are defined to when you attempt to use them. – Qirel Oct 08 '17 at 09:31
  • Never use free hosting to host your application you can try small cloud servers: https://www.cloudways.com/en/php-cloud-hosting.php – Shahroze Nawaz Sep 29 '18 at 08:33

2 Answers2

0
  1. If you are hitting this URL directly in browser address bar, which will be the GET method the code will not work. Try tools like postman to create POST method request to this URL along with the required params like email, password, subject, counsellor.
  2. There is no variable defined with name $connection in your register.php file, http://php.net/manual/en/function.mysqli-connect.php function returns the variable which you should set to $connection.
Sachin G.
  • 1,870
  • 19
  • 24
0

If you run the url in browser then it definitely shows the error because you are getting variable through post. It will only work if you send a post request to register file with all the parameters which you are getting through post.

Also try to use isset like that

  If (isset($_POST[`name`] )) {
$name=$_POST['name' ] ;
}

Also run your insert query when values are coming.

Abdul Rauf
  • 464
  • 6
  • 22