Michiel
Michiel

Reputation: 8103

Update only ONE table in database

Whenever I execute an update-query, my whole table is updated. What do I have to do when I just want ONE value to be updated?

Here is my database structure:

ID   ||     photo     ||   sequence
1    ||   test.png    ||      1
2    ||   bla.png     ||      2

Whenever I execute this script,

if (isset($_POST['submitted'])) {
    $project = new Project();

    $project->sequence  = $_POST['sequence'][$key];
    $projectid          = $_POST['photoid'];

    if($project->updateProject($_DB, $projectid)) {
        $feedback = "OK";
    } else {
        $feedback = "NOT OK";
    }
}

Results in this:

ID   ||     photo     ||   sequence
1    ||               ||      4
2    ||               ||      2

So, what do I have to do to just update the sequence-value in the database without touching the rest of the data in the database...

FUNCTION:

public function updateProject($db, $id) {
        $sql = "UPDATE tblProject SET 
            sequence = '".$db->escape($this->sequence)."'
        WHERE id = '".$id."'";
        return $db->insert($sql);
    }

INSERT FUNCTION:

  public function insert($sql) {
    mysql_query($sql, $this->_connection);
    return mysql_affected_rows($this->_connection);
  }

Upvotes: 1

Views: 112

Answers (2)

xkeshav
xkeshav

Reputation: 54050

There must be a problem with your $project->updateProject() function.

Try with simple query:

$qry = "UPDATE tblProject SET sequence = '".$project->sequence."' 
        WHERE ID =".(int)$projectid.";
mysql_query($qry);

Upvotes: 1

Mohammad Saberi
Mohammad Saberi

Reputation: 13166

Basically, whenever you get this problem, you are updating your table without any WHERE clause. Like the code below:

UPDATE myTable SET myField = 'newValue';

In this case, all of your stored records will update with the new value.

Use a WHERE clause in your query to update just one or some specified records.

UPDATE myTable SET myField = 'newValue' WHERE tableId = 'yourId';

Upvotes: 1

Related Questions