I have been developing a CRUD application using PHP & MySQL database.
I was succeeded by creating, displaying, updation parts. But I stuck at the deletion part of a row from a database table.
I tried my best solving all the PHP shown errors but now in final it is now showing a message which I wrote to echo in case of failure.
I request someone to please help me with this problem.
Thankyou in advance.
Code I wrote for deletion:
//include database connection
include 'db_connect.php';
//$mysqli->real_escape_string() function helps us prevent attacks such as SQL injection
$query = "DELETE
FROM `grocery`
WHERE `GrocerID` ='".$mysqli->real_escape_string($_GET['id'])."'
limit 0,1";
//execute query
if( $mysqli->query($query) ){
//if successful deletion
echo "User was deleted.";
}else{
//if there's a database problem
echo "Database Error: Unable to delete record.";
}
$mysqli->close();
?>
Code I wrote for delete link in display table:
//just preparing the delete link to delete the record
echo "<a href='delete.php?id={$GrocerID}'>Delete</a>";
Code I wrote for db config:
<?php
//set connection variables
$host = "localhost";
$username = "root";
$password = "secret";
$db_name = "crud"; //database name
//connect to mysql server
$mysqli = new mysqli($host, $username, $password, $db_name);
//check if any connection error was encountered
if(mysqli_connect_errno()) {
die("Connection failed: " . $conn->connect_error);
exit;
}
?>
I tried this and got working, can you update the code and see if this works?
$host = "localhost";
$username = "root";
$password = "secret";
$db_name = "crud"; //database name
//connect to mysql server
$mysqli = new mysqli($host, $username, $password, $db_name);
//check if any connection error was encountered
if(mysqli_connect_errno()) {
die("Connection failed: " . $conn->connect_error);
exit;
}
// Delete row
if ($mysqli->query (sprintf( "DELETE FROM grocery WHERE email = '".$mysqli->real_escape_string($_GET['id'])."' LIMIT 1") )) {
printf ( "Affected Rows %d rows.\n", $mysqli->affected_rows );
}
I hope this helps.
Provide a connection :
if( $mysqli->query($con, $query) ){
Related
I am facing an issue with PHP which doesn't perform one query in my script.
The SQL query works well in my MYSQL console but nothing is happening. Year column stays NULL:
$UpdateYear='UPDATE `pat` SET `Year` = SUBSTRING(`Prepa`,7,4)';
mysqli_query($connWarehouse,$UpdateYear) or die(mysqli_error($connWarehouse));
I don't know what I am doing wrong. Here the full script:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$db="datawarehouse";
// Create connection
$connWarehouse = new mysqli($servername, $username, $password, $db);
// Check connection
if ($connWarehouse->connect_error) {
die("Connection failed: " . $connWarehouse->connect_error);
}
$UpdateYear='UPDATE `pat` SET `Year` = SUBSTRING(`Prepa`,7,4)';
mysqli_query($connWarehouse,$UpdateYear) or die(mysqli_error($connWarehouse));
mysqli_close($connWarehouse));
?>
I finally managed to solve. I don't know if it is a correct way to do it. I have to open a new connection to the database in order to perform.
alter.php:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$db="datawarehouse";
// Create connection
$connWarehouse = new mysqli($servername, $username, $password, $db);
// Check connection
if ($connWarehouse->connect_error) {
die("Connection failed: " . $connWarehouse->connect_error);
}
$AddYear='ALTER TABLE `pat` ADD COLUMN `Year` YEAR;';
mysqli_query($connWarehouse,$AddYear) or die(mysqli_error($connWarehouse));
mysqli_close($connWarehouse));
include 'uppat.php';
?>
uppat.php
<?php
require_once 'config-datawarehouse.php';
$UpdateYear='UPDATE `pat` SET `Year` = SUBSTRING(`DatePrepa`,7,4)';
mysqli_query($conn,$UpdateYear) or die(mysqli_error($conn));
mysqli_close($conn);
?>
On that way it performed the update query.
I'm trying to execute an SQL Statement but it fails without producing an error message. Please can you let me know what is going wrong?
I'm using the same database connection that I've used successfully in other pages for SELECT, and INSERT statements. I've tested the SQL statement successfully in phpMyAdmin. But when executed from php it fails without error message, all other statement in the site work fine.
Database connection include
<?php
$servername = "xxxxxx";
$username = "xxxxxx";
$password = "xxxxxx";
$dbname = "xxxxxx";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
// Connect to DB
include 'incDBConnection.php';
$SQL = "SELECT * from tbl_Properties";
echo "<br/> <br />";
if ($conn->query($SQL) === TRUE) {
echo "Record updated successfully";
} else {
echo "<br/> <br />Error updating record: " . $conn->error;
}
Hello I am new at php and mysql and I don't know what is wrong.
I cant show the results from query and the connection with mysql is successfully connected.
I don't use wampserver I just install php,mysql and Apache separately.
Thanks in advance.
Code
<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
$servername = "localhost";
$username = "root";
$password = "";
// Create connection
$conn = mysqli_connect($servername, $username, $password);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";
$sql="select * from `books`;";
$result=mysqli_query($conn,$sql);
if (!$result){
echo "query cannot execute";
};
?>
its only show me "query cannot execute"
You need to pass fourth parameter database name in mysqli_connect()
It would be
$conn = mysqli_connect($servername, $username, $password,"YOUR_DATABASE");
Read http://php.net/manual/en/mysqli.error.php to check error in query.
Read http://php.net/manual/en/mysqli-result.fetch-array.php
To fetch data from query result
I am creating a form that will take a person's name and email and see if the email exists in the database already but I can't get the database to be selected. The mysql_error() won't display the error either. Is there something wrong with my code for selecting the database? All it shows is the hardcoded text "Could not select database because" then nothing. I replaced all the variables associated with the database with random fillers so as not to give away my info but everything I have is correct regarding that.
$host = "host";
$user = "user";
$password = "pass";
$database = "db";
$port = xxxx;
$table = "table";
// Create connection
$conn = mysqli_connect($host, $user, $password, $database, $port);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connection Successful";
mysqli_select_db($database)
or die("Could not select database because".mysqli_error());
// check if the username is taken
$check = "select email from $table where email = '".$_POST['email']."';";
$qry = mysqli_query($check)
or die ("Could not match data because ".mysqli_error());
$num_rows = mysqli_num_rows($qry);
if ($num_rows != 0) {
echo "Sorry, there the username $username is already taken.";
}
Don't call mysqli_select_db. You've already selected your database with the fourth parameter to mysqli_connect.
You only need to call mysqli_select_db if you want to access a different database after the connection has been established.
Also, Raptor is correct that if you call procedural mysqli_error() you must pass it a connection handle.
For Procedural style mysqli_error(), you must supply the MySQLi DB link as parameter:
mysqli_select_db($database)
or die("Could not select database because" . mysqli_error($conn));
But it's useless to call mysqli_select_db() as you already specified the DB schema in mysqli_connect().
Additionally, your SQL is vulnerable to SQL Injection attack. Always escape the parameter before putting into SQL statement, or use prepared statements.
try this code
<?php
$username = "your_name";
$password = "your_password";
$hostname = "localhost";
//connection to the database
$dbhandle = mysqli_connect($hostname, $username, $password)
or die("Unable to connect to MySQL");
echo "Connected to MySQL<br>";
?>
I am trying to have a database created for each user that signs up for my web app. This database will be based on a template to store their data.
The problem I'm running into is that Google Could SQL doesn't seem to be creating the Database via PHP and MySQL queries.
Connecting to the Instance works fine and triggers no errors based on this code:
//CLOUD SQL CONNECTION FOR SUBSCRIPTIONS
$hostname = details;
$username = details;
$password = details;
$port = details;
$socket = details;
$connection = new mysqli($hostname, $username, $password, $port, $socket);
// Check Connection
if($connection->connect_error) {
trigger_error('Connection failed: ' . $connection->connect_error, E_USER_ERROR);
}
However, when I go to create a simple database, mysqli fails without an error...:
//Create Database
$username = 'account';
$database = 'sub_'. $username .'_db';
$query = "CREATE DATABASE IF NOT EXISTS `{$database}`;";
if(mysqli_query($connection, $query)) {
echo "Database my_db created successfully";
} else {
echo "Error creating database: " . mysqli_error($connection);
}
The output of this is simply: Error creating database:
Am I missing something? How do I create a database in a Cloud SQL instance via PHP? Thanks for any input.
Can anyone confirm that this can actually be done on GAE PHP and Cloud SQL?
It seems the $connection variable was actually NULL despite not throwing an error. This worked for me though:
I connected to an existing database:
//CLOUD SQL CONNECTION FOR SUBSCRIPTIONS
$hostname = details;
$username = details;
$password = details;
$db = details;
$port = details;
$socket = details;
$connection = new mysqli($hostname, $username, $password, $db, $port, $socket);
// Check Connection
if($connection->connect_error) {
trigger_error('Connection failed: ' . $connection->connect_error, E_USER_ERROR);
}
Then I created a new database:
//Create Database
$username = 'account';
$database = 'sub_'. $username .'_db';
$query = "CREATE DATABASE IF NOT EXISTS `{$database}`;";
if(mysqli_query($connection, $query)) {
echo "Database my_db created successfully";
} else {
echo "Error creating database: " . mysqli_error($connection);
}
Then used mysqli_select_db() to select the newly created database.
//Select Newly Created DB
mysqli_select_db($connection, $database);