-1

I'm extremely new to mysql, like this weekend new. I can't figure out how to redirect using header(). What am I missing? BTW this page is test_db.php

<?php include("mysql_connect.php"); ?>
<?php
$sql = "SELECT * FROM budget ORDER BY id DESC";
$query = mysql_query($sql) or die (mysql_error());
if (isset($_GET['recordId'])) {
    $id = mysql_real_escape_string($_GET['recordId']);
    $sql_delete = "DELETE FROM budget WHERE id = {$id}";
    mysql_query($sql_delete) or die(mysql_error());

    header("Location: test_db.php");
    exit();
}

?>

... link in question...

<a href="test_db.php?recordId=<?php echo $row['id']; ?>">Delete</a>

looks like it goes to the connection page after the Delete button is clicked and redirect doesn't work. I can connect, this code deletes fine, but I want it to stay on this same page.

please help, thank you.

  • probably outputting before header. Here, find out if it is http://php.net/manual/en/function.error-reporting.php and apply it to your code. – Funk Forty Niner Nov 10 '15 at 19:16
  • 1
    What does this have anything to do with being new to MySQL? In fact your SQL code seems pretty good and you actually are new to PHP. Why do you have the `$sql` variable that is not called anywhere. – Racil Hilan Nov 10 '15 at 19:20
  • you should be using `mysqli` or `PDO`. `mysql` is deprecated. – CodeGodie Nov 10 '15 at 19:20
  • Like I said. Add error reporting to the top of your file(s) right after your opening PHP tag for example ` – Funk Forty Niner Nov 10 '15 at 19:24
  • or your query failed. So put an `else{ header }` if that works, then there's another thing you can go after. But, we won't know that till you start telling us what errors you're getting, nor do we know what's inside `mysql_connect.php` whether it's `mysql_` or `mysqli_` or PDO, no idea. – Funk Forty Niner Nov 10 '15 at 19:27
  • Thank you Fred-ii- for mentioning it may be something in the mysql_connect.php. I was outputting a string from there. I didn't understand how to use the error reporting function. I put the lines in but saw nothing on reload. I got it working now. Thank you so much. – Kirk Radish Nov 10 '15 at 19:40
  • Fred-ii- how can I mark a comment as correct? – Kirk Radish Nov 10 '15 at 19:41

2 Answers2

0

There is a new-line between the closing and opening PHP tags on the top part of the code. This creates output.

<?php include("mysql_connect.php"); ?>
<?php

If you change that to one PHP part it should work.

<?php
include("mysql_connect.php");
moorscode
  • 801
  • 6
  • 13
0

I think the problem is that you can't use header function after all HTTP header were sent to browser. If you are not sending any information to browser before the header function intentionaly, see if you have placed a blank space before <?php or if you have a BOM character.