![]() |
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:
|
Fiddy PHP Errors....
Just realized, you pull author into a field called random_tags but you never do use it any where. . |
Quote:
the command is error_reporting(E_ALL); damn. . |
Quote:
Playing with the code you posted now, shall report back in a little while, thank you again for your help with this php stuff, I really appreciate your time and assistance :thumbsup |
A cleaner version. More my style. No camelcase shit ;p
<?php error_reporting(E_ALL); $id=0; if ( ! isset($_GET['id'] ) ) { header('Location: index.php'); } else { $id=intval($_GET['id']); } if($id==0) { die('bad id passed in'); } $pdo=hookitup(); $table = "recipes"; $sql = "select * from " . $table . " where id=?"; try { $stmt = $db->prepare($sql_str); $stmt->execute(array($pdo->quote($id))); } catch( PDOException $e) { die($e-getMessage()); } $row=$stmt->fetch(PDO::FETCH_ASSOC); $title = $row['recipename']; $description = $row['recipedescription']; ?> <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 http-equiv="Content-Type" content="text/html; 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"/> <link rel="canonical" href="//siteurl/"> </head> <body class="home page page-id-21 page-template-default ie et_includes_sidebar"> <?php require_once('./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> <?php require_once('./templates/secondary.php'); ?> <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> <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> <?php require_once('./templates/midbanner.php'); echo "<table>"; echo "<tr>"; echo "<td>" . $row['title'] . "</td>"; echo "<td>" . $row['ingredients'] . "</td>"; echo "<td>" . $row['method'] . "</td>"; echo "</tr>"; echo "</table>"; ?> </div> </article> </div> <?php require_once('./templates/sidebar.php'); ?> </div> </div> <!-- end .container --> </div> <!-- end #main-area --></center> <?php require_once('./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> <?php function hookitup() { require_once('dbcreds.php'); return new PDO("mysql:host=$host;dbname=$dbname;charset=utf8" ,$user,$password,array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)); } In dbcreds.php would be: <?php $dbname='dbname'; $dbuser='dbusername'; $dbpass='dbpassword'; $dbhost='localhost'; ?> . |
A small addition to the code above:
$title = ucwords($row['recipename']); //Personal taste, but I like titles like that. The above makes each first character of a word uppercase: This Is A Great Title $description = ucfirst($row['recipedescription']); //Makes the first character of your string uppercase (in case it isn't stored that way in the database. It looks pretty bad in the search results if your description's first letter isn't uppercase. |
Apologies for the late response, we lost power again last night while they were working on the lines to try and get the rest of the neighborhood up and running.
I played with the code you posted and seem to be getting a weird error where the data from the SQL isn't showing, its just giving me a blank page and there are no meta tags showing up either. (Didn't change anything until after the blank page showed). Putting back the original code, the page works how it is supposed to, any thoughts? :Oh crap |
Code:
$sql = "select * from " . $table . " where id=?"; |
Quote:
I gave that a try and its still showing a blank page, this is the error from the logs: Quote:
|
Quote:
{ require_once('dbcreds.php'); return new PDO("mysql:host=$host;dbname=$dbname;charset=utf8" ,$user,$password,array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)); } dbcreds file: <?php $dbname='dbname'; $dbuser='dbusername'; $dbpass='dbpassword'; $dbhost='localhost'; ?> These two don't match, that's why you're getting a blank page (as no database connection is being made). Change the function to: function hookitup() { require_once('dbcreds.php'); return new PDO("mysql:host=$dbhost;dbname=$dbname;charset=utf8" ,$user,$password,array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)); } And as K33n noted, change: $stmt = $db->prepare($sql_str); to: $stmt = $db->prepare($sql); |
To OP: You can learn a lot from looking more closely to your error log. This one was easy to spot.
Your error: #0 /blah/blah/domain.com/recipesnew.php(119): PDO->__construct('mysql:host=;dbn...', NULL, NULL, Array) See how host= is empty... |
Quote:
Its still not showing anything BUT, does that mean I also need to make the following changes based on this error? Quote:
} Or am I looking in the wrong place/overthinking and doing the wrong thing? |
Yup, good catch. Those as well.
|
Sorry about that. I didn't do a full test and that kind of shit happens when you meld 2 different sets of code together.
In this case, I grabbed my dbcreds file from my local machine and my hookup routine from a different place. But that is what debugging is for, ya know? I really didn't think you were just going to switch out to the new code I posted, I was just trying to get to a code set that made sense to me. . |
Quote:
Quote:
|
Quote:
From the error you posted it appears that you do not have anything populated into your $dbuser and $dbpass variables. But post line 118 of the program, please. . |
Quote:
I'm really appreciative of everyone's help, assistance and knowledge in this thread as I'm sure you all have other things you could be doing instead of sharing your expertise. |
Quote:
Quote:
|
Quote:
By the way, I think you can even shorten this: $id=0; if ( ! isset($_GET['id'] ) ) { header('Location: index.php'); } else { $id=intval($_GET['id']); } if($id==0) { die('bad id passed in'); } Into: $id=intval($_GET['id']); if($id==0) { header( "Location: index.php" ); //or any other page like 404 exit; } As intval will also output 0 if $id is empty. |
Quote:
indicates that you are not passing in a username or password. The places that say NULL, NULL right before the Array indicate that $dbuser and $dbpass are coming in as NULL meaning there ain't nothing there. Your error: /blah/blah/domain.com/recipesnew.php(118): PDO->__construct('mysql:host=loca...', NULL, NULL, Array) PDO("mysql:host=$dbhost;dbname=$dbname" ,$dbuser,$dbpass,array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)); } So, if you are using the dbcreds.php file then the problem is in there. If you are not using that and have the user and password hardcoded then that would be where the issue is. The charset=utf8 just tells it to accept utf8 characters. The routine I pulled it from has to handle various different languages. I pretty much have everything I do coded for utf8 (unicode). . |
Quote:
I tend to, when trying to do code 101 stuff use old old school. First step intialize variable to default value then use it. But yeah you are initializing it to whatever comes into the get if it is a valid int or zero if not so it does the same thing in a couple of less words. . |
Quote:
Quote:
This is the dbcreds.php file that is being used: Quote:
Quote:
Quote:
|
The code you are posting looks right The bottom part you posted at the end where you moved the quote marks is wrong.
The part of the error you are showing us indicated that there is nothing in the username and password variables which would mean the problem is in the dbcreds.php like I said earlier. I have sent you a pm. . |
I sent this via pm also but
in the dbcreds.php you are missing a semicolon after localhost $dbhost='localhost' Should be $dbhost='localhost'; . |
In the code you sent me:
return new PDO("mysql:host=$dbhost;dbname=$dbname;charset=utf 8" ,dbuser=$dbuser;dbpass=$dbpass,array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)); should be: return new PDO("mysql:host=$dbhost;dbname=$dbname;charset=utf 8",$dbuser,$dbpass,array(PDO::ATTR_EMULATE_PREPARES => false, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION)); You added an equals sign in the dbuser and dbpass that should not be there. There is still an issue in the code. I am looking. . |
On line 15-17 you have a try statement that is not completed:
try { $sql = "select * from " . $table . " where id=?"; Get rid of that. . |
A mistake in the code I posted.
around line 16: try { $stmt = $db->prepare($sql); } catch( PDOException $e) { die($e-getMessage()); } should be: try { $stmt = $pdo->prepare($sql); } catch( PDOException $e) { die($e-getMessage()); } . |
Damn I suck today :(
Another issue in what I posted: try { $stmt = $pdo->prepare($sql); } catch( PDOException $e) { die($e-getMessage()); } Should be: try { $stmt = $pdo->prepare($sql); } catch( PDOException $e) { die($e->getMessage()); } . |
Also, (this one is not my mistake) you removed the execute statement from that same code block.
So: try { $stmt = $pdo->prepare($sql); } catch( PDOException $e) { die($e->getMessage()); } Should be: try { $stmt = $pdo->prepare($sql); $stmt->execute(array($pdo->quote($id))); } catch( PDOException $e) { die($e->getMessage()); } |
Well, that was fun.
To whoever was watching, the OP and I took it private so I could see the actual code without revealing any proprietary info. It looks like he is in pretty good shape now. It was mainly syntax issues. . |
sarettah,
I just want to say thank you for all of your help, not only in this thread over the past week but also by PM, you truly do not know how appreciative I am of your time helping me to get this pet project working, I'm sure there are a hundred other things you could have been doing this afternoon instead of spending time to get this working for me. I'm going to play around with a couple of other formatting issues that I'm having but other than that, I am really happy with how this site has turned out, even though its just a pet project of mine and not really going to be a money maker its good to start seeing it come to fruition :) Thank you so much :thumbsup |
All times are GMT -7. The time now is 02:07 PM. |
Powered by vBulletin® Version 3.8.8
Copyright ©2000 - 2025, vBulletin Solutions, Inc.
©2000-, AI Media Network Inc123