Here is my code:
<?php
$url = $_GET["id"];
$servername = "XXXX";
$username = "XXXX";
$password = "XXXX";
$dbname = "XXX";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "DELETE FROM Table WHERE id='".$url."'";
if ($conn->query($sql) === TRUE) {
echo 'Row deleted successfully';
} else {
echo "Error deleting record: " . $conn->error;
}
$conn->close();
header('Location:index.php');
exit;
?>
But header redirect is not working...
Here is the error log:
Strict Standards: header(): It is not safe to rely on the system's timezone settings. You are required to use the date.timezone setting or the date_default_timezone_set() function. In case you used any of those methods and you are still getting this warning, you most likely misspelled the timezone identifier. We selected 'America/New_York' for 'EST/-5.0/no DST' instead in /homepages/5/d394578306/htdocs/XXXXX/XXXXXX/delete.php on line 24
Warning: Cannot modify header information - headers already sent by (output started at /homepages/5/d394578306/htdocs/XXXXX/XXXXXX/delete.php:1) in /homepages/5/d394578306/htdocs/XXXXXX/XXXXXX/delete.php on line 24
<?php
Delete the whitespace before the PHP tag - that counts as output and will stop the header() call from working.
You're also doing some echo calls, both of which will also stop the redirect from working as echo will output content/headers to the browser.
It appears to me that the timezone warning of the header causes the output, which in turn makes the function itself not work. Try doing this:
#header('Location:index.php')
The # will suppress error output for that particular function call.
Also, as Bulk correctly pointed out, you should disable your echo calls in lines 17 and 19, or move them below the header method call.
Based on the timezone error, try this:
<?php
date_default_timezone_set("--HERE YOUR TIMEZONE---");
$url = $_GET["id"];
$servername = "XXXX";
$username = "XXXX";
$password = "XXXX";
$dbname = "XXX";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "DELETE FROM Table WHERE id='".$url."'";
if ($conn->query($sql) === TRUE) {
echo 'Row deleted successfully';
} else {
echo "Error deleting record: " . $conn->error;
}
$conn->close();
header('Location:index.php');
exit;
?>
Now replace the "--HERE YOUR TIMEZONE---".
Here you can find the correct timezone for your country: http://php.net/manual/en/timezones.php
Related
I have looked at several examples on how to call a MySQL stored procedure from PHP but none have helped me. The stored procedure works when run inside PHPMyAdmin but I am having trouble calling it from the web.
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "dbname";
$conn = new mysqli($servername, $username, $password, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$result = mysqli_query($conn,"CALL standings_build()");
if (mysqli_query($conn,$sql))
header('refresh:1; url=schedule_main_scores.php');
else
echo "failed";
?>
There's 2 problems here.
You're querying twice and using the wrong variable, being $sql instead of $result.
$result = mysqli_query($conn,"CALL standings_build()");
if (mysqli_query($conn,$sql))
^^^^^^^^^^^^ calling the query twice
^^^^ wrong variable, undefined
all that needs to be done is this:
if ($result)
and an else to handle the (possible) errors.
Error reporting and mysqli_error($conn) would have been your true friends.
http://php.net/manual/en/function.error-reporting.php
http://php.net/mysqli_error
Side note: You really should use proper bracing techniques though, such as:
if ($result){
echo "Success";
}
else {
echo "The query failed because of: " . mysqli_error($conn);
}
It helps during coding also and with an editor for pair matching.
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;
}
So, i got this code:
<?php
$servername = "localhost";
$username = "**";
$password = "**";
$dbname = "TestDB";
// Create connection
$conn = new mysqli($servername, $username, $password,$dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully<br>";
$ip = $_SERVER['REMOTE_ADDR'];
$sql = "INSERT INTO testdata (id,address,count) VALUES (DEFAULT,'$ip',1) ON DUPLICATE KEY UPDATE count=count+1";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully<br>";
} else {
echo "Error: " ;
}
$conn->close();
?>
With this code ip-addresses of customers are saved and it should increase count by 1 every time they return to the website. When i visit database.php directly it works like it should in every browser, but when i call the php-file via the index.html-page it counts up twice (most of the times, not always) in Mozilla, other browsers no problem, the code in html-file:
<img style="display: none;" src="http://servername/database.php?">
Anyone know why? Please help, i'm really stuck here
I have a website in PHP. I try to store the session variable $_SESSION['user_name'] to a mysql database when a logged in user visits a specific webpage on my site.
<?php
$servername = "localhost";
$username = "user1";
$password = "user1";
$dbname = "payment";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = 'INSERT INTO users
VALUES ('.$_SESSION['user_name'].')';
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
Error message:
Notice: Undefined variable: _SESSION in /opt/lampp/htdocs/succes.php on line 16
Tried a bunch of things but can't figure it out. What is wrong here?
You need to call session_start() at the beginning of your script (before using any $_SESSION variables). Also, you need quotes around the variable in you query:
$sql = 'INSERT INTO users
VALUES ("'.$_SESSION['user_name'].'")';
Please note that this is not safe; you are wide open to SQL injection. Instead, you should use prepared statements:
<?php
$servername = "localhost";
$username = "user1";
$password = "user1";
$dbname = "payment";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = 'INSERT INTO users
VALUES (?)';
$stmt = $conn->prepare($sql);
$stmt->bind_param('s', $_SESSION['user_name']);
if ($stmt->execute()) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
Before you use any $_SESSION variables you need to call session_start().
Of topic a bit though, something to look into PDO. It can be a bit a tad slower than mysqli() however supports many more Database types. Here is a good article on Tuts+ explaining some of the differences as well as explaining essential security steps.
If I could be a bit biased I have created a PHP Class for PDO Connections which can be found on GitHub
I'm an iOS developer trying to update a bit 'o php to work with the mysqli_* stuff instead of the deprecated mysql_* stuff.
I know next to nothing about php/mysql, and I'm stuck. I'm using a script found at http://www.w3schools.com/php/php_mysql_select.asp, with changes to reflect my field names etc
When I call the following from a browser I get an access error (Connection failed: Access denied for user 'whoisit7'#'localhost' (using password: YES)). The server name, password and username etc I'm using all work with an existing script in a browser but not with this new one.
Can anyone point me at what I'm getting wrong?
<?php
$servername = "localhost";
$userName = "whoisit7_ios";
$password = "blahblahblah";
$dbName = "whoisit7_scathing";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT name, latitude, longitude FROM location where status = 1";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "name: " . $row["name"]. " - latlong: " . $row["latitude"]. " " . $row["longitude"]. "<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
In PHP, variable names are case-sensitive. So, Change your connection variable names to:
$servername = "localhost";
$username = "whoisit7_ios";
$password = "blahblahblah";
$dbname = "whoisit7_scathing";
Read it if you are new to PHP: http://php.net/manual/en/language.variables.basics.php
Php is case sensitive, so change the below line from:
$conn = mysqli_connect($servername, $username, $password, $dbname);
to
$conn = mysqli_connect($servername, $userName, $password, $dbName);
You have made error in $username which is $userName and $dbname which should be $dbName as you declared above.
Just change -->
localhost to 127.0.0.1 //you need to change params in mysql config if u want to use it as localhost
And
$username to $userName //As u need to keep these variables similar :)
this Problem seems to be coming from database privileges as i have also have encountered this before. Give this fix a try.
GRANT ALL PRIVILEGES ON database_name TO usernamehere#host IDENTIFIED BY 'yourpassword';
FLUSH PRIVILEGES;
Just change the lower case letters to your own credentials.