Why the require_once is not working in following scenario? - php

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.

Related

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

how to call table attribute specifically from database?

Im trying to do if else using Camp data in table testing_data. did i do this correctly in retrieve the data and calling it in if else..i really newbies in php ..hope you can help me..tq
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "brindley";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT * FROM testing_data";
$result = $conn->query($sql);
$user = mysqli_fetch_array($result);
if($user["t_month"] === 'Q1')
{
if($user['t_bbq'] === 'Kambing Golek')
{
if($user['t_participant'] =='>=21 to <=40')
{
if($user['t_outdoor'] == 'Tiada')
{
if($user['t_packages']== 'Pakej 4')
{
echo "YES, People prefer choose packages like this";
}
else
{
echo "NO";
}
}
}
}
?>

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.

PHP Update MySQL value and redirect to a page

Im still new in PHP, I'd like help in a php example where I click a link, a value gets updated in mysql and redirect the user to a page.
Your will be greatly appreciated.
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "UPDATE MyGuests SET lastname='Doe' WHERE id=2";
if ($conn->query($sql) === TRUE) {
// if all ok, redirecting
header("Location: http://example.com/myOtherPage.php");
die();
} else {
echo "Error updating record: " . $conn->error;
}
$conn->close();
?>

Mysqli to variable in PHP

I've been a few days working on my project, but just stay stuck a specific part.
My problem is that i like to get a value from the database (mysqli)
But i always receive a 0. So, no value.
This is my code:
$servername = "localhost";
$username = "user";
$password = "pass";
$dbname = "dbname";
$serialkey = $_GET['hardwareSerial'];
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$user_idSQL = "SELECT user_id FROM ss_devices WHERE serialkey = '$serialkey'";
$user_idResult = $conn->query($user_idSQL);
$user_id = $user_idResult;
The script should see what the userid, it is in equality with the specified serial number.
But when i take a serial number like: FJRI433. I always get a 0. But in the database has this serial number user_id: 3.
I hope someone can help me out whith this problem.
Thanks.
You are executing the query but not fetching any results.
The manual has plenty of examples of using mysqli :
if ($result = $conn->query($query)) {
while ($row = $result->fetch_assoc()) {
//Do stuff with the next row.
}
}
Try this perhaps...
$servername = "localhost";
$username = "user";
$password = "pass";
$dbname = "dbname";
$serialkey = $_GET['hardwareSerial'];
$conn=mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if ( mysqli_connect_errno() ) {
die("Connection failed: " . mysqli_connect_error() );
}
$user_idSQL = "SELECT `user_id` FROM `ss_devices` WHERE `serialkey` = '$serialkey'";
if( $user_idResult = mysqli_query( $conn, $user_idSQL ) ) {
while ($row = mysqli_fetch_assoc($user_idResult)) {
echo $row['user_id'].'<br />';
}
}
mysqli_close( $conn );

Categories