Victor Bjelkholm
Victor Bjelkholm

Reputation: 2016

MySQL UPDATE doesn't change anything

I'm trying to update a database entry but it won't change anything. I'm getting no errors which confuses me...

Code:

if(isset($_GET['edit']))
{
    $idn = $_GET['id'];
    $namn = $_POST['namn'];
    $adress = $_POST['adress'];
    $postnummer = $_POST['postnummer'];
    $postort = $_POST['postort'];
    $email = $_POST['email'];
    $status = 0;
    echo $namn;
    $sql="UPDATE ordrar SET namn = '$namn' AND adress = '$adress' AND postnummer = '$postnummer'
    AND postort = '$postort' AND email = '$email' AND status = '$status' WHERE id = '$idn'";
    if (!mysql_query($sql))
    {
        die('Error: ' . mysql_error());
    }
    //$referer = $_SERVER['HTTP_REFERER'];
    //header('Location:'. $referer);
}

Thanks for answers /Victor

Upvotes: 1

Views: 5605

Answers (3)

Your Common Sense
Your Common Sense

Reputation: 157871

if you get no errors it does mean that no records matched WHERE condition

or you're probably don't have $_GET['edit'] varibale set

Upvotes: -4

Salil
Salil

Reputation: 47502

Ref this

Syntax for Update

UPDATE table_name SET field1=new-value1, field2=new-value2 [WHERE Clause]

Your query should

 $sql="UPDATE ordrar SET namn = '$namn' , adress = '$adress' ,
          postnummer = '$postnummer' , postort = '$postort' , email = '$email' ,
          status = '$status' WHERE id = '$idn'";

Upvotes: 4

Dave W. Smith
Dave W. Smith

Reputation: 24966

Your immediate problem is SQL syntax. Read the documentation on UPDATES and replace the ANDs with commas.

Your secondary, but possibly larger problem is that you're building a query out of untrusted user input. That's a recipe for a SQL injection attack. Use bind variables instead.

Upvotes: 10

Related Questions