Jump to content

help with POST and GET


arjang

Recommended Posts

hi :) i have a database with two tables and i use one form to update both tables depending on GET parameter. if get=kurdish_id then i get fields from kurdish table and vise versa for english table. my code retrieves the right table fields but when i click on Update Article, only english table is updated for both tables. i dnt know where the problem is! i am adding the code so that you can see where the problem is.

here is the code:

 

<?php
   $editFormAction = $_SERVER['PHP_SELF'];
   $colname_en_list = "-1";
   $colname_ku_list = "-1";
   if (isset($_GET['kurdish_id'])) {
      $colname_ku_list = mysql_prep($_GET['kurdish_id']);
      $colname_en_list = NULL;
   }elseif (isset($_GET['english_id'])) {
      $colname_en_list = mysql_prep($_GET['english_id']);
      $colname_ku_list = NULL;
   }   
   
   if(isset($_POST['update'])){
   $errors = array();
   $required_fields = array('title', 'author', 'content');
            foreach($required_fields as $fieldName){
               if(!isset($_POST[$fieldName]) || empty($_POST[$fieldName])){
                  $errors[] = $fieldName; 
               }
            }   
      $field_length = array('title' =>150);
      foreach($field_length as $fieldname => $max_length){
         if(strlen(trim(mysql_prep($_POST[$fieldname]))) > $max_length){
         $errors[] = $fieldname;
         }
      }
      if(empty($errors)){
      
          $id      = mysql_prep((int)$_POST['id']);
         $title   = mysql_prep(trim($_POST['title']));
         $author = mysql_prep(trim($_POST['author']));
         $content= mysql_prep($_POST['content']);
         
         $sql = "UPDATE "; 
         if($colname_en_list){
         $sql .= "english table ";
         }elseif($colname_ku_list){
         $sql .= "kurdish table ";
         }
         $sql .="SET
                  title   = '$title',
                  author   = '$author',
                  content = '$content'
               WHERE id   = $id";
         $result = mysql_query($sql, $connection);
         if(mysql_affected_rows()==1){
         global $message;
         $message = "The article was successfully updated.";
         }else{
         $message = "Update was not successful" . "<br />";
         $message .= mysql_error();
         }
      }else{
         $message = "there were ". count($errors) . " errors in the form";
         }   
   }

$query_en_list ="SELECT * FROM ";
            if($colname_en_list!=null){
            $query_en_list.="english table ";
            $query_en_list.="WHERE id = $colname_en_list";
            }
            elseif($colname_ku_list!=null){
            $query_en_list.="kurdish table";
            $query_en_list.="WHERE id = $colname_ku_list";
            }

$en_list = mysql_query($query_en_list, $connection) or die(mysql_error());
$row_en_list = mysql_fetch_assoc($en_list);
$totalrow_en_list_en_list = mysql_num_rows($en_list);   
?>

 

MOD EDIT: code tags added

Link to comment
https://forums.phpfreaks.com/topic/241452-help-with-post-and-get/
Share on other sites

Would you mind posting also your form?

 

What is the method of your form? post or get?

You can access the value of your form using $_POST or $_GET depending of the method you set in your form.. If you set it to post then your $_GET has no value at all...

 

<form method='post' action=''>

the list of articles are on another page with link to update.php using get method. im using POST method in the form. if i click on any of the two table's article list, the form fields get filled with the right table fields from database but when i click on update button, only english table is updated.

 

  <form action="<?php echo $editFormAction; ?>" method="post" name="form1" id="form1">
    <table align="center">
      <tr valign="baseline">
        <td align="right" nowrap="nowrap"> </td>
        <td><input name="id" type="hidden" id="id" value="<?php echo $row_en_list['id']; ?>" /></td>
      </tr>
      <tr valign="baseline">
        <td nowrap="nowrap" align="right">Title:</td>
        <td>
          <input type="text" name="title" value="<?php echo htmlentities($row_en_list['title'], ENT_COMPAT, 'utf-8'); ?>" size="32" />
        </td>
      </tr>
      <tr valign="baseline">
        <td nowrap="nowrap" align="right">Author:</td>
        <td>
          <input type="text" name="author" value="<?php echo $row_en_list['author']; ?>" size="32" />
        </td>
      </tr>
      <tr valign="baseline">
        <td nowrap="nowrap" align="right" valign="top">Content:</td>
         <textarea name="content" cols="50" row="5"><?php echo htmlentities($row_en_list['content'], ENT_COMPAT, 'utf-8'); ?></textarea>
        </td>
      </tr>
      <tr valign="baseline">
        <td nowrap="nowrap" align="right"> </td>
        <td><input name="update" type="submit" id="update" value="Update article" /></td>
      </tr>
    </table>
    <input type="hidden" name="id" value="<?php echo $row_en_list['id']; ?>" />
  </form>

i think there is a problem with get method. the $_GET[english_id] never turns to null so elseif statement never turns to true. for example, if i update an article with id=1 for both tables, only english table is updated. if i update an article from kurdish table with an id that doesnt exist in english table, the else tsatement below equals to true but without any mysql error. i mean only the first part of message is displayed.

<?php
else{
		$message = "Update was not successful" . "<br />";
		$message .= mysql_error();
		}
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.