Jump to content

I am getting an error while creating the link.


maviyazilim
 Share

Recommended Posts

I want to create a link to the content listed on the homepage and show the content on another page.

I am getting an error like this.

I am trying to post to file named icerik.php with post method.


Parse error: syntax error, unexpected string content "", expecting "-" or identifier or variable or number in on line 39

39 - echo '<a href='icerik.php?=icerik' . $goster['icerik_id'] . ''>' . $goster['baslik'] . '</a>';

$giden = $_POST['icerik_id'];

while($goster=mysqli_fetch_assoc($result)) {
  echo '<div id="konu">';
  echo '<div id="baslik">';
  echo '<a href='icerik.php?=icerik' . $goster['icerik_id'] . ''>' . $goster['baslik'] . '</a>';
  echo '</div>';
  echo '<div id="yazi">';
  echo substr($goster['yazi'],0,300);
  echo '</div>';
  echo '<div id="yazar">';
  echo $goster['yazar'];
  echo '</div>';
  echo '</div>';
}

Link to comment
Share on other sites

1 hour ago, maviyazilim said:

echo '<a href='icerik.php?=icerik' . $goster['icerik_id'] . ''>' . $goster['baslik'] . '</a>';

Remember that PHP does not write HTML.  
It writes strings that just happen to contain some words that your web browser can do something "clever" with. 

You are creating a string literal, and you've chosen to use the single-quote to wrap around it.  So far, so good. 
But your literal contains single quotes as well, and those are confusing PHP.  

You either need to escape your embedded single quotes or, because HTML doesn't care which you use, use double-quotes in the HTML instead

// Either (using double-quotes)
echo '<a href="icerik.php?=icerik' . $goster['icerik_id'] . '">' . $goster['baslik'] . '</a>';

// or (with escaped single-quotes)
echo '<a href=\'icerik.php?=icerik' . $goster['icerik_id'] . '\'>' . $goster['baslik'] . '</a>';

// or, my personal favourite 
printf( '<a href=\'icerik.php?=icerik%s\'>%s</a>', $goster['icerik_id'], $goster['baslik'] );

 

Regards, 
   Phill W.

Link to comment
Share on other sites

The short answer is:

bad quotes:  echo '<a href='icerik.php?=icerik' . $goster['icerik_id'] . ''>' . $goster['baslik'] . '</a>';

good quotes:  echo "<a href='icerik.php?icerik" . $goster['icerik_id'] . '''>" . $goster['baslik'] . '</a>';

OR break it down into parts:

$id = $goster['icerik_id'];

$href = "icerit.php?icerik=$id";

echo "<a href='$href'>" . $goster['baslik'] . "</a>";

You also had errors in your href to begin with which I changed.

Link to comment
Share on other sites

Side Note:

You also can't just insert PHP variables directory into URLs or HTML markup. They have to be escaped and encoded:
 

<?php

function html_escape($value, $encoding) {
  return htmlspecialchars($value, ENT_QUOTES | ENT_HTML5 | ENT_SUBSTITUTE, $encoding);
}

$url = 'icerik.php?'.http_build_query(['icerik' => $goster['icerik_id']]);

echo '<a href="'.html_escape($url , 'UTF-8').'">'.html_escape($goster['baslik'], 'UTF-8').'</a>';

?>

 

Link to comment
Share on other sites

ginerjm

good quotes:  echo "<a href='icerik.php?icerik" . $goster['icerik_id'] . '''>" . $goster['baslik'] . '</a>';

I implemented this code. gave an error. I changed it as below.

echo '<a href="icerik.php?icerik' . $goster['icerik_id'] . '">' . $goster['baslik'] . '</a>';

It works as a link. I reach the file named contentik.php. In the link it says:

https://localhost/blog/icerik.php?icerik22

 

It works but gives an error message.

Warning: Undefined variable $goster in C:\xampp\htdocs\blog\orta.php on line 35

Warning: Trying to access array offset on value of type null in C:\xampp\htdocs\blog\orta.php on line 35

Warning: Undefined array key "" in C:\xampp\htdocs\blog\orta.php on line 35

35 line - $giden = $_POST[$goster['icerik_id']];

When I access the file named contentik.php I get errors.

icerik.php ->

<?php
$gelenid = $_POST['icerik_id'];

$sorgula = mysqli_fetch_array(mysqli_query($conn,"select * from icerik where icerik_id='$gelenid'"));


echo '<div id="icerik">';
     echo '<div id="baslik">' . $row['baslik'] . '</div>';
         echo '<div id="yazi">' . $row['yazi'] . '</div>';
         echo '<div id="yazar">' . $row['yazar'] . '</div>';


echo '</div>';

Here are the errors received.

Warning: Undefined array key "icerik_id" in C:\xampp\htdocs\blog\icerik.php on line 19


Warning: Undefined variable $row in C:\xampp\htdocs\blog\icerik.php on line 25

Warning: Trying to access array offset on value of type null in C:\xampp\htdocs\blog\icerik.php on line 25

Warning: Undefined variable $row in C:\xampp\htdocs\blog\icerik.php on line 26

Warning: Trying to access array offset on value of type null in C:\xampp\htdocs\blog\icerik.php on line 26

Warning: Undefined variable $row in C:\xampp\htdocs\blog\icerik.php on line 27

Warning: Trying to access array offset on value of type null in C:\xampp\htdocs\blog\icerik.php on line 27

 

line 19 - $gelenid = $_POST['icerik_id'];

line 25 - echo '<div id="baslik">' . $row['baslik'] . '</div>'

line 26 - echo '<div id="yazi">' . $row['yazi'] . '</div>';

line 27 - echo '<div id="yazar">' . $row['yazar'] . '</div>';

Link to comment
Share on other sites

I want to learn by solving the problems in the code structure that I know a little bit. I don't understand the code structure that I don't know at all anyway. How can I do what I don't know at all, while I get an error in the structure when I know a little. As I learn, I look at structures that I do not know in the future. I want to solve them for now.

Link to comment
Share on other sites

Huh? We are showing you some PROPER 'code structures' so why don't you want to learn from them???  You are writing incorrect things that give you these errors and you ask for help solving them.  Learn from what we give you, not some other method that you apparently do not know, hence the errors are arising.

One of the best people on this forum gave you a solution and you SNUBBED him.  Really?  A guy spends his time to help you and you just flat out told him you didn't want his help.   

Edited by ginerjm
  • Like 1
Link to comment
Share on other sites

The code structure given by Barand worked. It was a structure that I did not understand. I thanked you for your help.

 

ginerjm the code you gave didn't work. but it was very close to what I knew and did. I worked on it and made it work.

 

ginerjm, I have 2 solutions. So I chose yours, the way I know. is this an error?

 

Thank you to all those who helped.

 

link problem solved. but my problem is that when I click on the link, I cannot capture the content_id number that I sent with the post method on the page I go to and print the relevant content on the screen.

 

The problem has become a different matter. So instead of starting a new thread, I continued here.

thank you to everyone. 

Link to comment
Share on other sites

Remember, people are not there to see what is actually happening or what you are doing. The solution that you get that works might be something that you don't understand, but a person who helped you who put his or her time in should at least be thanked. At least you acknowledge and thanked that in the last post that you made. I once had someone help me with a PHP script that I didn't understand and even to this day still don't after deciphering it, but it worked and there wasn't another solution out there. So I used it anyways as I spent too much time on the particular part of the script. There are a lot of times I don't understand what something does, but only to have a light-bulb go on in my head months or even years later. Object-oriented programming is something that I could do, but really never understood the meat & potatoes of it until now. It's starting to make a lot of sense now and to me that is what is fun with it comes to coding. 

  • Great Answer 1
Link to comment
Share on other sites

I suggest (strongly) that you begin a new post with a new title on it to ask your new question.  That way you will get attention from those who have already passed over this topic.  That's how forums work.

Edited by ginerjm
Link to comment
Share on other sites

On 10/1/2021 at 9:45 AM, Barand said:

Or avoid the concatenation which is usually the biggest source of error (and the query string needs an "=")

echo "<a href='icerik.php?icerik={$goster['icerik_id']}'>{$goster['baslik']}</a>";

 

@maviyazilim:

This is the best way to handle your problem.    Claiming you don't understand this code, so you can't use it, is not acceptable here.  Perhaps if this was in some way complicated, you might have an argument, but this is some of the most simple and basic PHP syntax that exists, and has existed in the language since it was created.  

This is using "Interpolation", which is one of the features that has made PHP great to work with for web development.

When using double quotes, PHP will look at the contents of a string for PHP variables, and replace those variables with their values at runtime.  This code is elegant, easy to read, and far superior to the alternative of concatenating a bunch of pieces together in most situations.  You should try and use it whenever you can.  The link is to the PHP Manual.  Look for "Complex (curly) Syntax".  

I will explain this to you with an example.  You also need to understand PHP arrays, and in particular the way that PHP arrays can have strings as a key.

 

Simple Interpolation:

<?php

$testString = 'scary ghost';
echo "She saw a $testString.  It was green!" . PHP_EOL;

//embed newline with \n
echo "She saw a $testString.  It was blue!\n";

Outputs:

She saw a scary ghost.  It was green!
She saw a scary ghost.  It was blue!

Again this is one of the most basic things about PHP you should know!  The $testString variable gets interpolated.  In the 2nd example I use "\n" for the newline instead of the PHP_EOL constant.

 

Interpolation of array variables is also a fundamental feature of PHP:

<?php
$fruit = array('apple', 'banana', 'orange', 'grape');
echo "I ate one $fruit[0] for breakfast, and one $fruit[3] for lunch\n";

Outputs:

I ate one apple for breakfast, and one orange for lunch

 

Of course PHP arrays can have programmer defined string constants for keys, and this causes a parsing problem for PHP:

$name = array('first' => 'George', 'last' => 'Washington');

// Syntax error
echo "The first US President was $name['first'] $name['last']\n";

 

The problem here is that using "$name['first']" confuses PHP, when it sees the "$var['something']" and breaks the PHP string parser.  So, PHP Helpfully allows you to put curly brackets around the array, telling php to resolve the value of the array, and then interpolate it as it would with any simple variable.

// Interpolating an array with a string constant key
$name = array('first' => 'George', 'last' => 'Washington');

// Using complex syntax
echo "The first US President was {$name['first']} {$name['last']}\n";

Output:

The first US President was George Washington

As in Barand's example to you, this is very useful when you are trying to create html markup.

 

This shows concatenation, which is more complicated, and harder to see minor problems you might have, vs using interpolation, where it is very clear what the markup is that you're creating, as well as the places where variables are being injected into the string.  In both cases, the output is the same html:

$linkData = array('url' =>'http://www.phpfreaks.com', 'name' => 'phpFreaks');

$linkConcatenate = '<a href="' . $linkData['url'] . "'>" . $linkData['name'] . '</a>' . PHP_EOL;

$linkInterpolate = "<a href='{$linkData['url']}'>{$linkData['name']}</a>\n";

echo "PHP questions answered at $linkConcatenate <br>\n";
echo "PHP questions answered at $linkInterpolate <br>\n";


Output:

PHP questions answered at <a href="http://www.phpfreaks.com'>phpFreaks</a>
 <br>
PHP questions answered at <a href='http://www.phpfreaks.com'>phpFreaks</a>
 <br>

 

Yet there is more: because this also works with Object variables and methods:

class Person {
    private $name;
    private $age;
    private $title;
    public $summary;
    
    public function __construct($name, $title, $age) {
        $this->name = $name;
        $this->age = $age;
        $this->title = $title;
        $this->summary = $this->__tostring();
    }
    
    public function __tostring() {
        return "Name: {$this->name}. Age: {$this->age}.  Title: {$this->title}";
    }
    
    public function getName() {
        return $this->name;
    }
    
    public function getAge() {
        return $this->age;
    }
}

$bob = new Person('Bob Smith', 'Manager', 31);
$sue = new Person('Sue Jones', 'Director', 28);

echo "Bob's Summary| {$bob->summary}\n";
echo "Sue's Summary| {$sue->summary}\n";

echo "
<table>
        <tr><th>Employee</th></tr>
        <tr><td>$bob</td></tr>
        <tr><td>$sue</td></tr>
</table>";


$employees[] = $bob;
$employees[] = $sue;

echo "\n\n\n";

foreach ($employees as $employee) {
    echo "{$employee->getName()} is {$employee->getAge()} years old\n";
}

Outputs:

Bob's Summary| Name: Bob Smith. Age: 31.  Title: Manager
Sue's Summary| Name: Sue Jones. Age: 28.  Title: Director

<table>
        <tr><th>Employee</th></tr>
        <tr><td>Name: Bob Smith. Age: 31.  Title: Manager</td></tr>
        <tr><td>Name: Sue Jones. Age: 28.  Title: Director</td></tr>
</table>


Bob Smith is 31 years old
Sue Jones is 28 years old

Interpolation is one of the best things about PHP.  I hope you take the time to learn more about how it works.

You can play with all the examples I provided here if you would like to experiment:  https://3v4l.org/7jUPS

  • Great Answer 1
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.