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);
}
?>
Related
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();
?>
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.
Trying to write a function that displays the id, and filepath of all public content in an SQL database. Here's what I have so far, but it failed to run.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "pricosha";
// Create connection
$connection = mysql_connect($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
//Added two extra variables, username and content_name
$query = "SELECT id, username, file_path, content_name FROM Content";
$result = mysql_query($query);
//Loop through the results of the query
while($row = mysql_fetch_array($result)){
echo "ID: " . $row["id"]. " Username: " . $row["username"]. " File Path: " . $row["file_path"]. " Content Name: " . $row["content_name"]. "<br>";
}
$conn->close();
?>
Your variable for create the connection to mysql is $connection, while you use $conn for this :
$conn->connect_error
and this :
$conn->close();
Now try to change $conn to $connection or just change this :
$connection = mysql_connect($servername, $username, $password, $dbname);
to this :
$conn = mysql_connect($servername, $username, $password, $dbname);
So far now, the function mysql_connect, mysql_query, and others have been deprecated. You need to change it to mysqli I think. Try this.
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "pricosha";
$connection = new mysqli($servername, $username, $password, $dbname) or die(mysqli_errno());
$query = "SELECT id, username, file_path, content_name FROM Content";
$result = mysqli_query($connection, $query);
while($row = mysqli_fetch_assoc($result)){
echo "ID: " . $row["id"]. " Username: " . $row["username"]. " File Path: " . $row["file_path"]. " Content Name: " . $row["content_name"]. "<br>";
}
?>
Try this
$conn = mysql_connect($servername, $username, $password, $dbname);
while executing query got this error "Error updating record: You have an error in your SQL syntax"
$conn = new mysqli($servername, $username, $password, $dbname);
$sql = "SET #a:=0;UPDATE registrations SET EXIBIT_NO=#a:=#a+1 ORDER BY GR_ID";
You can wrtie:
$result=mysqli_query($conn,$sql);
Code Example:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT id, firstname, lastname FROM MyGuests";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "id: " . $row["id"]. " - Name: " . $row["firstname"]. " " . $row["lastname"]. "<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
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.