maviyazilim Posted October 1, 2021 Share Posted October 1, 2021 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>'; } Quote Link to comment https://forums.phpfreaks.com/topic/313847-i-am-getting-an-error-while-creating-the-link/ Share on other sites More sharing options...
ginerjm Posted October 1, 2021 Share Posted October 1, 2021 wrong quotes Quote Link to comment https://forums.phpfreaks.com/topic/313847-i-am-getting-an-error-while-creating-the-link/#findComment-1590589 Share on other sites More sharing options...
maviyazilim Posted October 1, 2021 Author Share Posted October 1, 2021 What is missing or wrong? Quote Link to comment https://forums.phpfreaks.com/topic/313847-i-am-getting-an-error-while-creating-the-link/#findComment-1590590 Share on other sites More sharing options...
Barand Posted October 1, 2021 Share Posted October 1, 2021 42 minutes ago, maviyazilim said: What is missing or wrong? @ginerjm has just told you what's wrong. Quote Link to comment https://forums.phpfreaks.com/topic/313847-i-am-getting-an-error-while-creating-the-link/#findComment-1590592 Share on other sites More sharing options...
Phi11W Posted October 1, 2021 Share Posted October 1, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/313847-i-am-getting-an-error-while-creating-the-link/#findComment-1590593 Share on other sites More sharing options...
ginerjm Posted October 1, 2021 Share Posted October 1, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/313847-i-am-getting-an-error-while-creating-the-link/#findComment-1590594 Share on other sites More sharing options...
Barand Posted October 1, 2021 Share Posted October 1, 2021 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>"; 1 1 Quote Link to comment https://forums.phpfreaks.com/topic/313847-i-am-getting-an-error-while-creating-the-link/#findComment-1590595 Share on other sites More sharing options...
ginerjm Posted October 1, 2021 Share Posted October 1, 2021 Was trying to make it clearer without so many quotes. Did I leave out an = somwehere? Quote Link to comment https://forums.phpfreaks.com/topic/313847-i-am-getting-an-error-while-creating-the-link/#findComment-1590599 Share on other sites More sharing options...
thara Posted October 2, 2021 Share Posted October 2, 2021 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>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/313847-i-am-getting-an-error-while-creating-the-link/#findComment-1590616 Share on other sites More sharing options...
maviyazilim Posted October 4, 2021 Author Share Posted October 4, 2021 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>'; Quote Link to comment https://forums.phpfreaks.com/topic/313847-i-am-getting-an-error-while-creating-the-link/#findComment-1590651 Share on other sites More sharing options...
maviyazilim Posted October 4, 2021 Author Share Posted October 4, 2021 Barand. thank you so much. I tried the code you gave and it worked. but I've never seen it written that way before. so i can't use it. Quote Link to comment https://forums.phpfreaks.com/topic/313847-i-am-getting-an-error-while-creating-the-link/#findComment-1590652 Share on other sites More sharing options...
dodgeitorelse3 Posted October 4, 2021 Share Posted October 4, 2021 ???? Quote Link to comment https://forums.phpfreaks.com/topic/313847-i-am-getting-an-error-while-creating-the-link/#findComment-1590653 Share on other sites More sharing options...
Barand Posted October 4, 2021 Share Posted October 4, 2021 52 minutes ago, maviyazilim said: so i can't use it. Your code is wrong for several reasons, but I won't go into them as trying to help you is apparently a complete waste of time. Goodbye. Quote Link to comment https://forums.phpfreaks.com/topic/313847-i-am-getting-an-error-while-creating-the-link/#findComment-1590654 Share on other sites More sharing options...
maviyazilim Posted October 4, 2021 Author Share Posted October 4, 2021 Barand. thank you so much. good bye take care Quote Link to comment https://forums.phpfreaks.com/topic/313847-i-am-getting-an-error-while-creating-the-link/#findComment-1590655 Share on other sites More sharing options...
ginerjm Posted October 4, 2021 Share Posted October 4, 2021 You haven't seen code written like that which is perfectly normal, so "you can't use it"? What kind of nonsense is that? 1 Quote Link to comment https://forums.phpfreaks.com/topic/313847-i-am-getting-an-error-while-creating-the-link/#findComment-1590656 Share on other sites More sharing options...
Barand Posted October 4, 2021 Share Posted October 4, 2021 It's the antithesis of progress and learning. We can only tell him stuff that he already knows, which is pointless. If he doesn't know it he won't use it. Therefore, whatever we tell him is a waste of time. 2 Quote Link to comment https://forums.phpfreaks.com/topic/313847-i-am-getting-an-error-while-creating-the-link/#findComment-1590658 Share on other sites More sharing options...
maviyazilim Posted October 4, 2021 Author Share Posted October 4, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/313847-i-am-getting-an-error-while-creating-the-link/#findComment-1590671 Share on other sites More sharing options...
ginerjm Posted October 4, 2021 Share Posted October 4, 2021 (edited) 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 October 5, 2021 by ginerjm 1 Quote Link to comment https://forums.phpfreaks.com/topic/313847-i-am-getting-an-error-while-creating-the-link/#findComment-1590672 Share on other sites More sharing options...
maviyazilim Posted October 5, 2021 Author Share Posted October 5, 2021 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. Quote Link to comment https://forums.phpfreaks.com/topic/313847-i-am-getting-an-error-while-creating-the-link/#findComment-1590681 Share on other sites More sharing options...
Strider64 Posted October 5, 2021 Share Posted October 5, 2021 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. 1 Quote Link to comment https://forums.phpfreaks.com/topic/313847-i-am-getting-an-error-while-creating-the-link/#findComment-1590682 Share on other sites More sharing options...
ginerjm Posted October 5, 2021 Share Posted October 5, 2021 (edited) 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 October 5, 2021 by ginerjm Quote Link to comment https://forums.phpfreaks.com/topic/313847-i-am-getting-an-error-while-creating-the-link/#findComment-1590686 Share on other sites More sharing options...
gizmola Posted October 5, 2021 Share Posted October 5, 2021 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 1 Quote Link to comment https://forums.phpfreaks.com/topic/313847-i-am-getting-an-error-while-creating-the-link/#findComment-1590702 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.