PHP Redirect stopped working? - php

I have wrote a script to update a MySql DB from a form.
After the DB has been updated I want the page to auto redirect to another page.
This has been working fine however since switching hosting provider non of my sites re-directs work.
Here is the code:
<?php
$servername = "localhost";
$username = "XXX";
$password = "XXX";
$dbname = "XXX";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$id= $_POST[id];
$dob=$_POST[dob];
$sql=("update users set dob='$dob' where id='$id'")or die('Error 23 ' . mysql_error());
if ($conn->query($sql) === TRUE) {
echo "Updated successfully<br /><br />";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
?>
<?php
header("location:index.php?action=updated"); ?>
When I run the code the DB updates but the page just displays Updated successfully?

try using javascript to redirect like below:
if ($conn->query($sql) === TRUE) {
echo "<script>
alert('Updated successfully');
window.location.href = 'index.php?action=updated';
</script>";
}

Don't echo anything and try to redirect afterwards. Instead simply redirect without any echoing.
if ($conn->query($sql) === TRUE) {
header("location:index.php?action=updated");
exit;
} else
echo "Error: " . $sql . "<br>" . $conn->error;

try this :
<?php
ob_start();
header('Location: http://www.example.com/index.php?action=updated', true);
?>

Related

How to transfer data from DB1 to DB2 (different network/server)

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

How can i display a success pop up message before redirecting a user to the last php page

I want to have a pop up window appear to let people know that sending their comment was successful. Once they click on the OK button then they get redirected to the last page that they were on. I can get the alert to work if I remove the last line but they just don't work together. Using my code below the alert message is skipped and it goes directly to the referrer line. I have no idea why. I would really appreciate your help. Thanks.
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO comment (Comment, firstname, lastname, Image_path, Approved)
VALUES ('$comment', '$firstname','$lastname','$target_file','2')";
if ($conn->query($sql) === TRUE) {
$message = "Thankyou for your comment.";
echo "<script type='text/javascript'>alert('$message');</script>";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
$conn->close();
header('Location: ' . $_SERVER['HTTP_REFERER']);
You cannot output anything before a header() call as it will cause header already sent error.
Made the redirection using a js code.
Try this:
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO comment (Comment, firstname, lastname, Image_path, Approved)
VALUES ('$comment', '$firstname','$lastname','$target_file','2')";
if ($conn->query($sql) === TRUE) {
$message = "Thankyou for your comment.";
echo "<script type='text/javascript'>alert('$message');</script>";
echo "<script type='text/javascript'>
window.location = '".$_SERVER['HTTP_REFERER']."';
</script>";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
$conn->close();
You can try this, it will redirect to the previous page 3 seconds after the alert goes up (3000ms)
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO comment (Comment, firstname, lastname, Image_path, Approved)
VALUES ('$comment', '$firstname','$lastname','$target_file','2')";
if ($conn->query($sql) === TRUE) {
$message = "Thankyou for your comment.";
echo "<script type='text/javascript'>alert('$message');</script>";
echo "function goBack() { setTimeout(function(){ window.history.back(); }, 3000);}";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
}
$conn->close();

PHP syntax error for storing data into mysql db

I have resolved the issue. The following code now works perfectly.
Thank you all.
Please the relevant section of dbcontroller.php file as follows:
<?php
class DBController {
function runQuery2($query) {
$result = mysql_query($query);
return $result;
}
}
In addition, I have amended my original MySQL statements in my main html/php file to look like this:
<?php
session_start();
require_once("dbcontroller.php");
$db_handle = new DBController();
if(!empty($_POST["submit"])) {
if ($db_handle->runQuery2("INSERT INTO cquestionstable
(postid, ccode, nick, queries) VALUES ( 1,'cc-001', 'james', 'what
could be the problem?')") === TRUE) {
echo "New record created successfully";
} else {
echo "Error in posting question, pls try again." . "<br>";
}
?>
Thanks n cheers.
Your Code:
<?php
session_start();
require_once("dbcontroller.php");
$db_handle = new DBController();
if(!empty($_POST["submit"])) {
$sql = "INSERT INTO cquestionstable (postid, ccode, nick, queries) VALUES ( 1,'cc-001', 'james', 'what could be the problem?')";
if ($db_handle->runQuery($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $**sql . "<br>" . $db_handle->error;
}
}
?>
I see the errors already.
First if (!empty($_POST["submit"])) { should be if (isset($_POST["submit"])) {
Then you used if ($db_handle->runQuery($sql) === TRUE) { which should actually be if ($conn->query($sql) === TRUE) {
Then in your echo you used $**sql which should be $sql
Then i did not know what dbcontroller.php was but the final code should be
<?php
// session_start(); You do not need this when inserting into database
include "dbcontroller.php";
if (isset($_POST["submit"])) {
$sql = "INSERT INTO cquestionstable (postid, ccode, nick, queries) VALUES (1, 'cc-001', 'james', 'what could be the problem?')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $connn->error;
}
}
?>
dbcontroller.php
<?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);
}
?>
If you still do not understand why it is not working please look at w3Schools
Hope this is the answer you we're looking for.

Delete Page PHP MYSQL

Hey guys I have created A delete page, It does not work when I just submit the form and the URL is http://localhost/delete-session.php but once I change the URL to http://localhost/delete-session.php?id=1 it works, What am I missing In my code to make it work?
<h1>Delete Page</h1>
<h3>Enter the booking number of the session you would like to delete!</h3>
<form action ="delete-session.php" method="post">
Booking ID:(Refer To Database):<input type="text" name="booking">
This is the php
if(isset($_GET['booking'])){
$id=$_GET['booking'];
if(!is_numeric($id)){
echo "sorry, there appears to have been an error.";
exit;
}
} else {
echo "sorry, there appears to have been an error.";
exit;
}
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "olympics";
$conn = mysqli_connect($servername, $username, $password, $dbname);
$id=$_GET['id'];
if(!is_numeric($id)){
echo "Sorry, there is an error";
exit;
}
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql="DELETE from olympiics where booking='$id'";
echo $sql;
if (mysqli_query($conn, $sql)) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . mysqli_error($conn);
}
mysqli_close($conn);
I'm going to take a crack at this.
I'm guessing it's because when you go to http://localhost/delete-session.php?id=1 you're passing the id=1 via GET, so when you retrieve the GET input from in your code it succeeds with $id=1, but in your HTML your form is send via POST.
As a fix try using $id=$_POST['booking'];
Bench test your code.
It starts getting $id=$_GET['booking']; which does not exist because you have set the method="post" in your <form> tag.
So use $id=$_POST['booking'];
Then later on it does $id=$_GET['id']; overwriting the value you already attempted to get from above.
This would explain why it requires the extra id paramter on http://localhost/delete-session.php?id=1 as using the querystring to send data will send the id parameter in the $_GET['id'] array, and I dont see why you would want to do this anyway as it has been done at the top of your code by getting this id value from $id=$_POST['booking']
It also makes code so much easier to read and more importantly debug if you adopt an indentation standard in your script like below.
Try this out for size, without adding the id=1 to the querystring
if(isset($_POST['booking'])){
$id=$_POST['booking'];
if(!is_numeric($id)){
echo "sorry, there appears to have been an error. Booking must be numeric";
exit;
}
} else {
echo "sorry, there appears to have been an error.";
exit;
}
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "olympics";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql="DELETE from olympiics where booking='$id'";
$res = mysqli_query($conn, $sql);
if ($res !== FALSE) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . mysqli_error($conn);
}
mysqli_close($conn);
?>
As you are using the mysqli extension, you should also be using parameterized queries to prevent SQL Injection.
if(isset($_POST['booking'])){
$id=$_POST['booking'];
if(!is_numeric($id)){
echo "sorry, there appears to have been an error. Booking must be numeric";
exit;
}
} else {
echo "sorry, there appears to have been an error.";
exit;
}
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "olympics";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql="DELETE from olympiics where booking=?";
$stmt = mysqli_prepare($conn, $sql);
if ( $stmt === FALSE ) {
echo mysqli_error($conn);
exit;
}
mysqli_stmt_bind_param($stmt, 'i', $id);
$res = mysqli_stmt_execute($stmt);
if ($res !== FALSE) {
echo "Record deleted successfully";
} else {
echo "Error deleting record: " . mysqli_error($conn);
}
mysqli_close($conn);
?>
Change form method to get, or use $_REQUEST instead of $_GET

Post variable via ajax to mysql db - phonegap

I just want to post a simple variable from my phonegap app to mysql database. But it dont work. The db show me empty results.
Mysql database looks like this, Tablename is user
Rows:
user (varchar 25),vip (varchar 1),call (varchar 1)
At top of my project i inserted:
<script type="text/javascript" src="js/tcPlugin.js"></script>
<script type="text/javascript" src="js/phoneApp.js"></script>
<script type="text/javascript" src="js/start.js"></script>
start.js is my file for the ajax request - the other two working fine so i guess there is not the problem
my start.js file looks like this:
$(document).ready( function() {
var email = "john#web.de";
$.ajax({
type: 'post',
url: 'http://www.blablabla.de/phone/action.php',
data: {
data: {"email" : email },
//data:email,
},
success: function(result) {
console.log(result);
}
});
and my action.php looks like this:
<?php
$dbhost = "bla";
$dbuser = "bla";
$dbpass = "bla";
$dbname = "bla";
$user = $_POST['data']['email'];
$conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO user (email)
VALUES ('" . $user . "')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
But everytime i start the app the db is empty. debugger concole is not giving me any errors. db connection is working, tried it with echo connection result. and the other js are working fine.
action.php
<?php
$dbhost = "bla";
$dbuser = "bla";
$dbpass = "bla";
$dbname = "bla";
print_r($_POST); // see full contents of the POST
$user = $_POST['data']['email'];
print PHP_EOL . $user . PHP_EOL; // see full contents of the $user var
$conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO user (email) VALUES ('" . $user . "')";
print PHP_EOL . $sql . PHP_EOL; // see the actual sql query, you can test this later in your sql editor ex. phpmyadmin
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
make these edits, run the ajax and check the response tab in the debugger.

Categories