View Single Post
Old 08-02-2021, 12:58 PM  
vdbucks
Monger Cash
 
Industry Role:
Join Date: Jul 2010
Posts: 2,773
Quote:
Originally Posted by Publisher Bucks View Post
Any programmers able to see what I'm fucking up?

This is for a personal project I'm working on.

I should be able to open index.php to display a listing of 'recipes' in the database which will allow me to click through to viewmore.php where i have more information displayed on the page.


Which should, allow me to generate a link to viewmore.php?id=46 to show the data in the sql for that row, correct?



I'm sure its a simple issue that I'm missing but I've been up almost 48 hours and my mind just isn't seeing it :/

Any help would be greatly appreciated.
There are a handful of things wrong here...

1. Your stated goal is to display a list of recipes on index.php with a link to the individual recipe info on viewmore.php. The problem is, your index.php script is expecting a recipe ID to be passed as a query string, ala index.php?id=123.

2. Your SELECT query on index.php only ever asks for 'ReceipID' from the db, but you're attempting to display other data. Not to mention, you're trying to get the ReceipID from the db while also telling the db what the RecipeID is.

3. Your output on index.php and viewmore.php are effectively the same, assuming the queries worked.

4. You're using <table> in 2021.

At any rate, going by what your stated goal is, I think something like this is more in line with what you want:

Code:
<?php
/*** index.php ***/

	// change hostname, username, password, and dbname
	$db = new mysqli( 'hostname', 'username', 'password', 'dbname');
	if ($db->connect_errno)
		die("Connect failed: %s\n", $db->connect_error);
	 
	$getRecipes = "SELECT ReceipID, Title FROM Recipes";
	$response = $db->query($getRecipes);

	if ($response->num_rows) {
		echo "<table width=100%>
				<tr>
				<th>ID</th>
				<th>Title</th>
				</tr>";

		$row = $db->get_results($response);

		foreach($row as $key => $value) {

			$id = $value->RecipeID;
			$title = $value->Title;

			echo "<tr>";
			echo "<td>{$id}</td>"; // not sure why you want to display the recipe id, but okay.
			echo "<td> <a href=\"viewmore.php?id={$id}\">{$title}</a></td>";
			echo "</tr>";
		}

		echo "</table>";
	}

	$db->close();
?>
Code:
<?php
/*** viewmore.php ***/

	// change hostname, username, password, and dbname
	$db = new mysqli( 'hostname', 'username', 'password', 'dbname');
	if ($db->connect_errno)
		die("Connect failed: %s\n", $db->connect_error);

	$id = $_GET['id'];

	$getRecipe = "SELECT * FROM Recipes WHERE ReceipID = {$id}";
	$response = $db->query($getRecipe);

	if ($response->num_rows) {
		echo "<table width=100%>
				<tr>
				<th>Title</th>
				<th>Ingredients</th>
				<th>Method</th>
				<th>Keywords</th>
				</tr>";

		$row = $db->get_results($response);

		foreach($row as $key => $value) {
			echo "<tr>";
			echo "<td>{$value->Title}</td>";
			echo "<td>{$value->Ingredients}</td>";
			echo "<td>{$value->Method}</td>";
			echo "<td>{$value->Keywords}</td>";
			echo "</tr>";
		}
		echo "</table>";
	}

	$db->close();
?>
vdbucks is offline   Share thread on Digg Share thread on Twitter Share thread on Reddit Share thread on Facebook Reply With Quote