azukah Posted September 7, 2011 Share Posted September 7, 2011 HI, My code works but I was wondering if there's a simpler/cleaner way to code it ... <?php require_once('config.php'); // code to get data form database mysql_select_db($database, $makeconnection); $sql_get_categories = " SELECT * FROM tbl_categories ORDER BY category_id ASC"; $get_categories = mysql_query($sql_get_categories, $makeconnection) or die(mysql_error()); $row_get_categories = mysql_fetch_assoc($get_categories); $totalRows_get_categories = mysql_num_rows($get_categories); // i named the 3 fields form the form in the HTML so i can update them into the tadabase $category_1 = $_POST['category_1']; $category_2 = $_POST['category_2']; $category_3 = $_POST['category_3']; //updating the databse if (isset($_POST['submitted_categories'])&&($_POST['submitted_categories'] == "yes")) { $register_query = "SELECT category_id FROM tbl_categories WHERE category_id='$category_id'"; mysql_select_db($database, $makeconnection); $sql_modify1 = ("UPDATE `tbl_categories` SET `category_name` = '$category_1' WHERE `category_id` =1"); $sql_modify2 = ("UPDATE `tbl_categories` SET `category_name` = '$category_2' WHERE `category_id` =2"); $sql_modify3 = ("UPDATE `tbl_categories` SET `category_name` = '$category_3' WHERE `category_id` =3"); $Result1 = mysql_query($sql_modify1, $makeconnection) or die(mysql_error()); $Result2 = mysql_query($sql_modify2, $makeconnection) or die(mysql_error()); $Result3 = mysql_query($sql_modify3, $makeconnection) or die(mysql_error()); header ("Location: the-rest.php"); } ?> here's the form in the html part <!--CATEGORIES --> <h2>Categories</h2> <form action="" method="post" enctype="multipart/form-data" name="category-form" id="category-form"> <?php do { ?> <h3><input class="user-form-input" id="category_<?php echo $row_get_categories['category_id'];?>" name="category_<?php echo $row_get_categories['category_id'];?>" type="text" value="<?php echo $row_get_categories['category_name'];?>" /></h3> <?php } while ($row_get_categories = mysql_fetch_assoc($get_categories)); ?> <input name="submitted_categories" type="hidden" id="submitted_categories" value="yes" /> <input name="submit" type="submit" class="button-save" id="submit" value="update categories"/> </form> <!--END OF CATEGORIES--> Link to comment https://forums.phpfreaks.com/topic/246654-php-mysql-easiercleaner-way-to-code-this/ Share on other sites More sharing options...
btherl Posted September 8, 2011 Share Posted September 8, 2011 You could abstract out the updates into a function, so your main code would have 3 calls to that function instead of 3 queries followed by 3 query executions. That's a small gain in readability. Also I don't see where $register_query is used. And you shouldn't need to mysql_select_db() a second time. Link to comment https://forums.phpfreaks.com/topic/246654-php-mysql-easiercleaner-way-to-code-this/#findComment-1266692 Share on other sites More sharing options...
azukah Posted September 8, 2011 Author Share Posted September 8, 2011 thanks for advice -- Link to comment https://forums.phpfreaks.com/topic/246654-php-mysql-easiercleaner-way-to-code-this/#findComment-1267090 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.