Jump to content

PHP & mySQL .. easier/cleaner way to code this?


azukah

Recommended Posts

HI,

My code works but I was wondering if there's a simpler/cleaner way to code it ... :shrug:

 

<?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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.