Djo64
Djo64

Reputation: 11

Remove character at the end of url

1) I have url like this : http://example.com/post.php?id=1234 And inside : my article

2) but for this url http://example.com/post.php?1234somewords It's also work, i see my article

3) and for this url http://example.com/post.php?somewords I have good 404 page error

Question is : how could i have 404 error for the 2) url ? (alternative question : how could i redirect "1234somewords" to "1234" ?)

php mysql query inside post.php is :

require_once('conn_sql.php');
$post = $_GET['post'];
$nQuery = mysqli_query($conn, "SELECT * FROM `post` WHERE post_id = '$post'");
$res = mysqli_fetch_array($nQuery);

It seems that the query "post=1234somewords" works, and this is not what i want. however, if i search "post=1234somewords" in phpmyadmin, this not works, and this is what i want !

What is the problem with my code ?

Upvotes: 0

Views: 52

Answers (1)

ScaisEdge
ScaisEdge

Reputation: 133400

this happen because mysql use the beginning part of the string as a valid id .. (this i related to the implic data conversion performed by mysql) you should check if your parameter are valid number before perform the query

you could try removing the not numeric value from the string

$result  = preg_replace("/[^0-9]/", "", $_GET['post']; );  

if (is_numeric( $result))  {

  $nQuery = mysqli_query($conn, "SELECT * FROM `post` WHERE post_id = '$post'");
  $res = mysqli_fetch_array($nQuery);

} else {
  ......

}

Upvotes: 1

Related Questions