![]() |
What a wild 3 weeks (and php question).
So a few weeks ago I was told I had covid, so after 3 weeks of pure hell, sleeping non-stop, vomiting, aches and pains plus coughing, I finally get the all clear and am back at it.
No clue how I even got it as I have pretty much remained a hermit this whole time. Anyway, I'm trying to get caught up on some of my personal projects, including a little recipe site that I'm having an issue with... I'm using the following code on an index page for a website.. PHP Code:
However, when the link that is generated is clicked on to go to recipes.php?id=4 it keeps telling me the page isnt working. This is the code im using in the recipes.php page, can anyone give me a little guidance as to what is messed up please? PHP Code:
What it *should* be doing is displaying a table of all the data for the MYSQL database recordwith a RecipeID of 4 on that recipes.php page. Any help would be greatly appreciated. Oh and to top the last 3 weeks of dealing with covid off... New Orleans is now expecting to be hit by a cat 4 hurricane and parts of the area have been issued a mandatory evacuation, thankfully I'm in an area where its only voluntary and well inside the levee protection system :thumbsup |
Print $sql and make sure the query is valid...i don't think you are passing the $GET['id'] value. And, you say that you want the RecipeID column but you are querying id column, it will fail because your table doesn't have a column "id".
|
// check incoming params exist
if ( ! isset($_GET['id'] ) { // missing param, go to an error page for example header('Location: index.php'); exit; } is missing a ) |
Another tiny error
" echo $e-getMessage(); " Missing a > |
Quote:
|
Quote:
|
Quote:
I tried changing it in a couple of spots and still running into 500 errors. (Sorry only been working on php for a couple of months so still pretty green with it). Thank you for the explanation :) |
hey if you still live in New Orleans stay safe and good luck! it doesn't look pretty.
:Oh crap # |
Quote:
Its still relatively calm here right now, a little high wind and some light rain, that's about it (so far). A few miles away several of the parishes have lost power and I'm seeing reports of flooding, still nothing majorly serious, we lose power and flood as a city all the time :1orglaugh The media (at this point in time) is definitely playing it up for ratings. |
First, stay safe.
Second, as someone said above, you have an error in your if statement, you are missing a paren. You have: // check incoming params exist if ( ! isset($_GET['id'] ) { // missing param, go to an error page for example header('Location: index.php'); exit; } That should be: // check incoming params exist if ( ! isset($_GET['id'] ) ){ // missing param, go to an error page for example header('Location: index.php'); exit; } Thirdly: In the recipe.php you do not establish a database connection. In the first program you are establishing a mysqli connection. In the Recipe.php you do not establish any database connection and then you try to use a pdo connection that does not exist. Instead of mixing between mysqli and pdo, use one. You are using mysqli in the first program, use that again in the second. You use it at the bottom of the second to iterate through the rows. Put an error_reporting(E_ALL) at the start of the program and it should show you your errors. Once you have everything debugged change that to error_reporting(0) so it won't show the errors. <?php error_reporting(E_ALL); // check incoming params exist if ( ! isset($_GET['id'] ) ){ // missing param, go to an error page for example header('Location: index.php'); exit; } // open a mysqli connection $con=mysqli_connect("localhost","recipes","passwor d","recipelist"); // Check connection if (mysqli_connect_errno()) { die("Failed to connect to MySQL: " . mysqli_connect_error()); } $table = "Recipes"; $sql = "SELECT * FROM" . $table . " WHERE id =" . $_GET['id']; // do the query $result = mysqli_query($con, $sql); echo "<table>"; while($row = mysqli_fetch_array($result)) { echo "<tr>"; echo "<td>" . $row['Title'] . "</td>"; echo "<td>" . $row['Ingredients'] . "</td>"; echo "<td>" . $row['Method'] . "</td>"; echo "</tr>"; } echo "</table>"; ?> Something like that should work. I have not checked to see if there are any other syntax issues. You should also do some validation on $_GET['id']. I used it raw above and that is not really safe. It is fine for right now while you are trying to figure things out but for something open to the net it isn't. For example is the id field an integer? Then you would want to do a validation such as making sure it is an int coming in. $recipeid=0; if(isset($_GET['id'])) { $recipeid=intval($_GET['id']); } if($recipeid==0) { echo "Bad id value coming in or something like that..."; } . |
Quote:
Going to see if I can get this working with those fixes you made and the changes. Out of interest, is there a specific time/reason I should use mysqli vs PDO in this type of scripting? Also, is there any significant difference in using echo vs print for the output data? Winds are starting to pick up, no real damage here as yet though (2 miles outside the Quarter) unless you count a couple of upturned garbage cans that weren't secured properly by neighbors. Power and Internet is still working, there was an advisory an hour or so back not to use dishwashers, washing machines or other appliances that utilize a lot of water due to the sewerage plant system losing power, other than that, nothing major to report here yet. Have enough food, smokes and liquor to last a week if the power does go out and I just finished a huge pan of baked ziti, so should be somewhat comfortable if utilities do get knocked out at this point. Again, thank you so much for your detailed reply, going to play around with this now. #Quick Editing# Yes, RecipeID goes from 001 through 3600 at the present time so will look into validation of that to protect from SQL injection once I have this working how I want before setting the whole thing live. I honestly didn't think this pet project would take so long to get up and running, but I'm glad its finally in the process of being built haha |
edited in: This got posted while you were posting apparently. It is not meant as a response ot your last post but rather a continuation of my post.
.................................................. ... OP, You had posted about this same program a while back. https://gfy.com/fucking-around-and-b...-web-page.html Did you revisit that thread at all? Vdbucks gives a nice solution there. You should study the code that he, I and several other people have shown you. If you don't understand the code that you are looking at, play with it, ask questions, play with it some more until you understand exactly what it is doing and why it is doing it. The recipe.php you posted indicates to me that you did not really examine the code at all before yelling for help. If you had, you would see that the comments in there are trying to guide you through how to use it but are not actually providing the code you should be using. For example, it says: Quote:
A programmer should never use code that they do not fully understand. Just my opinion, of course. . |
Quote:
Myself, I almost always use pdo because it makes the code more portable in that pdo will work with many different databases while mysqli is mysql specific. . |
Sarettah's approach is better but for learning purposes, check the red notes. Tested.
Code:
<?php |
Quote:
|
Quote:
|
Quote:
I appreciate the notes, always good to learn from mistakes and understanding why it didn't work is a huge plus :) |
Quote:
You are close to having a working solution using mysqli. So, if I were you, I would get what I have working and make sure I understand what is going on and why I used each and every comment and function that I used. Then If I want to play with pdo, make a copy and change it out to work with pdo. Then I would probably make another copy and turn the whole thing into an class and do an object oriented cut of it. Each time it will increase your knowledge. Earlier I forgot to answer you about echo versus print. They are bascially the same in common usage. There are differences. Print returns a value of 1 while echo does not and echo can take multiple parameters. As I said in the earlier thread php.net is your friend. https://www.php.net/manual/en/function.echo.php https://www.php.net/manual/en/function.print.php . |
Quote:
. |
Quote:
|
Quote:
|
sarettah . . . too useful (and scarcely spiteful) for this board.
|
Quote:
# |
One last bump for this project as I have a related question...
What is the function called to be able to add certain data entries as I guess 'tags' for want of a better word without knowing the correct term? What I'm trying to do is to include some of the SQL data to display in the HTML meta tags, so for example, it would pull the data from the row for the 'RecipeName' column and in the meta tags I'd like to put a snippet of code like below: PHP Code:
<meta name="Keywords" content="Simple recipe for Cake" /> <meta name="Description" content="Cake recipe ingredients" /> Just trying to give this project a little SEO advantage, what is the function called that I need to use in order to do something like that in the template pages, that pulls directly from the SQL database for the RecipeName based on the same RecipeID in the URL? TIA :thumbsup |
Quote:
# |
I love these coding threads!
I can't understand a single fucking word. LOL |
Sorry, forgot to post the code im using to attach the data from the database..
PHP Code:
PHP Code:
|
Quote:
|
Quote:
<head> <base href="https://gfy.com/" /><!--[if IE]></base><![endif]--> <meta name="description" content="<?php echo $description; ?>"> </head> |
Skip the base ref part... Somehow that got added.:helpme
|
Quote:
. |
Quote:
By the way, to OP: I see you use include in the original code... You would use include if you want to include a file. <?php include("path/to/file.php"); ?> |
Glad you're feeling better.
|
Quote:
|
Quote:
Else the output will be empty. Also I think you're missing the Recipes table in your query... "SELECT RecipeName, RecipeDescription, Author FROM Recipes" |
I am looking through today's discussion and at this point I am pretty confused.
What tables are you using and what fields are in the tables? I am seeing you use what I think are table names as output? So, not sure what to tell you. As zijlstravideo said, you need to be doing the query at the top of the page if you expect to output it in the head section. Code:
<html> You are mixing methodologies it appears also. . |
This is what the top of the HTML looks like presently on my test page:
PHP Code:
The page is called by URL like this: PHP Code:
Hope that makes sense than my rambling posts earlier :1orglaugh |
I know what you are trying to do. I am thouroghly confused by how you are trying to do it. Your code does not make sense to me.
Quote:
Your query makes no sense. SELECT * FROM $table WHERE RecipeName, RecipeDescription, Author You say: Quote:
What fields are in your table? The Recipes table we were working with before had the fields 'recipeid', 'title', 'method' and 'ingredients' in it. It now has RecipeName, RecipeDescription and Author also? If so then you need to pull those fields like you pulled the other fields from the table. You did that by calling for a result by id. In this case you have not grabbed your input variable so you don't know what id the recipe you are pulling has. What does the entire .php file look like right now? . |
Quote:
Quote:
Quote:
|
Quote:
|
All times are GMT -7. The time now is 07:33 AM. |
Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc123