GoFuckYourself.com - Adult Webmaster Forum

GoFuckYourself.com - Adult Webmaster Forum (https://gfy.com/index.php)
-   Fucking Around & Business Discussion (https://gfy.com/forumdisplay.php?f=26)
-   -   Tech What a wild 3 weeks (and php question). (https://gfy.com/showthread.php?t=1347823)

Publisher Bucks 08-28-2021 10:58 AM

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:

<?php
$con
=mysqli_connect("localhost","recipes","password","recipelist");
// Check connection
if (mysqli_connect_errno())
{
echo 
"Failed to connect to MySQL: " mysqli_connect_error();
}

$result mysqli_query($con,"SELECT * FROM Recipes");

echo 
"<table border='1'>
<tr>
<th>ID</th>
<th>Title</th>
<th>Method</th>
<th>Ingredients</th>
</tr>"
;

while(
$row mysqli_fetch_array($result))
{
    
$link "recipes.php?id=".$row['RecipeID'];
    echo 
"<tr>";
    echo 
"<td><a href = "$link ">" $row['Title'] . "</a></td>";
    echo 
"<td>" $row['Ingredients'] . "</td>";
    echo 
"<td>" $row['Method'] . "</td>";
    echo 
"</tr>";
}
echo 
"</table>";

mysqli_close($con);
?>

Which does everything that it is supposed to for that page (for now).

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:

<?php
    
// check incoming params exist
    
if ( ! isset($_GET['id'] ) {
        
// 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

    
$table "Recipes";

    
$sql "SELECT * FROM $table WHERE id = :RecipeID";

    try {
        
$stmt $pdo->prepare($sql);

        
$stmt->bindParam(':id'$_GET['id'], PDO::PARAM_INT);
        
$stmt->execute();

        
$rows $stmt->FetchAll();  // $rows now contains all the results of the query
    
}
    catch( 
PDOException $e) {
        echo 
$e-getMessage();
    }

    foreach ( 
$rows as $row ) {
        
// do things with the $row['column_name'] data
    
}
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>";
?>

Its basically not showing anything.

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

k33n 08-28-2021 09:40 PM

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".

Colmike9 08-28-2021 09:52 PM

// 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 )

hornyasf 08-28-2021 11:34 PM

Another tiny error

" echo $e-getMessage(); "

Missing a >

Publisher Bucks 08-29-2021 07:44 AM

Quote:

Originally Posted by Colmike9 (Post 22906597)
// 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 )

Thank you!

Publisher Bucks 08-29-2021 07:44 AM

Quote:

Originally Posted by hornyasf (Post 22906615)
Another tiny error

" echo $e-getMessage(); "

Missing a >

Much appreciated :)

Publisher Bucks 08-29-2021 07:45 AM

Quote:

Originally Posted by k33n (Post 22906595)
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".

Could you show me where im messing up on that?

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 :)

plsureking 08-29-2021 08:22 AM

hey if you still live in New Orleans stay safe and good luck! it doesn't look pretty.

:Oh crap

#

Publisher Bucks 08-29-2021 09:30 AM

Quote:

Originally Posted by plsureking (Post 22906681)
hey if you still live in New Orleans stay safe and good luck! it doesn't look pretty.

:Oh crap

#

Thanks.

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.

sarettah 08-29-2021 11:01 AM

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...";
}

.

Publisher Bucks 08-29-2021 11:36 AM

Quote:

Originally Posted by sarettah (Post 22906738)
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...";
}

.

Damn! Thank you so much for the detailed response :thumbsup

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

sarettah 08-29-2021 11:41 AM

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:

// 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
It then shows you the code you might choose to use if you had a pdo connection established. It was not intended to be just run, it was intended to be modified before being run.

A programmer should never use code that they do not fully understand. Just my opinion, of course.

.

sarettah 08-29-2021 11:43 AM

Quote:

Originally Posted by Publisher Bucks (Post 22906749)
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?

Nope. I just went with the mysqli because you already had a working connection in the first program.

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.

.

k33n 08-29-2021 11:46 AM

Sarettah's approach is better but for learning purposes, check the red notes. Tested.

Code:

<?php
    // 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=yourdbname", "dbusername","dbpass");
    $table = "Recipes";
  //3. Your query was looking for a column called id, but your column is called RecipeID
  //use this instead
      $sql = "SELECT * FROM $table WHERE RecipeID = :id";
    try {
        $stmt = $pdo->prepare($sql);

        $stmt->bindParam(':id', $_GET['id'], PDO::PARAM_INT);
        $stmt->execute();

        $rows = $stmt->FetchAll();  // $rows now contains all the results of the query
    }
    catch( PDOException $e) {
        echo $e-getMessage();
    }
    //Added the table as i've seen a </table> below
    echo "<table border='1'>
                        <tr>
                        <th>ID</th>
                        <th>Title</th>
                        <th>Method</th>
                        <th>Ingredients</th>
                        </tr>";
    foreach ( $rows as $row ) {
        // do things with the $row['column_name'] data
      echo "<tr>";
      echo "<td>" . $row['Title'] . "</td>";
      echo "<td>" . $row['Ingredients'] . "</td>";
      echo "<td>" . $row['Method'] . "</td>";
      echo "</tr>";
    }
  // 4. You are using PDO, mysqli_fetch_array($result) will throw error:  mysqli_fetch_array() expects parameter 1 to be mysqli_result.And $result is undefined. 

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>";
?>


Publisher Bucks 08-29-2021 11:47 AM

Quote:

Originally Posted by sarettah (Post 22906752)
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
.

I tried running a search on the board for it but couldn't find it, I remembered posting about this issue a few weeks back before getting the rona, just didn't see it in the results.

Publisher Bucks 08-29-2021 11:59 AM

Quote:

Originally Posted by sarettah (Post 22906753)
Nope. I just went with the mysqli because you already had a working connection in the first program.

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.

.

So that being said, would it be prudent for me to start utilizing pdo instead of mysqli if there range of databases is larger, will pdo allow me any benefit other than a wider range of database usage overall?

Publisher Bucks 08-29-2021 12:00 PM

Quote:

Originally Posted by k33n (Post 22906757)
Sarettah's approach is better but for learning purposes, check the red notes. Tested.

Code:

<?php
    // 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=yourdbname", "dbusername","dbpass");
    $table = "Recipes";
  //3. Your query was looking for a column called id, but your column is called RecipeID
  //use this instead
      $sql = "SELECT * FROM $table WHERE RecipeID = :id";
    try {
        $stmt = $pdo->prepare($sql);

        $stmt->bindParam(':id', $_GET['id'], PDO::PARAM_INT);
        $stmt->execute();

        $rows = $stmt->FetchAll();  // $rows now contains all the results of the query
    }
    catch( PDOException $e) {
        echo $e-getMessage();
    }
    //Added the table as i've seen a </table> below
    echo "<table border='1'>
                        <tr>
                        <th>ID</th>
                        <th>Title</th>
                        <th>Method</th>
                        <th>Ingredients</th>
                        </tr>";
    foreach ( $rows as $row ) {
        // do things with the $row['column_name'] data
      echo "<tr>";
      echo "<td>" . $row['Title'] . "</td>";
      echo "<td>" . $row['Ingredients'] . "</td>";
      echo "<td>" . $row['Method'] . "</td>";
      echo "</tr>";
    }
  // 4. You are using PDO, mysqli_fetch_array($result) will throw error:  mysqli_fetch_array() expects parameter 1 to be mysqli_result.And $result is undefined. 

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>";
?>


Thank you!

I appreciate the notes, always good to learn from mistakes and understanding why it didn't work is a huge plus :)

sarettah 08-29-2021 12:07 PM

Quote:

Originally Posted by Publisher Bucks (Post 22906766)
So that being said, would it be prudent for me to start utilizing pdo instead of mysqli if there range of databases is larger, will pdo allow me any benefit other than a wider range of database usage overall?

Concentrate on one thing at a time.

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

.

sarettah 08-29-2021 12:09 PM

Quote:

Originally Posted by Publisher Bucks (Post 22906758)
I tried running a search on the board for it but couldn't find it, I remembered posting about this issue a few weeks back before getting the rona, just didn't see it in the results.

For future reference, since you started the thread just do a search for all threads started by you and you can find it pretty easily.

.

Publisher Bucks 08-29-2021 12:15 PM

Quote:

Originally Posted by sarettah (Post 22906771)
For future reference, since you started the thread just do a search for all threads started by you and you can find it pretty easily.

.

Thanks, I would have just added to that thread if I'd found it LOL

Publisher Bucks 08-29-2021 12:16 PM

Quote:

Originally Posted by sarettah (Post 22906770)
Concentrate on one thing at a time.

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.


.

Good points, thank you again for all your help and advice on this project of mine, I really appreciate it :thumbsup

CaptainHowdy 08-29-2021 12:17 PM

sarettah . . . too useful (and scarcely spiteful) for this board.

plsureking 08-29-2021 03:54 PM

Quote:

Originally Posted by CaptainHowdy (Post 22906776)
sarettah . . . too useful (and scarcely spiteful) for this board.

:1orglaugh:1orglaugh

#

Publisher Bucks 09-06-2021 07:29 AM

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 <?php include "RecipeName"?>" />
<meta name="Description" content="<?php include "RecipeName"?> recipe ingredients" />

Which would display...

<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

plsureking 09-06-2021 07:34 AM

Quote:

Originally Posted by Publisher Bucks (Post 22910187)
PHP Code:

Simple recipe for (phocode




#

The Porn Nerd 09-06-2021 07:54 AM

I love these coding threads!
I can't understand a single fucking word. LOL

Publisher Bucks 09-06-2021 08:00 AM

Sorry, forgot to post the code im using to attach the data from the database..

PHP Code:

<?php
session_start
();
(
"mysql:host=localhost;dbname=database""user","password");
    
$table "Recipes";

$metasettings = @mysql_fetch_assoc(@mysql_query("SELECT * FROM RecipeName, RecipeDescription, Author"));

$title $metasettings['RecipeName'];
$description $metasettings['RecipeDescription'];
$random_tags $metasettings['Author'];

?>

Then im using this in place on each of the metas..

PHP Code:

<?=$title?>
<?=$description?>
<?=$random_tags?>

Hope that makes sense :Oh crap

Publisher Bucks 09-06-2021 08:01 AM

Quote:

Originally Posted by The Porn Nerd (Post 22910196)
I love these coding threads!
I can't understand a single fucking word. LOL

I've only been coding a month or two so can barely understand it myself, hence this thread, I'm trying to learn though :thumbsup

zijlstravideo 09-06-2021 08:12 AM

Quote:

Originally Posted by Publisher Bucks (Post 22910201)
Sorry, forgot to post the code im using to attach the data from the database..

PHP Code:

<?php
session_start
();
(
"mysql:host=localhost;dbname=database""user","password");
    
$table "Recipes";

$metasettings = @mysql_fetch_assoc(@mysql_query("SELECT * FROM RecipeName, RecipeDescription, Author"));

$title $metasettings['RecipeName'];
$description $metasettings['RecipeDescription'];
$random_tags $metasettings['Author'];

?>

Then im using this in place on each of the metas..

PHP Code:

<?=$title?>
<?=$description?>
<?=$random_tags?>

Hope that makes sense :Oh crap

You want to print it in your head, right?

<head>
<base href="https://gfy.com/" /><!--[if IE]></base><![endif]-->
<meta name="description" content="<?php echo $description; ?>">
</head>

zijlstravideo 09-06-2021 08:13 AM

Skip the base ref part... Somehow that got added.:helpme

sarettah 09-06-2021 08:38 AM

Quote:

Originally Posted by zijlstravideo (Post 22910209)
Skip the base ref part... Somehow that got added.:helpme

Yeah the board does that.

.

zijlstravideo 09-06-2021 09:13 AM

Quote:

Originally Posted by sarettah (Post 22910219)
Yeah the board does that.

.

It's alive!:stoned

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"); ?>

J. Falcon 09-06-2021 10:27 AM

Glad you're feeling better.

Publisher Bucks 09-06-2021 01:57 PM

Quote:

Originally Posted by zijlstravideo (Post 22910208)
You want to print it in your head, right?

<head>
<base href="https://gfy.com/" /><!--[if IE]></base><![endif]-->
<base href="https://gfy.com/" /><!--[if IE]></base><![endif]-->
<meta name="description" content="<?php echo $description; ?>">
</head>

That's what I've been doing but its not showing up, I think I'm messing up with the get/post for the variable in the connection part of my script.

zijlstravideo 09-06-2021 01:59 PM

Quote:

Originally Posted by Publisher Bucks (Post 22910321)
That's what I've been doing but its not showing up, I think I'm messing up with the get/post for the variable in the connection part of my script.

You have the php sql query code at the top?
Else the output will be empty.

Also I think you're missing the Recipes table in your query...
"SELECT RecipeName, RecipeDescription, Author FROM Recipes"

sarettah 09-06-2021 02:57 PM

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>
<?php
Hook up to the database.
Get your input variables.
Do your query for the meta data
?>
<head>
<base href="https://gfy.com/" /><!--[if IE]></base><![endif]-->
<title><?php echo your title here; ?php></title>
<meta name="keywords" value="<?php echo your keywords here; ?>">
<meta name="desctiption" value="<?php echo your description here; ?>">
</head>
<body>
<?php
do your recipe query here
output your recipe table here
?>
</body>
</html>

Is kind of what the structure of the page should be, I think.

You are mixing methodologies it appears also.

.

Publisher Bucks 09-06-2021 03:24 PM

This is what the top of the HTML looks like presently on my test page:

PHP Code:

<?php
session_start
();
(
"mysql:host=localhost;dbname=database""user","password");
    
$table "Recipes";

$metasettings = @mysql_fetch_assoc(@mysql_query("SELECT * FROM $table WHERE RecipeName, RecipeDescription, Author"));

$title $metasettings['RecipeName'];
$description $metasettings['RecipeDescription'];
$random_tags $metasettings['Author'];

?>
<!DOCTYPE html>
<head>
<base href="https://gfy.com/" /><!--[if IE]></base><![endif]-->


    <meta charset="UTF-8"/>

    <title><?php echo $title?> Recipe</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"/>

What I'm trying to do in the SQL data is to add the 'RecipeName', 'RecipeDescription' and 'Author' rows as meta tags.

The page is called by URL like this:

PHP Code:

recipes.php?id=34 

The recipes.php works perfectly, I just cant get this row data showing in the metas where I want them to, once I get this part working I can secure the id= stuff and set the site live, hopefully :Oh crap

Hope that makes sense than my rambling posts earlier :1orglaugh

sarettah 09-06-2021 05:06 PM

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:

$metasettings = @mysql_fetch_assoc(@mysql_query("SELECT * FROM $table WHERE RecipeName, RecipeDescription, Author"));

$title = $metasettings['RecipeName'];
$description = $metasettings['RecipeDescription'];
$random_tags = $metasettings['Author'];

This looks all sorts of wrong to me.

Your query makes no sense.

SELECT * FROM $table WHERE RecipeName, RecipeDescription, Author

You say:
Quote:

What I'm trying to do in the SQL data is to add the 'RecipeName', 'RecipeDescription' and 'Author' rows as meta tags.
You select columns (field names) from a table then you read through the rows returned. Rows do not have names, unless this is some new fangled sql that I have never seen before.

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?

.

Publisher Bucks 09-06-2021 05:29 PM

Quote:

Originally Posted by sarettah (Post 22910418)
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?

Yes, they got added a few days back, so it contains the original ones plus those additional columns.
Quote:

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.
That just gets put at the top of the page still correct?

Quote:

What does the entire .php file look like right now?
As posted above or, do you mean the full code incl html for the recipes.php page?

The Porn Nerd 09-06-2021 05:50 PM

Quote:

Originally Posted by Publisher Bucks (Post 22910203)
I've only been coding a month or two so can barely understand it myself, hence this thread, I'm trying to learn though :thumbsup

GFY is a great place to learn! Lots of excellent coders on here. :thumbsup

sarettah 09-06-2021 05:54 PM

Quote:

Originally Posted by Publisher Bucks (Post 22910423)
Yes, they got added a few days back, so it contains the original ones plus those additional columns.


That just gets put at the top of the page still correct?



As posted above or, do you mean the full code incl html for the recipes.php page?


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.

.

plsureking 09-06-2021 05:59 PM

Quote:

Originally Posted by The Porn Nerd (Post 22910429)
GFY is a great place to learn! Lots of excellent coders on here. :thumbsup

better coders than corporate :drinkup

#

sarettah 09-06-2021 06:07 PM

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.

.

sarettah 09-06-2021 06:11 PM

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.

.

Publisher Bucks 09-06-2021 06:15 PM

Quote:

Originally Posted by sarettah (Post 22910432)
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.

.

Thank you (again!).

PHP Code:

<?php
session_start
();
(
"mysql:host=localhost;dbname=database""user","password");
    
$table "Recipes";

$metasettings = @mysql_fetch_assoc(@mysql_query("SELECT * FROM $table WHERE RecipeName, RecipeDescription, Author"));

$title $metasettings['RecipeName'];
$description $metasettings['RecipeDescription'];
$random_tags $metasettings['Author'];

?>
<!DOCTYPE html>
<head>
<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"/>

    {-- removed all the random css testing crap --}

        </head>

<body class="home page page-id-21 page-template-default ie et_includes_sidebar">

        <?php
//in middle:
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> <!-- end .container -->

    <?php
//in middle:
require('./templates/secondary.php'); 
 
?>

[b]included in secondary.php is a search function too, do you need to see the code for that?[/b]

    <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">&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'); 
 
?>

<?php
// 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";
      
$sql "SELECT * FROM $table WHERE ID = :id";
    try {
        
$stmt $pdo->prepare($sql);

        
$stmt->bindParam(':id'$_GET['id'], PDO::PARAM_INT);
        
$stmt->execute();

        
$rows $stmt->FetchAll();  // $rows now contains all the results of the query
    
}
    catch( 
PDOException $e) {
        echo 
$e-getMessage();
    }



    foreach ( 
$rows as $row ) {
        
// do things with the $row['column_name'] data
       
echo "<tr>";
       echo 
"<p><h2>" $row['Title'] . " Recipe.</h2></p>";

       echo 
"<p><b>Ingredients</b></p>";
       echo 
"<p> " $row['Ingredients'] . "</p>";
       echo 
"<p><b>Method</b></p>";
       echo 
"<p>" $row['Method'] . "</p>";
       echo 
"</tr>";
    }
  
// 4. You are using PDO, mysqli_fetch_array($result) will throw error:  mysqli_fetch_array() expects parameter 1 to be mysqli_result.And $result is undefined.   

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>";
?>

                                </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>

Like I say, the recipes.php page works for everything else, its just not letting me throw the meta tag data in that I'd like to use.

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.

sarettah 09-06-2021 06:29 PM

<?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">&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.

.

sarettah 09-06-2021 06:40 PM

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.


.

sarettah 09-06-2021 06:53 PM

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.

.

sarettah 09-06-2021 07:19 PM

Sorry to carry on but.

You said:
Quote:

Like I say, the recipes.php page works for everything else, its just not letting me throw the meta tag data in that I'd like to use.
Really? it shouldn't have. You had pdo stuff and mysqli stuff mixed together in there, should have been throwing errors out the wazoo on you.

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.

.

ruff 09-06-2021 07:41 PM

Quote:

Originally Posted by The Porn Nerd (Post 22910196)
I love these coding threads!
I can't understand a single fucking word. LOL

I read everything here. Didn't understand shit, but I admire smart people.:thumbsup


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