Update where id = $GET[‘id’]

I am trying to create a successful update, using the following HTML:

<form id="form2" name="form2" method="post" 
  onsubmit="return validateForm();" action="">

    Id <input type="text" class="txt" name="id" />
    <br />
    Name <input type="text" class="txt" name="name" />
    <br />
    Website <input type="text" class="txt" name="website" />
    <br />
    Description <input type="text" class="txt" name="description" />
    <br />
    <input type="submit" id="submit" value="Submit"/>
</form>

I then use the following PHP to read the value and update my database:

Read More
<?php
  global $wpdb;
  if (isset($_GET['id']) && !empty($_GET['id']) &&
    isset($_POST['name']) && !empty($_POST['name']) &&
    isset($_POST['website']) && !empty($_POST['website']) &&
    isset($_POST['description']) && !empty($_POST['description']))
  {
      $wpdb->query("update where id = $_GET['id'] ".PRO_TABLE_PREFIX
            ."tutorial (name, website, description) "
            ."values('{$_POST['name']}', '{$_POST['website']}', '{$_POST['description']}')");
  }
?>

What am I doing wrong?

Related posts

Leave a Reply

5 comments

  1. <form id="form2" name="form2" **method="post"** onsubmit="return validateForm();" action="">
    

    Please note that your form method is post

    and you are trying to fetch data using get method

    isset ( $_GET['id'] ) && ! empty ( $_GET['id'] )

    replace these with $_POST[‘key_name];
    to get proper results.

  2. use this your update query is not correct syntax

    $wpdb->query("update ".PRO_TABLE_PREFIX."tutorial set  name='{$_POST['name']}', website='{$_POST['website']}', description= '{$_POST['description']}' where id= $_GET['id'] ) ");
    
  3. If you want the ID to be in $_GET[‘id’], then form action should be like this

    action = "<page-url>?id=<somevalue>"
    

    Also, you have to create a textbox to input id and the for the form should be changed according to the input there, using jQuery or Javascript.

    Finally, if this is too complicated, change $_GET[‘id’] to $_POS[‘id’] as the others suggested.

    Also, the SQL query is incorrect. Others have already pointed it out.

  4. Try with POST id

    <?php
         global $wpdb;
    if ( isset ( $_POST['id'] ) && ! empty ( $_POST['id'] ) &&
     isset ( $_POST['name'] ) && ! empty ( $_POST['name'] ) &&
     isset ( $_POST['website'] ) && ! empty ( $_POST['website'] ) &&
     isset ( $_POST['description'] ) && ! empty ( $_POST['description'] ))
    {
    $wpdb->query("INSERT ".PRO_TABLE_PREFIX."tutorial ( name, website, description )
    values('{$_POST['name']}','{$_POST['website']}','{$_POST['description']}') ");
    }
    ?>
    
  5. 2 Things.

    1) Since your form method is POST, you would have to use $_POST['id'].

    2) Inside your query string, your mysql UPDATE syntax as wrong as well as to use an array with key inside a string, you have to wrap it in { and } tags. This should work:

    $wpdb->query("UPDATE " . PRO_TABLE_PREFIX . "tutorial SET name='{$_POST['name']}', website='{$_POST['website']}', description='{$_POST['description']}' WHERE id={$_POST['id']}");