Witajcie chciałam się zapytać czy tak powinien wyglądać skrypt do newsletter zbierających tylko numery telefonów komórkowych i dodający do bazy sprawdza czy już nie był wcześniej wpisany.
Formularz robi wstępną walidacje nie wiem czy ew można jeszcze coś dodać
<input type="phone" name="phone" maxlength="9" minlength="9" required>
<form action="./admin/code.php" method="POST" style="width: 100%;">
<div class="msg">
<?php include('message.php');?>
<?php
$category = "SELECT * FROM `categories` WHERE c_status='0' ";
$category_run = mysqli_query($con, $category);
if (mysqli_num_rows($category_run) > 0) {
?>
<select name="city" required style="padding: 5px 0;">
<option value="">--Wybierz Miasto--</option>
<?php
foreach ($category_run as $categoryitem) {
?>
<option value="<?= $categoryitem['c_name'] ?>"><?= $categoryitem['c_name'] ?></option>
<?php
}
?>
</select>
<?php
} else {
?>
<h5>Brak dostępnych miast</h5>
<?php
}
?>
</div>
<div class="msg">
<div class="text">Telefon *</div>
<input type="phone" name="phone" maxlength="9" minlength="9" required>
</div>
<div class="btn">
<button type="submit" name="newsletter">Wyślij</button>
</div>
</form>
i potem
if (isset($_POST['newsletter'])) {
$city = htmlspecialchars($_POST['city']);
$phone = preg_replace('/[^0-9]/', '', $_POST['phone']);
if (strlen($phone) == 9) {
$query = "INSERT INTO `newsletter` (city, phone) VALUES ('$city','$phone')";
$sql1 = "SELECT * FROM `newsletter` WHERE city='$city' AND phone='".$phone."' LIMIT 1";
$run_query = mysqli_query($con, $sql1);
if (mysqli_num_rows($run_query) > 0) {
$_SESSION['message'] = "Twój numer jest już dodany do bazy";
header('location: ../index.php#newsletter');
exit(0);
} else {
$query_run = mysqli_query($con, $query);
if ($query_run) {
$_SESSION['message'] = "Numer dodano do bazy";
header('location: ../index.php#newsletter');
exit(0);
} else {
$_SESSION['message'] = "Coś poszło nie tak!";
header('location: ../index.php#newsletter');
exit(0);
}
}
} else {
$_SESSION['message'] = "Niepoprawny nr telefonu!";
header('location: ../index.php#newsletter');
exit(0);
}
}
dany numer jest przypisywany do miasta z pola select i założyłam, że nr tel może być przypisany do kilku miast,
skrypt działa ale czy tak jest poprawnie ?? Wzorowałam się na tym co w sieci jest podane.