Jump to content

Connecting to Database


spudly1987

Recommended Posts

I have the following code created, however when i run it it doesn't come back as saying connected successfully?

<?php

$host = "localhost";
$my_user = "root";
$my_password = "";
$my_db = "notes_db"

$con = mysqli_connect("localhost","my_user","my_password","my_db");

echo ("Connected Successfully");
?>
Link to comment
https://forums.phpfreaks.com/topic/293543-connecting-to-database/
Share on other sites

After modifying the php.ini did you restarted the Web server? (Apache or whatever)

 

You don't notice anything missing in this line?

$my_db = "notes_db"

 

And in this one.. Is that exactly what you wrote using string literals or your were trying to use the previously defined variables? (like $my_host or $my_user, etc.)

$con = mysqli_connect("localhost","my_user","my_password","my_db");

I use xampp and yes after i adjusted the php.ini file i closed out and reopened,

 

and i have corrected the errors, still does not show the "connected succesfully" echo

<?php

$host = "localhost";
$my_user = "root";
$my_password = "";
$my_db = "notes_db";

$con = mysqli_connect("$host","my_user","my_password","my_db"); 

echo ("Connected Successfully");

?>


define('DATABASE_HOST', 'localhost'); // normally it's called localhost:
define('DATABASE_NAME', 'notes_db');
define('DATABASE_USERNAME', 'root'); // usually it's called root:
define('DATABASE_PASSWORD', 'your password');

$con = new mysqli(DATABASE_HOST, DATABASE_USERNAME, DATABASE_PASSWORD, DATABASE_NAME);

/* check connection */
if (mysqli_connect_errno()) {
    printf("Connect failed: %s\n", mysqli_connect_error());
    exit();
}

 

Something is not adding up. Either your script will execute the echo and you'll get output, or PHP will stop earlier and issue an error or warning message.

I suspect it's the latter. Maybe you don't have mysqli installed. If you're not seeing a message then your php.ini settings are not set correctly.

 

Use phpinfo() to see what your error_reporting and display_errors settings are, and while you're there make sure it mentions mysqli.

Okay I have worked on some things and now i'm kinda stuck

 

first thing is i got my database connection working great

<?php
$hostname = "localhost";
$username = "root";
$password = "";
$dbname = "pc_notes";

// Create Connection
$con = mysql_connect($hostname, $username, $password, $dbname);

mysql_select_db("notes", $con);

echo "connected";
?>

when i run it with the url "localhost/notes/db_connection.php, it runs and says connected

 

I also created what is called an insert.php to get the data from the index.html to the database

 

here is the html code

<html>
<title>
PC Note Template
</title>
<head>
</head>
<body>
<form action="insert.php" method="post">
<label for="">Tech Name: <input type="text" name="techname" /><br><br>
<label for="">Date: <input type="date" name="date" /><br><br>
<input type="submit" name="submit" value="submit" />
</form>
</body>
</html>

and the below is the insert.php data

<?php
$sql="INSERT INTO notes (techname, date)
VALUES
('$_POST[techname]','$_POST[date]')";
?>

how ever none of the information is being submitted to the database

 

I also noticed that when i run the insert.php by its self i get the following two notices

 

http://localhost/notes/insert.php

 

Notice: Undefined index: techname in C:\xampp\htdocs\notes\insert.php on line 4

Notice: Undefined index: date in C:\xampp\htdocs\notes\insert.php on line 4

 

i did have it working at one point but it was inserting the data blank into the database, so this is where i'm at

 

any help would be great

I have updated my insert.php code

<?php

$selcted = mysql_select_db("notes");

$mysql ="INSERT INTO notes (techname, date) VALUES 
('$_POST[techname]','$_POST[date]')";

if(!mysql_query($mysql))
die(mysql_error());

echo "data inserted";

mysql_close(notes);


?>

and now i am getting the following errors

 

Notice: Undefined index: techname in C:\xampp\htdocs\notes\insert.php on line 6

Notice: Undefined index: date in C:\xampp\htdocs\notes\insert.php on line 6
No database selected

okay i think i have it set correctly

$hostname = "localhost";
$username = "root";
$password = "";
$dbname = "pc_notes";

// Create Connection
$con = mysql_connect($hostname, $username, $password, $dbname);
mysql_select_db("notes", $con);

$mysql ="INSERT INTO notes (techname, date) VALUES 
('$_POST[techname]','$_POST[date]')";
<?php
$hostname = "localhost";
$username = "root";
$password = "";
$dbname = "pc_notes";

// Create Connection
$con = mysql_connect($hostname, $username, $password, $dbname);
mysql_select_db("notes", $con);

$mysql ="INSERT INTO notes (techname, date) VALUES 
('$_POST[techname]','$_POST[date]')";

$var = '';

if (isset($var)) {
	
}

$a = "techname";
$b = "date";

var_dump(isset($a));
var_dump(isset($a, $b));

unset ($a);

var_dump(isset($a));   
var_dump(isset($a, $b));

$foo = NULL;
var_dump(isset($foo));
?>

okay i set this up and i get the results of

 

bool(true)

bool(true)

bool(false)

bool(false)

bool(false)

i've tried that but every time i switch to mysqli everything breaks on me again

<?php
$hostname = "localhost";
$username = "root";
$password = "";
$dbname = "pc_notes";

// Create Connection
$dbhandle = mysqli_connect($hostname, $username, $password, $dbname);
mysqli_select_db("notes", $dbhandle);

$mysqli ="INSERT INTO notes (techname, date) VALUES 
('$_POST[techname]','$_POST[date]')";

$var = '';

if (isset($var)) {
	
}

$a = "techname";
$b = "date";

var_dump(isset($a));
var_dump(isset($a, $b));

unset ($a);

var_dump(isset($a));   
var_dump(isset($a, $b));

$foo = NULL;
var_dump(isset($foo));
?>

Warning: mysqli_select_db() expects parameter 1 to be mysqli, string given in C:\xampp\htdocs\notes\db_connection.php on line 9

Notice: Undefined index: techname in C:\xampp\htdocs\notes\db_connection.php on line 12

Notice: Undefined index: date in C:\xampp\htdocs\notes\db_connection.php on line 12
bool(true) bool(true) bool(false) bool(false) bool(false)

connection goes first mysqli

mysqli_select_db($dbhandle,"notes");

then need to run a query

if ($result = mysqli_query($dbhandle, $mysqli)) {
 echo "inserted";
}

As for the post variables, check them first before even perform any mysql queries

is database name pc_notes or notes?

The mysqli connection already sets the database unless need to change to something different

 

complete code

if(isset($_POST['techname']) && trim($_POST['techname']) != ''){
$techname = trim($_POST['techname'];
}

if(isset($_POST['date']) && trim($_POST['date']) != ''){
$date = trim($_POST['date'];
}

if(isset($techname) && isset($date)){
$hostname = "localhost";
$username = "root";
$password = "";
$dbname = "pc_notes";

$dbhandle = mysqli_connect($hostname, $username, $password, $dbname);

//if need to change database different than in the connection
//mysqli_select_db($dbhandle,"notes");

$mysqli ="INSERT INTO notes (techname, date) VALUES
('$techname','$date')";

if ($result = mysqli_query($dbhandle, $mysqli)) {
echo "inserted";
}


mysqli_close($dbhandle);
}

Next would be checking data types and escaping the values before inserting

mysqli_real_escape_string

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.