r/DatabaseHelp • • Mar 23 '15

Can you build a relational database using WAMP?

I have been given a project to design a food calculator, and the way i was think about setting the tables out is to build one that has all the meals and another that has all the ingredients and get it to reference that table when the user clicks the meal, so it will display all the ingredients and what they cost. Is this possible? or do I need to rethink?

Any help/tutorials would be appreciated.

2 Upvotes

13 comments sorted by

View all comments

Show parent comment

1

u/insert-team-name May 15 '15 edited May 15 '15

Yeah it is just for homework, its a dish costing system, so a meal will be linked to ingredients and if there price changes all prices will change. That's basically my tables, i just cannot figure you how to display a way for the user to select ingredients and then match them with the meal and put them in the mealingredients table.

[http://imgur.com/ovPm9Sl]

So far my code is

    <html>
	
	<head>
	<meta http-equiv='Content-Type' content='text/html; charset=utf-8' />
	<meta http-equiv='X-UA-Compatible' content='IE=edge,chrome=1' />
	<!--<link rel='stylesheet' type='text/css' href='styles.css' />-->
	<link href="Style/Style.css" rel="stylesheet" type="text/css"/>
	<script type="text/javascript" src="prototype.js"></script>
	
	<style>
	body
	{
	background: url(img/plane1.jpg) no-repeat center center fixed; 
	webkit-background-size: cover;
	moz-background-size: cover;
	o-background-size: cover;
	background-size: cover;

	}
	
	
	</style>


	
	</head>

	<body>
	<div id="greeting">

	<div id='cssmenu'>
	<ul>
	<li class='active'><a href='viewall.php'><span>Home</span></a></li>
	<li class='has-sub'><a href='viewall.php'><span>Options</span></a>
      <ul>
         <li class='last'><a href='viewall.php'><span>View all Meals</span></a></li>
		 <li class='last'><a href='add.php'><span>Add Meal</span></a></li>
		 <li class='last'><a href='inadd.php'><span>Add Ingredients</span></a></li>
		 <li class='last'><a href='inviewall.php'><span>Edit Ingredients</span></a></li>

      </ul>
	</li>
		 <li class='last'><a href='.php'><span>N/A</span></a></li>
	 </li>
	<li class='last'><a href='logout.php'><span>Logout</span></a></li>
	</ul>
	</div>
	<div id='page-wrap' >
	
		<?php
$dbhost = 'localhost';
	$dbuser = 'root';
	$dbpass = '';
	$db = 'test';
	$conn = mysqli_connect($dbhost, $dbuser, $dbpass, $db);
	if (mysqli_connect_errno())
  {
  echo "Failed to connect to MySQL: " . mysqli_connect_error();
  }
  
  $SQLDROP = "SELECT ";
  
  
if ($_SERVER['REQUEST_METHOD'] == 'POST')
	{
	//$MealID = $_POST['MealID'];
	$Meal = $_POST['Meal'];
	$Price = $_POST['MealPrice'];

	if ($db) {
	var_dump($db);
	$SQL = "INSERT INTO meal (Meal, MealPrice) VALUES ('" . $Meal . "', '" . $Price . "')";
	//$SQL = "INSERT INTO mealingredients (MealID, InID) VALUES ('" . $MealID . "', '" . $InID . "')";
	//$SQL = "SELECT InID FROM ingredients WHERE Name in (the list of ingredients the user selected);
	
	$result = mysqli_query($conn, $SQL);
	mysqli_close($conn);
	}
	else {

	print "Database NOT Found ";
	mysqli_close($conn);
	}
	}
	/*for each InID in ingredientIDqueue do
	{
     $sql = "INSERT INTO mealingredients (MealID, InID) values (($maxMealID + 1), $InID)";
	}*/

?>

<!DOCTYPE html>
<html>
<head>
</head>
<body>

<form method="post" action="<?php echo $_SERVER['PHP_SELF'] ;?>" id="Add">
<input type="hidden" name="MealID" />
<br />
<h3>Add Meal</h3>
Meal name:<br />
<input type="text" name="Meal" />
<br />
Meal Price:<br />
<input type="text" name="MealPrice" />
<br />
<br />
<input type="submit" name="submit" value="Add Meal">
</form>
<a href='viewall.php'>Return to view all meals<a/>
</body>
</html>
	</div>

	</div>

	</body>
</html>

1

u/BinaryRockStar May 15 '15

You haven't given any filenames or anything so it's difficult to tell what's where. What file(s) have you given me the contents of? Throwing it up on GitHub would be easier.

What problem do you currently have?

1

u/insert-team-name May 15 '15

This is the add page. I'm trying to display ingredients and allow the user to select the ones they want and then click the add button and it will take the ingredients ID and Meal ID and put them into the mealingredients table to create a link that will show on my output page.

1

u/BinaryRockStar May 15 '15

So you will need to do a loop in PHP and output an <input type="checkbox" .../> element for each of the possible ingredients. The user can select whichever of the checkboxes they like to indicate which ingredients are included in the meal.

The processing part of your script (if method == "POST") will then need to do another loop checking if each ingredient is selected and INSERT a row into the DB for each selected ingredient.

1

u/insert-team-name May 15 '15

this is my code so far, but its throwing an error of Notice: Object of class mysqli_result could not be converted to int in C:\wamp\www\TheSunInn\add.php on line 121.

	$sqlin = "SELECT * FROM ingredients";
	$result = mysqli_query($conn, $sqlin);

	if ($result != 0) {
		echo '<label>Ingredients:';
		echo '<select name="InName">';
		echo '<option value="">all</option>';

		$num_results = mysqli_num_rows($result);
		for ($i=0;$i<$num_results;$i++) {
			$row = mysqli_fetch_array($result);
			$name = $row['InName'];
			echo '<option value="' .$InName. '">' .$InPrice. '</option>';
		}

		echo '</select>';
		echo '</label>';

1

u/BinaryRockStar May 15 '15

It's probably this line:

 if ($result != 0) {

Why are you comparing it to zero? What resource are you using to learn or copy from? From what I'm reading it should be:

 if ($result != FALSE) {

The next issue is that you're referring to $InName and $InPrice without defining them anywhere, and you're never referring to $name after initialising it.