I have $_GET['id']
and I need to check if the value it takes from url index.php?id=
is 1 or 2 , and if none of the alternatives performs a die();
, if it is 1 or 2 it assigns $var = $_GET['id'];
and with a if
it calls each of the operations for each specific id
% and executes the SQL operation, it is not being executed.
Full Code:
//Verifica se id é 1 ou 2
if (!preg_match('/^[1-2]/', $_GET['id'])) {
die();// not valid
} else {
$var = $_GET['id'];
}
//Recebe outros dados do index.php via post
$pk = $_POST['pk'];
$name = $_POST['name'];
$value = $_POST['value'];
$conn = new PDO('mysql:dbname=my_database;host=127.0.0.1', 'root', '');
//$conn->exec("SET CHARACTER SET utf8"); #Estava em um exemplo, não sei se é necessario aqui
//index.php?id=1
if($var == "1"){
//Executa SQL
$sql = "UPDATE table_1 ".
"SET :name=':value' ".
"WHERE name_id = ':pk'";
}
else
{
die("ERRO #1");
}
//index.php?id=2
if($var == "2"){
//Executa SQL2
$sql = "UPDATE table_2 ".
"SET :name=':value' ".
"WHERE name_id = ':pk'";
}
else
{
die("ERRO #2");
}
$statement = $conn->prepare($sql);
$statement->bindValue(":pk", $pk);
$statement->bindValue(":name", $name);
$statement->bindValue(":value", $value);
$count = $statement->execute();
Debug : Returns ERRO #2
when it should return ERRO #1
in operation 1 . (Inverted)