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

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.

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()];

Cannot execute database statement from another php file using include

What I'm doing wrong after adding class define to StoreUser.php file and calling it from store.php file because it doesn't work any moore? It worked fine inside file StoreUser.php without class define and function see below.
$dbhost = "localhost";
$dbname = "riskinarviointilomake";
$dbuser = "root";
$dbpass = "";
//private $email;
if(isset($_GET["email"])){
$conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if (!( $stmt = $conn->prepare("INSERT INTO users(unique_id,email,
encrypted_password) VALUES(?,?,?)"))){
echo "Table creation failed: (" . $conn->errno . ") " . $conn->error;
}
$stmt->bind_param('sss', $unique_id,$email,$password);
$unique_id=123457764;
$password="df12467hh";
$result = $stmt->execute();
$stmt->close();
$conn->close();
}
?>
This doesn't work
class StoreUser{
$dbhost = "localhost";
$dbname = "riskinarviointilomake";
$dbuser = "root";
$dbpass = "";
//private $email;
public function store($email){
$conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if (!( $stmt = $conn->prepare("INSERT INTO users(unique_id,email, encrypted_password) VALUES(?,?,?)"))){
echo "Table creation failed: (" . $conn->errno . ") " . $conn->error;
}
$stmt->bind_param('sss', $unique_id,$email,$password);
$unique_id=123457764;
$password="df12467hh";
$result = $stmt->execute();
$stmt->close();
$conn->close();
}
}
<?php
include 'StoreTest.php';
if(isset($_GET['email'])){
$email = $_GET['email'];
$store = new StoreTest();
$store->store($email);
}
?>

Config file doesn't connect with mysql database

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; }
?>

mysql will not insert past fourth row

I'm not exactly sure what happened but this database and the php effecting it were working just fine until it hit the fourth row and now it won't insert new records at all.
if($_POST)
{
$servername = ******;
$username = ******;
$password = ******;
$db = ******;
$conn = mysqli_connect($servername, $username, $password, $db);
mysqli_select_db($conn,$db);
$uuid = $_POST['uuid'];
$sql = "INSERT INTO uuid VALUES ('$uuid');";
mysqli_query($conn,$sql);
mysqli_close($conn);
}
I'm not sure what happened but this is the relevant code for the mysqli query.
try this
<?php
if(isset($_POST['uuid']))
{
$servername = yourServerName;
$username = username;
$password = password;
$dbname = databaseName;
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$uuid = $_POST['uuid'];
$sql = "INSERT INTO tableName (columnName) VALUES ('$uuid')";
if (mysqli_query($conn, $sql)) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
}
?>
Also, I recommend using prepared statements.

Categories