0

I try, to run this part of code but it keeps, saying the same error. On line 14, i tried already multiple things to check if the connection was done right but everyting seems alright. I think the error is on another line but i cant find it. ($id = $_GET['id'];)This is the error its always giving.

<?php
$servername = "localhost";
$username = "root";
$password = "";
$db = "netland";

try {
    $conn = new PDO("mysql:host=$servername;dbname=$db", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); 
} catch (PDOException $e) {
    echo "Connection failed: " . $e->getMessage();
}   

$id = $_GET['id'];

$data = $conn->query("SELECT * FROM Series WHERE id = " . $id)->fetchAll();

echo "<td><a href='index.php?'>Terug</a><br></td>"; 


foreach ($data as $row) {
    echo "<br>";
    echo "<table>";
    echo "<h1>" . $row["title"] . ' - ' . $row["rating"] . "</h1>";
    echo "<tr>";
    echo "<th>Awards:</th>";
    echo "<td>" . $row["has_won_awards"] . "<br/></th>";
    echo "</tr>";
    echo "<th>Seasons:</th>";
    echo "<td>" . $row["seasons"] . "<br/></th>";
    echo "</tr>";
    echo "<th>Country:</th>";
    echo "<td>" . $row["country"] . "<br/></th>";
    echo "</tr>";
    echo "<th>Language:</th>";
    echo "<td>" . $row["language"] . "<br/></th>";
    echo "</tr>";
    echo "</table>";
    echo "<br><br>";
    echo "<td>" . $row["description"] . "<br/></td>";
    echo "</tr>";
}
?>

I hope someone knows, how to fix this.

Jean
  • 49
  • 8
  • 2
    [Please do not upload images of code/errors when asking a question.](//meta.stackoverflow.com/q/285551) – CBroe Sep 13 '21 at 09:01
  • 1
    _"On line 14, i tried already multiple things to check if the connection was done right"_ - this has nothing to do with the database connection. Your query errors, because it did not contain any actual ID value to search for, it literally just ends with `WHERE id = `. And that is because of the _previous_ error, that `$_GET['ID']` was not set to begin with. You always need to go about fixing such errors in sequences, one after the other. – CBroe Sep 13 '21 at 09:04
  • noo, Still dontget to fix it. Still giving same error same line.... – Jean Sep 13 '21 at 09:14
  • are you sure you are receiving the `id` parameter in your GET request that uses this code ? – Moussab Kbeisy Sep 13 '21 at 09:15
  • 1
    The URL you called this script by, did not contain any `id` parameter in the query string. The only thing you _can_ fix here in this script part, is that you script does not throw an error in this case. (And how to do that, is explained in _great detail_ in the mentioned duplicate.) But you will only get a proper result, once you actually start passing a value for this parameter. If you called the script via a link from some other place in your site - then you need to make sure that you are correctly passing the ID in the link URL there. – CBroe Sep 13 '21 at 09:18
  • What is your URL when calling this script? – brombeer Sep 13 '21 at 09:20
  • http://localhost/database/Level%205/Series-en-films-wijzigen-633c0ec9-1998ac36/edit_serie.php – Jean Sep 13 '21 at 09:24
  • Are you rewriting urls? (If so you might also want to post your rewrite rules, as they might not work as intended) The Notice (and other users) is telling you, there is no `$_GET['id']` in that url. `edit_serie.php?id=12` would have it set. – brombeer Sep 13 '21 at 09:30
  • Your query is insecure. If you wrote the query properly using `$conn->prepare()` and `execute()` you would not get this error. – GrumpyCrouton Sep 13 '21 at 17:58

1 Answers1

0

your $id variable is not set. You need to set it first so you can use it in your query.

geertjanknapen
  • 1,159
  • 8
  • 23
  • Yes i know, But i already setted the variable with ```$id = $_GET['id'];``` right? – Jean Sep 13 '21 at 09:17
  • 1
    You _would_ be setting `$id` to the value of `$_GET['id']` here - **if** `$_GET['id']` existed in the first place. But the error message you are getting clearly tells, that wasn't the case to begin with. – CBroe Sep 13 '21 at 09:19
  • @Jean well yes but no, since the Undefined index error keeps throwing it means that `$id` has not been set. You need to check why that's happening. – geertjanknapen Sep 13 '21 at 09:22
  • do, you might know some ways @geertjanknapen to check how and why that is happening? Because its so weird. like on a another page with exact the same code it works perfectly fine. (I really dont know how to solve it...) – Jean Sep 13 '21 at 09:27
  • First of, try `var_dump($_GET['id'];` after you (try to) set your `$id` variable. This probably returns nothing but I always like to check. Other than that I would have to see some more of your code. Contact me? – geertjanknapen Sep 13 '21 at 09:31
  • @Jean The duplicate CBroe posted has ways to fix it. Check if `$_GET['id']` is set and _only then_ run you query. – brombeer Sep 13 '21 at 09:33
  • @geertjanknapen mate, even var dump doesnt work. – Jean Sep 13 '21 at 10:49
  • @Jean You're acting like I broke your code, I'm only trying to lend a helping hand. var_dump works, are you implementing it correctly? What does your URL look like? Do you understand how `$_GET['']` works? – geertjanknapen Sep 13 '21 at 11:37
  • Yes, ill fix it thanks for your help. – Jean Sep 14 '21 at 07:42