![]() |
Having PHP Issues with Displaying SQL Data on a Dynamic Web Page.
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. PHP Code:
This is my viewmore.php page code... PHP Code:
Any help would be greatly appreciated. |
// change the user_name and password
$db = mysqli_connect("localhost", "dbuser", "dbpass"); // change the database_name mysqli_select_db("dbname",$db); Did you put in the right info? |
Quote:
I do know my host just recently upgraded a bunch of php so I also have a ticket in with them to make sure they didn't fuck anything up (its Hostgator who I use for my personal stuff). |
I don't think it is making the database connection in the viewmore.php or the index.php file.
I don't believe the $con has actually been defined for the db connection. Just my 2 cents. :) |
Quote:
|
Quote:
https://www.php.net/manual/en/mysqli.connect-error.php |
Nevermind, I just actually looked at your code.
You called your db connection $db but are called $con in your mysqli_query $con = mysqli_connect("localhost", "dbuser", "dbpass"); mysqli_select_db("dbname",$con); You also need to establish the db connection on the viewmore.php file |
This is just wrong unless I am really missing something.
You select recipeid from the table but then you are trying to echo out variables before you have them. You are treating the href as if it is an include. while($row = mysqli_fetch_array($result)) { // Here you pull in the ID field. $id = $row['RecipeID']; echo "<tr>"; echo "<td>" . $row['RecipeID'] . "</td>"; //Everything is fine till here. // You attempt to build your href but it isnt going to work because // you attempt to echo out title when you don't have the title yet. echo "<td> <a href='viewmore.php?id=$id'>" . $row['Title'] . "</a> </td>"; // Then you attempt to echo out the other fields when, again, you don't have them yet. echo "<td>" . $row['Ingredients'] . "</td>"; echo "<td>" . $row['Method'] . "</td>"; echo "<td>" . $row['Keywords'] . "</td>"; echo "</tr>"; } echo "</table>"; .................................................. ...................... Try something like this instead: $result = mysqli_query($con,"SELECT RecipeID, Title FROM Recipes WHERE id = $id"); echo "<table width=100%> <tr> <th>ID</th> <th>Title</th> </tr>"; while($row = mysqli_fetch_array($result)) { $id = $row['RecipeID']; echo "<tr>"; echo "<td>" . $row['RecipeID'] . "</td>"; echo "<td> <a href='viewmore.php?id=$id'>" . $row['Title'] . "</a> </td>"; echo "</tr>"; } echo "</table>"; Then, in viewmore, as k0nr4d said, you need to establish a connection. Not sure I caught all of it but that is the way I see it, like I said, unless I missed something somewhere. . |
Appreciate the help guys, going to play around a little and see if I can put a working code together based on your feedback, thank you!
|
It seems i was too late on party :D Still, always check how your variables contain data/functions.
|
Ive edited the code as suggested for both pages and now its throwing out a blank page with the 'ID' and 'Title' table but no data being inserted, i checked the error logs and its showing me this:
Quote:
index.php PHP Code:
PHP Code:
The url im using is: domain.com/index.php?id=X (where X is a valid RecipeID in the table) domain.com/viewmore.php?id=X (where X is a valid RecipeID in the table) I'm going to get a few hours sleep, but if ya'll happen to spot anything amiss it'd be appreciated. Thanks again for the help so far :thumbsup |
mysqli_select_db("dbname",$con);
Should be mysqli_select_db($con,"dbname"); PHP.net is your friend. https://www.php.net/manual/en/mysqli.select-db.php Quote:
|
Quote:
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 Code:
<?php |
LOL, miss a lot of stuff when you move too fast.
. |
Quote:
|
All times are GMT -7. The time now is 12:23 PM. |
Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc123