UPDATE does not update table

Asked

Viewed 868 times

-1

I am unable to update my fields. Where I am missing?

Caught the id through GET and I can display the table data, but I can’t update it.

CAMPOS

$id_destino = $_GET['id_destino'];
$destino = mysql_real_escape_string($_POST['destino']);
$historia = mysql_real_escape_string($_POST['historia']);
$geografia = mysql_real_escape_string($_POST['geografia']);
$clima = mysql_real_escape_string($_POST['clima']);
$cultura   = mysql_real_escape_string($_POST['cultura']);

if($_POST['enviar']) {
    $sql = mysql_query("UPDATE `destinos_pt` SET destino = '$destino', historia = '$historia', geografia = '$geografia', clima = '$clima', cultura = '$cultura'  WHERE id_destino = '$id_destino'");
    header("Location: dashboard.php");
}

FORM

<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="POST" name="destinosform" novalidate role="form">
</form>

What’s wrong I can’t update my table?

  • 2

    Put $sql = mysql_query("UPDATE destinos_pt SET destination = '$destination', history = '$history', geography = '$geography', climate = '$climate', culture = '$culture' WHERE id_destination = '$id_destination'") or die(mysql_error());

  • There is the variable $_POST['enviar'] on your form?

  • 2

    Why mix GET with POST? Place the entire form, which makes it easier to try to find a solution.

  • 1

    Put php from the connection part of the database also if possible.

  • 1

    Important reading: How do you debug PHP scripts?

  • You need to specify your code more so we can see where the bug is.

  • 2

    Beware of sql Injection using the parameters this way, either do a cleanup or use Prepared statements.

Show 2 more comments

1 answer

1


From what I understand, $_GET['id_destination']; is not being set. Do this and check if there is any value being returned: var_dump($_GET['id_destino']);

If it is empty, you should (if you really want to use this mix of GET and POST, but I don’t recommend it) send the id inside the action of your form.

And $_POST['enviar']? Does it exist inside your form? If it does not exist, the condition will not be met and UPDATE will not be executed.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.