-4

I want to use a function after WHERE

like this it works

$sql = "SELECT * FROM Prodotti WHERE Id=10";

what if i want the id to be in the URL link? the link example is this: https://www.try.org/product.php?signup=98 this way it's not working

$sql = "SELECT * FROM Prodotti WHERE strpos($fullUrl, signup=Id)";
SuperDJ
  • 7,488
  • 11
  • 40
  • 74

2 Answers2

0

You can get the id by using the $_GET superglobal:

$id = (int) $_GET['signup']; // (int) makes sure it is an integer and no string

Now in order to make it work within your query you first need to make the input secure. You can make an input secure by using mysqli_real_escape_string but since you need an integer and not a string it is better to use a prepared statement.

In your query you can than do

$sql = "SELECT * FROM `Prodotti` WHERE `Id` = $id";

Use backticks around table and column names to prevent mysql reserved word error.

Example of prepared statement:

$mysqli = new mysqli("localhost", "my_user", "my_password", "world");
$id = (int) $_GET['signup']; 
if ($stmt = $mysqli->prepare("SELECT * FROM `Prodotti` WHERE `Id` = ?")) {

    /* bind parameters for markers */
    $stmt->bind_param("i", $id);// i for integer s for string

    /* execute query */
    $stmt->execute();

    /* bind result variables */
    $stmt->bind_result($district);

    /* fetch value */
    $stmt->fetch();

    // Do something with the fetched data

    /* close statement */
    $stmt->close();
}
Script47
  • 14,230
  • 4
  • 45
  • 66
SuperDJ
  • 7,488
  • 11
  • 40
  • 74
-3

You can get the signup parameter from the url like this:

$signup = $_GET['signup'];

and then use it in your query:

$sql = "SELECT * FROM Prodotti WHERE Id = '$signup'";

but this is not secure, i suggest you also google for "php mysql prepared statements"

  • 2
    Why not just suggest the preferred way instead of showing bad code examples? – Script47 Jun 18 '18 at 14:42
  • 2
    While this solution works, it is extremely vunerable to SQL injection. Consider revising this example to provide additional security... – War10ck Jun 18 '18 at 14:43