![]() |
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:
|
Quote:
I know where the issue is at this point. Post the entire recipes.php page so I can kind of walk you through the mess. please. . |
Quote:
# |
Ok, you need to think through what you are doing some.
You are using different methodologies within the same page I think. That makes no sense. Before this, in prior discussions you hooked up to the database and you got your input variables. Then you did your query using the id that was passed in as your key. You only need to do that once and you need to do it at the very beginning of the program. Then you have all the data you need to do everything else. It looks to me (from what you posted) that you are trying to do 2 queries. The one you posted today that is totally discombobulated and the one you were doing during the hurricane which we had figured out. You only need one query. But it has to occur before you output anything. So your flow becomes: Get your input variable. Hook up to the database. (these 2 items can be done in either order) Do your query grab your results present your data. Once you put up the code I will try to show you what I mean. . |
By the way, anybody looking at this, it still helps to draw out a logic flow. You shouldn't be coding anything without some sort of flow chart drawn out until you have a whole lot of experience under your belt.
Even then, once you have experience it helps to draw it out., Some of us have been around long enough to do flow charts and entity diagrams in our head. beginners should not be doing that. I wrote my first computer code 49 years ago. I still draw out the flow. I even use symbols when I want to explain it to others. . |
Quote:
PHP Code:
The TABLE 'Recipes' contains the following columns... RecipeID Title Description (not used but has data in it) Ingredients Method Nutritional (not used but has data in it) RecipeName (for metas) RecipeDescription (for metas) Author (for metas) The last 3 are the only thing that was added 'new' to the table since last week. Although thinking about it now, I could probably use 'Title' instead of the 'RecipeName' data as its pretty much the same thing. |
<?php
//Get rid of the shit you posted today, most of what you needed you already had. It was just in the wrong place // k33n and sarettah help with code fixes and examples from gfy // check incoming params exist if ( ! isset($_GET['id'] ) ) { //1.you were missing a ) // missing param, go to an error page for example header('Location: index.php'); exit; } // You can now use $_GET['id'] which will be the id number passed // any way you want. // For example using a PDO connection called $pdo // 2. Do the PDO connection $pdo = new PDO("mysql:host=localhost;dbname=database", "dbuser","dbpass"); $table = "Recipes"; // Select * pulls all the fields from the table $sql = "SELECT * FROM $table WHERE ID = :id"; try { $stmt = $pdo->prepare($sql); $stmt->bindParam(':id', $_GET['id'], PDO::PARAM_INT); $stmt->execute(); } catch( PDOException $e) { echo $e-getMessage(); //<-- You should do a die() here to kill the program } //I moved this out of the if, just better style imho $row=$stmt->fetch(PDO::FETCH_ASSOC); //Your query will only return one row, the row with id as key, I changed from fetchall to fetch assoc which will give us that one record $title = $row['RecipeName']; $description = $row['RecipeDescription']; $random_tags = $row['Author']; ?> <!DOCTYPE html> <head> <base href="https://gfy.com/" /><!--[if IE]></base><![endif]--> <base href="https://gfy.com/" /><!--[if IE]></base><![endif]--> <base href="https://gfy.com/" /><!--[if IE]></base><![endif]--> <meta charset="UTF-8"/> <title><?php echo $title; ?> Recipe | Sitename</title> <meta name="Description" content="<?php echo $description; ?>"> <meta name="Keywords" content="<?php echo $title; ?> Recipe Ingredients Make <?php echo $title; ?> Recipes"> <link rel="stylesheet" href="style.css" type="text/css"/> </head> <body class="home page page-id-21 page-template-default ie et_includes_sidebar"> <?php require('./templates/header.php'); ?> <div id="hd_logo0"><p><a href="http://www.domain.com"> <img class="alignnone size-full wp-image-169" alt="" src="logo.png" style="width: 310px"/></a></p> </div> </div> //<-----------------------------------------------What is this closing. Where is container opened? <!-- end .container --> <?php //in middle: require('./templates/secondary.php'); ?> included in secondary.php is a search function too, do you need to see the code for that? //<---NO <center><div id="main-area"> <div class="container" style="left: 0px; top: 0px"> <div id="content-area" class="clearfix"> <div id="left-area"> <div id="breadcrumbs" class="clearfix"> <a href="http://www.domain.com" class="breadcrumbs_home">Home</a> <span class="raquo">»</span> </div> <!-- end #breadcrumbs --> <article id="post-21" class="post-21 page type-page status-publish hentry entry clearfix"> <div class="post_content clearfix"> <h1 class="title">Home</h1> <h1 style="text-align: center;"></h1> <?php //in middle: require('./templates/midbanner.php'); // I killed some code here, looked like you were doing double duty. //You will only get one row back so you do not need to loop through, my bad for not correcting you on that the other day echo "<table>"; echo "<tr>"; echo "<td>" . $row['Title'] . "</td>"; echo "<td>" . $row['Ingredients'] . "</td>"; echo "<td>" . $row['Method'] . "</td>"; echo "</tr>"; echo "</table>"; ?> </div> <!-- end .post_content --> </article> <!-- end .entry --> </div> <!-- end #left-area --> <?php //in middle: require('./templates/sidebar.php'); ?> </div> <!-- end #content-area --> </div> <!-- end .container --> </div> <!-- end #main-area --></center> <?php //in middle: require('./templates/footer.php'); ?> <script type="text/javascript" src="jquery.form.min.js"></script> <script type="text/javascript" src="superfish.js"></script> <script type="text/javascript" src="jquery.fitvids.js"></script> <script type="text/javascript" src="custom.js"></script> </body> </html> --------------------------------------------------------------------------- I got rid of the stuff you posted today and I moved all the code we did a few days ago to the top of the file. In the new code you did today, you were doing another connection and another query using a new methodology. WHY????? You already had a pdo connection that works, move it to the top of the page and use it. Rarely will you need more than one database connection in any one particular program. All the data you need is in the same table in 1 record, you only need one query to pull all the data you need. . |
Now, do you understand all that?
If not, ask questions. Play with the code. Figure out what each and every statement does and why it is there. Some things have to be done in a certain order, some things don't. You have to have the input variable for the id before you do the query because the query depends on the id. You have to have a database connection before you can do the query. You have to do the query before you can output anything from the database. Also, not to be rude or mean but where the fuck did you get this from? "SELECT * FROM RecipeName, RecipeDescription, Author"; Which you later changed to this: "SELECT * FROM $table WHERE RecipeName, RecipeDescription, Author"; That was what was really throwing me for a loop. I was thinking the original "SELECT * FROM RecipeName, RecipeDescription, Author" Might be attempting to do a 3 table join of some sort. . |
Btw, I did a last minute change just now. Changed the fetch all to fetch assoc.
The other changes I made would have been wrong with the fetchall in there, I missed it the first time through. I also removed your session_start(). I saw no evidence in any of the code you posted that you were using sessions. If you are then you will want to put that back in. Also, please stop using the [ PHP ] tag around the code. Makes it really difficult to read. . |
Sorry to carry on but.
You said: Quote:
It doesn't look like you have error reporting turned on. If you can't see the errors you cannot fix them. PHP is a very forgiving language a lot of times, that is detrimental for a beginning coder. Puth the command: error_reporting[E_ALL]; at the very top of the program, once you have all the errors debugged then change it to error_reorting[0]; anytime you neeed to debug change it back to E_ALL . E_ALL tells it to show you all errors no matter how small. A beginner should NOT be ignoring any errors. Just all imho of course. . |
Quote:
|
All times are GMT -7. The time now is 06:30 PM. |
Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc123