简体   繁体   中英

How to update the data stored in mysql database

I have a web page in which a user can 'add' a post or 'update' an existing one. In the code below, my 'add' function works totally fine to add a new post. However, my 'update' function is not working at all. It fails to update the existing post in my data base. Can anyone help me what I am doing wrong? Thank You!!

class ShareModel extends Model{
public function Index(){
       $this->query('SELECT * FROM shares ORDER BY create_date DESC');
       $rows = $this->resultSet();
       return $rows;
}

public function add(){
    // Sanitize POST
    $post = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);

    if($post['submit']){
        if($post['title'] == '' || $post['body'] == '' || $post['link'] ==''){
            Messages::setMsg('Please Fill In All Fields', 'error');
            return;
        }
        // Insert into MySQL
        $this->query('INSERT INTO shares (title, body, link, user_id) VALUES(:title, :body, :link, :user_id)');
        $this->bind(':title', $post['title']);
        $this->bind(':body', $post['body']);
        $this->bind(':link', $post['link']);
        $this->bind(':user_id', 1);
        $this->execute();
        // Verify
        if($this->lastInsertId()){
            // Redirect
            header('Location: '.ROOT_URL.'shares');
        }
    }
    return;
}



   public function update(){

        $post = filter_input_array(INPUT_POST, FILTER_SANITIZE_STRING);

    if($post['update']){

        if($post['title'] == '' || $post['body'] == '' || $post['link'] ==''){
            Messages::setMsg('Please Fill In All Fields', 'error');
            return;
        }



        $title = $post['title'];
        $body = $post['body'];
        $link = $post['link'];


        // Update MySQL
        $this->query('UPDATE shares SET (title, body, link, user_id) VALUES(:title, :body, :link, :user_id) ' );
        $this->bind(':title', $post['title']);
        $this->bind(':body', $post['body']);
        $this->bind(':link', $post['link']);
        $this->bind(':user_id', 1);
        $this->execute(); 

            header('Location: '.ROOT_URL.'shares');

     }
     return;
  } 



}

Your update syntax is off. MySQL's update syntax, which is also what most other databases use, is:

UPDATE some_table
SET col1 = val1, col2 = val2, ...

In other words, each column is assigned an update value directly using equals. Try this version:

$this->query('UPDATE shares SET title = :title, body = :body, link = :link, user_id = :user_id');

Edit: Most of the time, you would also have a WHERE clause with an update, unless you really intend to update every record of the table.

The SQL UPDATE syntax:

The general syntax is:

UPDATE table-name 
SET column-name = value, column-name = value, ...

To limit the number of records to UPDATE append a WHERE clause:

Notice the WHERE clause in the UPDATE syntax : The WHERE clause specifies which record or records that should be updated. If you omit the WHERE clause, all records will be updated!

the full syntax for the MySQL UPDATE statement when updating one table is

 UPDATE table-name 
   SET column-name = value, column-name = value, ...
 WHERE condition

OR

The syntax for the UPDATE statement when updating one table with data from another table in MySQL is:

UPDATE table1
SET column1 = (SELECT expression1
               FROM table2
               WHERE conditions)
[WHERE conditions];

OR

The syntax for the MySQL UPDATE statement when updating multiple tables is:

UPDATE table1, table2, ... 
SET column1 = expression1,
    column2 = expression2,
    ...
WHERE table1.column = table2.column
AND conditions;

Note: Put the echo or var_dump() to the statement and then exit (); the operation over there. After that you will see the query over to the browser and then copy that query and place in the SQL section and execute the query over there. If you fins all the Operations are correct then you remove the exit and echo and then run the code. Hope so this explanation might be clear for your understanding purpose. Otherwise mybe you can print last SQL query string using echo $this->last_query();exit;

By QAL Happy Coding :)

syntax error in your query

 $this->query('UPDATE shares SET (title, body, link, user_id) VALUES(:title, :body, :link, :user_id) ' );

try like this:

 $this->query('UPDATE shares SET title=:title, body=:body, link=:link, user_id=:user_id ');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM