Config file doesn't connect with mysql database - php

I'm using MAMP and the problem here is the config file that won't to connect with a database,
<?php
define("HOST","localhost");
define("USER", "root");
define("PASS", "root");
define("DB", "inserttest");
$con = mysqli_connect(HOST,USER,PASS,DB);
?>
...
<?php
include 'config.php';
$firstname = $_POST['firstname'];
$fathername = $_POST['fathername'];
$grandfathername = $_POST['grandfathername'];
$familyname = $_POST['familyname'];
$sql = mysqli_query($con,"INSERT INTO subventions (firstname,fathername,grandfathername,familyname) VALUES ('$firstname','$fathername','$grandfathername','$familyname')");
if ($sql == true) {
echo '<b> تم الإرسال </b>';
} else {
echo '<b> فشل الإتصال </b>';
}
mysqli_close($con);
?>
...............................................................

You can use some functions to check weather the connection has established or not
you can do like this
<?php
define("HOST","localhost");
define("USER", "root");
define("PASS", "root");
define("DB", "inserttest");
$con = mysqli_connect(HOST,USER,PASS,DB);
if(mysqli_connect_errno())
{
die("Some Error Occured While COnnection to database :".mysqli_connect_error());
}
?>
Hope this will help

You can check existing file path.
Try Again.
<?php
//include(foldername/filename);
include('config.php') or die('File path error');

try this
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "inserttest";
// Create connection
$conn = new mysqli($servername, $username,
$password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error); } ?>
include('config.php');
$firstname = $_POST['firstname'];
$fathername = $_POST['fathername'];
$grandfathername = $_POST['grandfathername'];
$familyname = $_POST['familyname'];
$sql = "INSERT INTO subventions
(firstname,fathername,grandfathername,familyname) VALUES
('$firstname','$fathername','$grandfathername','$familyname')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully"; } else {
echo "Error: " . $sql . "" . $conn->error; }
?>

Related

How to transfer data from DB1 to DB2 (different network/server)

I want to set up a website with a form in it. The form will transfer the data to the DB, but I think it is not safe to let the personal data in the DB which is external reachable.
So I thought I should transfer the data via PHP from the DB1(server1 - external reachable) to DB2(server2 - only internal reachable).
The following picture should help to know what I am searching for.
Is there any names/methods to google for?
From php you just have to create a new connection for DB2.
<?php
$servername = "localhost";
$username = "database1";
$password = "xxxxxxxx";
$dbname = "database1";
$servername2 = "localhost";
$username2 = "database2";
$password2 = "xxxxxxxx";
$dbname2 = "database2";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
$conn2 = new mysqli($servername2, $username2, $password2, $dbname2);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if ($conn2->connect_error) {
die("Connection failed: " . $conn2->connect_error);
}
//escape variables for security
$fname = mysqli_real_escape_string($conn, $_POST['fname']);
$lname = mysqli_real_escape_string($conn, $_POST['lname']);
$sql = "INSERT INTO mytable (fname,lname)
VALUES ('$fname','$lname')";
if ($conn->query($sql) === TRUE) {
echo "Successfully Saved";
} else {
echo "Error: Go back and Try Again ! " . $sql . "<br>" . $conn->error;
}
if ($conn2->query($sql) === TRUE) {
echo "Successfully Saved";
} else {
echo "Error: Go back and Try Again ! " . $sql . "<br>" . $conn2->error;
}
$conn->close();
$conn2->close();
?>

SQL with PHP variables

I'm trying to update db record using the code below. Even if the redirection is done, the record isn't being updated as it should be. any tips please?
<?php
session_start();
?>
<?php
$servername = "localhost";
$username = "root";
$password = "root";
$dbname = "Mydatabase";
$id = $_GET["session_id()"];
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "UPDATE leads SET first_name='hola' WHERE id = '$id'";
if ($conn->query($sql) === TRUE) {
header( "thank-you.php" ); die;
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
?>
This one causes the problem.
$id = $_GET["session_id()"];
session_id() itself is a PHP function. No need for quoting. Just remove the quotes and it should be fine.
$id = $_GET[session_id()];

PHP mysql insert not working without error check

My table has 2 fields a primary with auto increment and then entry_id
why does the below not insert in the table:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "ecolog";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$entry_id = 5;
$sql = "INSERT INTO orders (entry_id) VALUE ('$entry_id')";
$conn->close();
?>
but when adding a error check it does?
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "ecolog";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$entry_id = 5;
$sql = "INSERT INTO orders (entry_id) VALUE ('$entry_id')";
if ($conn->query($sql) === TRUE) {
echo "";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
cant get my head around why this would be happening.

Why I am getting a lines under the table in phpmyadmin(localhost)

I am trying to send a data from android studio, but I am getting lines under the table instead of assigning data.
Dont know where I am gone wrong.Plz help me.Thanks in advance.
This is my PHP code
add_employee
<?php
include('connection.php');
if (isset($_POST["name"])){
$emp_name = $_POST["name"];
echo $emp_name;
echo "is your name";
}
else{
$emp_name = NULL;
echo "POST filename is not assigned";
}
$success = 0;
$status = "Active";
$sqli = "INSERT INTO `employee` (`emp_name`) VALUES ('$emp_name')";
if(mysqli_query($conn,$sqli)){
$success=1;
}
$response["success"]=$success;
die(json_encode($response));
mysqli_close($conn);
?>
Connection.php
<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = '';
$conn = mysqli_connect($dbhost, $dbuser, $dbpass);
if(!$conn) {
die('Could not connect: ' . mysqli_error());
}
mysqli_select_db($conn,'student');
?>
You have so many errors in your code. no db name, no proper query definition. you can use this simple code:
Connection.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "slim";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
Inserting employee code:
<?php
if (isset($_POST["name"])){
$emp_name=$_POST["name"];
echo $emp_name;
echo "is your name";
}
else{
$emp_name = null;
echo "POST filename is not assigned";
}
$success=0;
$status="Active";
$sql = "INSERT INTO employee (name)
VALUES ('$emp_name')";
if ($conn->query($sql) === TRUE) {
$success=1;
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
It's working and easy to understand for you.

Why the require_once is not working in following scenario?

I've following code of a function in a PHP file titled sample.php :
function deleteValue($value) {
$servername = "localhost";
$username = "root";
$password = "jumbo";
$dbname = "demo";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "DELETE FROM user WHERE value = '$value'";
if ($conn->query($sql) === TRUE) {
$conn->close();
return true;
} else {
$conn->close();
return false;
}
}
Actually, in this file(sample.php) there are too many such functions which repeats the following database connectivity code :
$servername = "localhost";
$username = "root";
$password = "jumbo";
$dbname = "demo";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
Then, I created one file titled db.php in the same folder and added the above code to it. Using require_once('db.php'); at the beginning of file 'sample.php'I included the file to the above code. Finally the code appeared as follows :
require_once('db.php');
function deleteValue($value) {
$sql = "DELETE FROM user WHERE value = '$value'";
if ($conn->query($sql) === TRUE) {
$conn->close();
return true;
} else {
$conn->close();
return false;
}
}
Now it's giving me 500 Internal Server error.
Even instead of including the file I tried by pasting the whole code from db.php at the beginning of file sample.php as follows but still I get 500 Internal Server Error.
$servername = "localhost";
$username = "root";
$password = "jumbo";
$dbname = "demo";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
function deleteValue($value) {
$sql = "DELETE FROM user WHERE value = '$value'";
if ($conn->query($sql) === TRUE) {
$conn->close();
return true;
} else {
$conn->close();
return false;
}
}
Can someone please correct the mistake I'm making in my code?
Thanks in advance.
I will suggest you that in db_config.php file you should use only database config and functions like this:
$servername = "localhost";
$username = "root";
$password = "jumbo";
$dbname = "jumbo_jet";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_errno) {
echo "Failed to connect to MySQL: (" . $conn->connect_errno . ") " . $conn->connect_error;
}
And in common.php use code like this:
require_once("db_config.php");
function deleteToken($receivedToken) {
global $conn;
$sql = "DELETE FROM user_login WHERE token = '$receivedToken'";
if ($conn->query($sql) === TRUE) {
return true;
}
return false;
}
I hope it will work for you. If anything else then let me know.

Categories