www.webdeveloper.com
Results 1 to 4 of 4

Thread: UPDATE query + using GET & POST method at once?

  1. #1
    Join Date
    Sep 2013
    Posts
    18

    UPDATE query + using GET & POST method at once?

    Hi everyone here, I have a table that its content is what I have in DB (mine: easyPHP), here I wanted to edit the content of the table in browser & then by clicking the update button, I should get success to update the new information in my Data Base. in my <form> I've used POST method before for another purposes I had before, but now for UPDATE I need to use GET method. I'm actually new in PHP & PDO & everything. I think that I've tried myself to figure out how to write UPDATE query or how to use POST & GET method together, but when I run code , I get no Error! but also with no results. I get confused! I need your help.Thank you

    here I show you my code (but not the full code, cause it's not necessary) :

    this one is my form:
    PHP Code:
            <form method="POST" action="index.php?get=variables">
                <table border="1 solid #000000">
                    <tr>
                        <td>row</td>
                        <td>title</td>
                        <td>content of task</td>
                        <td>delete</td>
    <!--                    <td>Edit</td>-->
                        <td>Update</td>
                    </tr>
                    <?php
                    $MyTask 
    "SELECT id,title, content_task FROM `task` " ;
                    
    $query=$conn->prepare ($MyTask);
                    
    $query->execute( );
                    
    $task_panel $query->fetchAll(PDO::FETCH_ASSOC);
                    
    // print_r($task_panel);
                    // Array ( [title] => web [0] => web [content_task] => task [1] => task )
                    // Array ( [title] => web [content_task] => task )

                    //                print_r ($query->fetchAll(PDO::FETCH_ASSOC));
                    //                print_r($query->errorInfo());
                    
    $n 0;
    //                print_r($task_panel);
                    
    foreach($task_panel as $row)
                    {
                        echo 
    '<tr>
                                    <td>'
    .++$n .'</td>
                                    <td><p contenteditable>'
    .$row['title'].'</p></td>
                                    <td><p contenteditable>'
    .$row['content_task'].'</p></td>
                                    <td><input name="checkbox[]" type="checkbox" value="'
    .$row['id'].'"></td>
                                   <!--<td><button name="edit[]" type="submit" value="'
    .$row['id'].'">edit</td>-->
                                    <td><button name="update[]" type="submit" value="'
    .$row['id'].'">update</td>
                                </tr>'
    ;
                    }
                    
    ?>


                </table>
                <button type="submit" name="delete" value="delete">delete</button>
            </form>
    & this one is what I've tried to write the UPDATE query:

    PHP Code:
    //if (isset($_GET['update']))
    if($_SERVER['REQUEST_METHOD'] == 'GET')
    {
        
    $update $_GET['update'];

        
    $up_id implode(','$update);
        
    $upt "UPDATE  task SET `title` =:'title', `content_task`=:'content_task' WHERE id IN ($up_id)";
        
    $upp $conn->prepare($upt);
        
    $upp->execute($upt);



  2. #2
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,334
    Why are you checking for GET when your form tag says method="POST"?

    Also, the ":" character needs to be inside of the quotes in the query, e.g. ':title', not :'title'. However, that then means you have to bind those parameters to a PHP variable, either via the PDOStatement::bindParam() method, or by using them as keys in an array of input data as the second argument to PDOStatement::execute().
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

  3. #3
    Join Date
    Sep 2013
    Posts
    18
    well, I should use GET, cause it's a task that someone offers me to do to learn it, it can be good for me , cause in this way I would learn how to use both GET & POST together. & about UPDATE query I have no idea how to change it if necessary. & I surely know there's something wrong about 'update', cause it seems changes cannot be set in data base.

  4. #4
    Join Date
    Aug 2004
    Location
    Ankh-Morpork
    Posts
    19,334
    You should only use GET for read-only requests. Since this is an update request which will change data, POST is the correct option. (One reason being that if it's done as a GET request, the user can bookmark the resulting URL, and accidentally (or intentionally) just keep doing the same update every time s/he selects that bookmark or refreshes the page, without any indication that that is what will happen.) But if you really feel the need to use GET for educational purposes, just change the form tag's method value to "GET".

    If unsure about how to handle bound parameters in PDO, the manual is your best friend.
    "Please give us a simple answer, so that we don't have to think, because if we think, we might find answers that don't fit the way we want the world to be."
    ~ Terry Pratchett in Nation

    eBookworm.us

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles