arjang Posted July 8, 2011 Share Posted July 8, 2011 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 Quote Link to comment https://forums.phpfreaks.com/topic/241452-help-with-post-and-get/ Share on other sites More sharing options...
Cineex Posted July 9, 2011 Share Posted July 9, 2011 I can't see any problem there, how dose the HTML look like ? Quote Link to comment https://forums.phpfreaks.com/topic/241452-help-with-post-and-get/#findComment-1240325 Share on other sites More sharing options...
Network_ninja Posted July 9, 2011 Share Posted July 9, 2011 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=''> Quote Link to comment https://forums.phpfreaks.com/topic/241452-help-with-post-and-get/#findComment-1240370 Share on other sites More sharing options...
Pikachu2000 Posted July 9, 2011 Share Posted July 9, 2011 When posting code, enclose it within the forum's . . . BBCode tags, especially when it's more than just a line or two. Quote Link to comment https://forums.phpfreaks.com/topic/241452-help-with-post-and-get/#findComment-1240373 Share on other sites More sharing options...
arjang Posted July 9, 2011 Author Share Posted July 9, 2011 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> Quote Link to comment https://forums.phpfreaks.com/topic/241452-help-with-post-and-get/#findComment-1240376 Share on other sites More sharing options...
Network_ninja Posted July 9, 2011 Share Posted July 9, 2011 I can't see any problem with your code if your current url looks like this: http://sitename/pagename.php?kurdish_id=value Quote Link to comment https://forums.phpfreaks.com/topic/241452-help-with-post-and-get/#findComment-1240385 Share on other sites More sharing options...
arjang Posted July 9, 2011 Author Share Posted July 9, 2011 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(); } ?> Quote Link to comment https://forums.phpfreaks.com/topic/241452-help-with-post-and-get/#findComment-1240403 Share on other sites More sharing options...
arjang Posted July 9, 2011 Author Share Posted July 9, 2011 any help please ??? Quote Link to comment https://forums.phpfreaks.com/topic/241452-help-with-post-and-get/#findComment-1240566 Share on other sites More sharing options...
cyberRobot Posted July 9, 2011 Share Posted July 9, 2011 Have you tried using var_dump() to see if the variable contain what you think they should? http://php.net/manual/en/function.var-dump.php Quote Link to comment https://forums.phpfreaks.com/topic/241452-help-with-post-and-get/#findComment-1240575 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.