12-16-2009, 11:49 AM
Ok i want to make my code more flexible, and easier to configure. Also maybe a bit neater
admin_panel.php
movie_add.php
movie_add.inc.php
stock_update.php
stock_update.inc.php
config/config.php
database/database.php
database/settings.inc.php
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
?>