3
I have the $_GET['id']
and I need to check if the value it takes from the url index.php?id=
is 1 or 2, and if none of the alternatives runs a die();
, if it is 1 or 2 assigning $var = $_GET['id'];
and with a if
calls each of the operations for each id
specifies and executes the SQL operation, which is not being executed.
Complete 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 should I return ERRO #1
in the operation 1. (Inverted)
Thank you for editing, demonstrated your real interest in helping. @Bacco, if possible could you explain this to me
$count
, if he is not needed there, why is he? What is he for exactly?– Florida
@Florida for me does not make sense a variable with that name. o
execute
will returntrue
whether the operation was successful, orfalse
otherwise.– Bacco