I have problem cant exist catogaries : I want chek if the catogary is add before or not please i need help I have problem cant exist catogaries : I want chek if the catogary is add before or not please i need help I have problem cant exist catogaries : I want chek if the catogary is add before or not please i need help I have problem cant exist catogaries : I want chek if the catogary is add before or not please i need help I have problem cant exist catogaries : I want chek if the catogary is add before or not please i need help I have problem cant exist catogaries : I want chek if the catogary is add before or not please i need help I have problem cant exist catogaries : I want chek if the catogary is add before or not please i need help
<?php
require '0.php';
if (isset($_POST['login'])){
$catogary_name = $_POST['catogary-name'];
$catogary_much = $_POST['catogary-much'];
if (empty($catogary_name) == true || empty($catogary_much) == true){
}else{
$sql = "insert into catograyies (CATOGRARY,howMush) VALUES (' $catogary_name','$catogary_much')";
if ($sq->query($sql) === TRUE) {
} else {
}
}
header("location:");
exit;
}
?>
CodePudding user response:
Use a SELECT
query first to check if the category already exists.
<?php
require '0.php';
if (isset($_POST['login'])){
$catogary_name = $_POST['catogary-name'];
$catogary_much = $_POST['catogary-much'];
if (empty($catogary_name) == true || empty($catogary_much) == true){
}else{
$stmt = $sq->prepare("SELECT 1 FROM catograyies WHERE CATOGRARY = ?");
$stmt->bind_param("s", $catogary_name);
$stmt->execute();
$result = $stmt->get_result();
if ($result->num_rows > 0) {
echo "Category $catogary_name already exists";
} else {
$stmt = $sq->prepare("insert into catograyies (CATOGRARY,howMush) VALUES (?, ?)");
$stmt->bind_param("ss", $catogary_name, $catogary_much);
if ($stmt->execute()) {
} else {
}
}
}
header("location:");
exit;
}
?>
I've also shown how to use prepared statements instead of substituting variables into the SQL, to protect against SQL-injection.
CodePudding user response:
I would do a select first. Here is an example below, this won't work out the box but will give you an idea what todo.
<?php
require '0.php';
if (isset($_POST['login'])){
$catogary_name = $_POST['catogary-name'];
$catogary_much = $_POST['catogary-much'];
if (empty($catogary_name) || empty($catogary_much) ){
} else {
$checkExistsQuery = sprintf("SELECT id FROM catograyies WHERE CATOGRARY = %s", $catogary_name);
$checkExists = $sq->query($checkExists);
if (count($checkExists) > 0) {
return 'Already Exists'
}
$sql = "insert into catograyies (CATOGRARY,howMush) VALUES (' $catogary_name','$catogary_much')";
if ($sq->query($sql) === TRUE) {
} else {
}
}
header("location:");
exit;
}
?>