1

I am not able to connect to my database. Here's my code could you please help me out.

<?php

error_reporting(E_ALL); 
ini_set('display_errors', 1);

define('DB_HOST', 'mysql.hostinger.in');
define('DB_NAME', 'u25*****41_hari');
define('DB_USERNAME', 'u25*****41_hari');
define('DB_PASSWORD', 'hariharan');

$link = mysqli_connect(DB_HOST, DB_USERNAME,    DB_PASSWORD);

if (!$link) {
     die('Could not connect line 9');
}

$DB_SELECT = mysqli_select_db(DB_NAME, $link);

if (!$DB_SELECT) {
        die('Could not connect line 15');
}

$valueone = $_POST['Name'];
$valuetwo = $_POST['Username'];
$valuethree = $_POST['Password'];
$valuefour = $_POST['Mobile_Number'];

$sqlone = "INSERT INTO Account (Name) VALUES ('$valueone')";
$sqltwo = "INSERT INTO Account (Username) VALUES ('$valuetwo')";
$sqlthree = "INSERT INTO Account (Password) VALUES ('$valuethree')";
$sqlfour = "INSERT INTO Account (Mobile_Number) VALUES ('$valuefour')";

if (!mysql_query($sqlone) || !mysql_query($sqltwo) || !mysql_query($sqlthree) || !mysql_query($sqlfour) || !mysql_query($sqlfive)) {
     die('Could not connect name line 33');
}


mysql_close();
?>

This is the error I get:

Warning: mysqli_select_db() expects parameter 1 to be mysqli, string given in /home/u256114841/public_html/login.php on line 17 Could not connect line 15

cryptic_star
  • 1,863
  • 3
  • 26
  • 47

2 Answers2

7

Foreword: Make sure that all your form element name attributes match the POST arrays and that your database uses the correct types for the data input and their lengths.


In mysqli, the connection comes first.

you have

$DB_SELECT = mysqli_select_db(DB_NAME, $link);

which should read as

$DB_SELECT = mysqli_select_db($link, DB_NAME);

Read the manual:

But... you would be better off using all 4 parameters all in one go:

$link = mysqli_connect(DB_HOST, DB_USERNAME, DB_PASSWORD, DB_NAME);

while getting rid of $DB_SELECT = mysqli_select_db($link, DB_NAME);

However, you are mixing MySQL APIs with mysql_ functions.

Those different APIs do not intermix.

Consult the following on Stack:

You also did not post your HTML form to go with your POST arrays.

Make sure that your form does use a POST method and that your inputs all hold the same name attribute.

N.B.: $_POST['Name'] with name="name" will fail, if that is what your form element is named as and the same for all the other inputs. Those are case-sensitive.

Add error reporting to the top of your file(s) which will help find errors.

<?php 
error_reporting(E_ALL);
ini_set('display_errors', 1);
mysqli_report(MYSQLI_REPORT_ERROR | MYSQLI_REPORT_STRICT);

// rest of your code

Sidenote: Displaying errors should only be done in staging, and never production.

Additionally, your code is prone to an SQL injection. Use a prepared statement.

You don't need to use seperate queries like that.

Just add the columns and values all in one go.

$sqlone = "INSERT INTO Account (Name, Username, Password, Mobile_Number) 
            VALUES ('$valueone', '$valuetwo', '$valuethree', '$valuefour')";

Using mysqli_real_escape_string() for all your values.

I.e.:

$valueone = mysqli_real_escape_string($link, $_POST['Name']);

and doing the same for the others.

I also noticed that you may be storing passwords in plain text. This is not recommended.

Use one of the following:

Other links:


Additional references:

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
  • Y'all might want to point out the `mysql_query` part; and he's using INSERT in a rather - ah - ideosyncratic manner. – andrewsi Oct 28 '15 at 19:13
  • @andrewsi yeah I just spotted that now. – Funk Forty Niner Oct 28 '15 at 19:14
  • @andrewsi yeah.... what a crazy method to do an insert. I'll add an additional note. – Funk Forty Niner Oct 28 '15 at 19:16
  • I think he wants to add all those items to the same record. Unless he has a very odd database structure, which I admit I'm not going to rule out. – andrewsi Oct 28 '15 at 19:19
  • @andrewsi Their code is funky. I thought those were different tables. My eyes are spinning a bit. I'll make an additional edit. Thanks mate. – Funk Forty Niner Oct 28 '15 at 19:20
  • You're probably overdue for a trip to Tim's. – andrewsi Oct 28 '15 at 19:22
  • @andrewsi hahahaha! wayyy overdue. – Funk Forty Niner Oct 28 '15 at 19:25
  • 1
    Wow, that's become a comprehensive answer. My only regret is that I have but one up-vote to give. – andrewsi Oct 28 '15 at 19:26
  • i get this.. Undefined index: Mobile in /home/u256114841/public_html/login.php on line 26. Though my form has "Mobile" as the name. Either '0' or some garbage value like this '2147483647' gets inserted. – Asritha Mallemala Oct 29 '15 at 05:36
  • @AsrithaMallemala make sure your form's input element matches the one you have for `$_POST['Mobile_Number']` - is not the same as `name="Mobile"` lettercase and everything must match; or change `$_POST['Mobile_Number']` to `$_POST['Mobile']`. Plus, you need to change the column length for it to `varchar` and using a higher number for the length. the 2147483647 number means that you've reached the limit of the column's type being most likely an `int`. Make sure that all your elements match and the columns match the types. I wrote everything down in my answer. Please read it carefully. – Funk Forty Niner Oct 29 '15 at 12:39
0

The correct is

mysqli_select_db ( mysqli $link , string $dbname )

You are putting

mysqli_select_db ( string $dbname , mysqli $link )

Fix this and tell me if worked. For more info, see docs: http://php.net/manual/pt_BR/mysqli.select-db.php

rafaelbpa
  • 451
  • 6
  • 12