Support Forums

Full Version: Help cleaning up code
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Ok i want to make my code more flexible, and easier to configure. Also maybe a bit neater Smile

admin_panel.php
Code:
<?php
echo "<center>";
        echo "<h1>" . "Admin Panel" . "</h1>" . "<br />";

// Add movie        
echo "<fieldset style=\"width:25%\">";
    include("movie_add.inc.php");
echo "</fieldset>";
echo "<br />";

// Update Stock
echo "<fieldset style=\"width:25%\">";
    include("Stock_update.inc.php");
echo "</fieldset>";

echo "</center>";
?>

movie_add.php
Code:
<?php
include("database/database.php");
include("config/config.php");

// Set variables
$movie = mysql_real_escape_string($_POST['movie']);
$descr = mysql_real_escape_string($_POST['descr']);
$stock = mysql_real_escape_string($_POST['stock']);

// Insert set values
if ($movie == "" or $descr == "" or $stock == "") {
echo "INPUT ERROR";
} else {
mysql_query("INSERT INTO movies
(movie, description, stock) VALUES ('$movie', '$descr', '$stock') ")
or die(mysql_error());
echo "<b>" . $movie . "</b>" . " Added";
}
echo "<br />" . $___adminlink;
?>

movie_add.inc.php
Code:
<html>
<form action="movie_add.php" method="post">
            <font size="+1"><b><u>Add Movie</u></b></font>
            <br />
            Movie:<center><input type="text" name="movie"></center>
        <br />
            Description:<center><td><input type="text" name="descr"></center>
        <br />
            Stock:<center><input type="text" name="stock"></center>
        <br />
            <INPUT TYPE=SUBMIT>
</form>
</html>

stock_update.php
Code:
<?php
include("database/database.php");
include("config/config.php");

echo "<center><font size=\"+1\"><b><u>Stock Update</u></b></font></center>";

$movie_id = mysql_real_escape_string($_POST['movie']);

if($movie_id) {
  // Get the movie
  $sql_result = mysql_query("SELECT * FROM movies WHERE movie = '".$movie_id."' LIMIT 1");

  // Get array
  if(mysql_num_rows($sql_result)>0) {
    $movie = mysql_fetch_array($sql_result);
    // Put code here for administration or anything else.. Example to display the stock count:
    echo $movie_id . ": ";
    echo $movie['stock'];
  } else {
    echo "Movie with such ID not found.";
  }
} else {
echo "No movie ID has been given.";
}

if(mysql_error()) {
  echo mysql_error();
}
// increase stock variables
$incre_id = mysql_real_escape_string($_POST['incre']);

$newstock = $movie['stock'] + $incre_id;
// increase
mysql_query("UPDATE movies SET stock = '".$newstock."'
WHERE movie = '$movie_id'");
echo "<br />" . "New stock: " . $newstock;
echo "<br />" . "$___adminlink";

?>

stock_update.inc.php
Code:
<html>
<form action="stock_update.php" method="post">
            <font size="+1"><b><u>Update Stock</u></b></font>
            <br />
            Movie:<center><input type="text" name="movie"></center>
        <br />
            Increase By:<center><input type="text" name="incre"></center>
        <br />
            <INPUT TYPE=SUBMIT>
</form>
</html>

config/config.php
Code:
<?php
$___url = "localhost";
$___adminlink = "<a href=\"" . $url . "/admin_panel.php\">Admin Panel</a>";
?>

database/database.php
Code:
<?php
        include("settings.inc.php");
        $connection = mysql_connect($CONFIG['DBHOST'],
        $CONFIG['DBUSER'],
        $CONFIG['DBPASS']) or die("Could not establish a connection with mysql.");
        mysql_select_db($CONFIG['DBNAME']) or die("Unable to select database.");
?>

database/settings.inc.php
Code:
<?php
        $CONFIG['DBHOST'] = "localhost"; // Database servers location, normally localhost
        $CONFIG['DBUSER'] = "root"; // sql user
        $CONFIG['DBPASS'] = ""; // db users password
        $CONFIG['DBNAME'] = "movies"; // Database to connect to
?>
What's the problem, it looks to me like it does what it should!
I don't really see needed changes.
To make it more flexible and clean it up a bit, i mean it seems ok for me, but im a newbie so more advance PHP coders may look at it and think "could be better here" etc
I have something other to do, but I'll look at it soon!
Until then I hope someone offers support sooner! Big Grin
Thanks Smile

Updated: movie_add.php
added: validation rules

Code:
<?php
include("database/database.php");
include("config/config.php");

// Set variables
$movie = mysql_real_escape_string($_POST['movie']);
$descr = mysql_real_escape_string($_POST['descr']);
$stock = mysql_real_escape_string($_POST['stock']);

// Insert set values
if ($movie == "" or $descr == "" or $stock == "") {
echo "INPUT ERROR";
} else {
mysql_query("INSERT INTO movies
(movie, description, stock) VALUES ('$movie', '$descr', '$stock') ")
or die(mysql_error());
echo "<b>" . $movie . "</b>" . " Added";
}
echo "<br />" . $___adminlink;
?>
None of your code appears to be xhtml compatible. I suggest you look into that.

stock_update.inc.php should be rewritten.
You should also get into the habit of tabbing your code.
That would mean adding a tab to every line that appears inside certain braces.

i.e.
Code:
<?php
class my_tabs {
     function hey_look_im_tabbed() {
          echo "two tabs for two braces.  ;)";
     }
} ?>