PHP mysql insert not working without error check - php

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.

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

Trying to write a function that displays data in an SQL database

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

Why I can't run this query? [duplicate]

This question already has answers here:
Can I mix MySQL APIs in PHP?
(4 answers)
Closed 7 years ago.
I'm trying to run this code in php but does not print anything.
My database is not empty.
<?php
$dbname = "mytest";
$servername = "***";
$username = "mohsen";
$passwords = "****";
// Create connection
$conn = new mysqli($servername, $username, $passwords, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$User=$_POST['username'];
$sql="SELECT * FROM Money WHERE Debtor='$User' OR Creditor='$User' ";
$result = $conn->query($sql);
//
while($field=mysql_fetch_assoc($result))
{
print $field["Debtor"]."|";
print $field["Creditor"]."|";
print $field["Cost"]."|";
print $field["Status"]."|"."\n";
}
$conn->close();
?>
EX:
I have record with this user in my database
User=mohsen
link:
http://test.kholaseketab.ir/Update.php
You are mixing object oriented and procedural style query.
<?php
$dbname = "mytest";
$servername = "***";
$username = "mohsen";
$passwords = "****";
// Create connection
$conn = new mysqli($servername, $username, $passwords, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$User=$_POST['username'];
$sql="SELECT * FROM Money WHERE Debtor='$User' OR Creditor='$User' ";
$result = $conn->query($sql);
//
while($field = $result->fetch_object())
{
print $field->Debtor."|";
print $field->Creditor."|";
print $field->Cost."|";
print $field->Status."|"."\n";
}
$conn->close();
?>
<?php
$dbname = "mytest";
$servername = "***";
$username = "mohsen";
$passwords = "****";
// Create connection
$conn = new mysqli($servername, $username, $passwords, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$User=$_POST['username'];
$sql="SELECT * FROM Money WHERE Debtor='$User' OR Creditor='$User' ";
$result = $conn->query($sql);
while($field=$result->fetch_assoc())
{
print $field["Debtor"]."|";
print $field["Creditor"]."|";
print $field["Cost"]."|";
print $field["Status"]."|"."\n";
}
$conn->close();
?>

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.

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