$show =$_GET; //if i put the id manually it works and the content get changes
print_r($_GET); // Working
$news_sql=mysql_query( "SELECT *FROM latestnews WHERE id = '$show' ");//i cant retrieve id here
while($rsnews = mysql_fetch_assoc($news_sql)){
?>

my code

<?php
}
?>

Dani AI

Generated

Brief diagnosis and practical next steps.

The output from the superglobal was visible in the original post, so the most likely causes are: the script is assigning the whole request array instead of the specific parameter, the request method in the form/URL doesn't match the code reading it, or the raw value is being concatenated into an SQL string without validation. was right to check the form method, rightly pointed out the need for the id to be present in the request, and ’s syntax nit is good to correct but is unlikely to be the root cause.

Concrete checklist and a safe example:

  • Confirm the id parameter is actually being sent (either in the query string or as a submitted field). Browser differences will not drop a query parameter; missing/incorrect code will.
  • Validate and coerce the input to an integer before using it.
  • Stop using the old mysql_* API; migrate to PDO or mysqli and use prepared statements to eliminate injection risk and to work on modern PHP.

Example (PDO, validation, simple error handling):

<?php
$id = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT);
if ($id === false || $id === null) {
    http_response_code(400);
    exit('Missing or invalid id');
}
$pdo = new PDO('mysql:host=localhost;dbname=yourdb;charset=utf8mb4','dbuser','dbpass',[PDO::ATTR_ERRMODE=>PDO::ERRMODE_EXCEPTION]);
$stmt = $pdo->prepare('SELECT * FROM latestnews WHERE id = :id');
$stmt->execute([':id'=>$id]);
$news = $stmt->fetch(PDO::FETCH_ASSOC);
if (!$news) { http_response_code(404); exit('Not found'); }
?>

Final notes: prefer a hidden form field if the id must travel with a POST submission; log SQL errors during debugging; avoid echoing raw input back to output; and migrate off ext/mysql since it is removed from modern PHP. These steps address the points raised by , and while adding validation and a secure query pattern.

Recommended Answers

All 5 Replies

Check your form method in html page whether you have given method="get" or "post". if you gave get means, there will be no problem. If you use post and here you use $_GET, it definitely be a problem. And additionally, i suggest you to use post method, it is more reliable and have many advantages than Get method and this problem will get over. There is nothing in your code. Try it and solve.
If still any probs, please let me know..

You can still use the $_GET method to grab variables from your URL using form method="post", but you'd have to append the Id variable to the form action like this:

<form action="<?php 'process.php?id=$id' ?>" method="post">

(that's if you're actually using a form, you haven't stated whether you are).

Can you be a bit more specific with what you're trying to do?

Oh... But this id passing through code cannot works sometimes in Mozilla, IE and Opera. This will not works perfectly some times. So, directly passing through code is not good practice. I am sorry if my suggestion hurts..

Your sql query has a syntax error in it. There should be a space after *:

$news_sql=mysql_query( "SELECT * FROM latestnews WHERE id = '$show' ");

The query will still work even if there isn't a space between the asterisk and the 'FROM'
The query looks fine. The issue is most likely with the variable not being set.
Try this and post the result:

<?php
    $show = $_GET['id'];

    if (isset($show))
    {
        $news_sql = mysql_query("SELECT *FROM latestnews WHERE id='$show'") or die("MySQL syntax error: ". mysql_error());
    }
    else { die ('$show isn't set! the $_get didn't work...'); }
?>

Note:- The $_GET must mean that the URL has a variable called 'id' (www.site.com/script.php?id=2)
And say I used $_GET then the url should be something like (www.site.com/script.php?foo=bar)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.