我在这里找不到错误。我正在通过以下 html表单
接收数据:<form action="insertpacient.php" method="POST">
<br> Prenume:
<br>
<textarea name="firstname" required></textarea>
<br>
<br> Nume:
<br>
<textarea name="lastname" required></textarea>
<br>
<br> Sex:
<br>
<select name="sex" size="1" required>
<option value="" selected hidden>Selectați o variantă</option>
<option value="Feminin">Feminin</option>
<option value="Masculin">Masculin</option>
</select>
<br>
<br> CNP:
<br>
<textarea name="cnp" required></textarea>
<br>
<br> Număr de telefon:
<br>
<textarea name="phonenumber" required></textarea>
<br>
<br> E-mail:
<br>
<textarea name="email" required></textarea>
<br>
<br> Adresă:
<br>
<textarea name="address" style="height:100px;" required></textarea>
<br>
<br> Discount:
<br>
<select name="producername" size="1" required>
<option value="" selected hidden>Selectați o variantă</option>
<option value="5">5%</option>
<option value="10">10%</option>
<option value="15">15%</option>
</select>
<br>
<br>
<input style="width:40%;background-color:#F4FDFE;border-radius: 5px; border:2px outset #FFFFFF; height:24px" type="submit" value="Submit">
</form>
这是 insertpacient.php 文件:
<?php
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$phonenumber = $_POST['phonenumber'];
$cnp = $_POST['cnp'];
$address = $_POST['address'];
$email = $_POST['email'];
$discount = $_POST['discount'];
$sex = $_POST['sex'];
if (!empty($firstname) || !empty($lastname) || $sex == 'Selectați o variantă' || $discount == 'Selectați o variantă' || !empty($phonenumber) || !empty($cnp) || !empty($address) || !empty($email)) {
$host = "localhost";
$dbUsername = "root";
$dbPassword = "";
$dbname = "pharmagest";
$conn = new mysqli($host, $dbUsername, $dbPassword, $dbname);
if (mysqli_connect_error()) {
die('Connect Error(' . mysqli_connect_errno() . ')' . mysqli_connect_error());
} else {
$SELECT = "SELECT lastname From pacients Where lastname = ? Limit 1";
$INSERT = "INSERT Into pacients (firstname,lastname,phonenumber,cnp,address,email,discount,sex) values(?,?,?,?,?,?,?,?)";
$stmt = $conn->prepare($SELECT);
$stmt->bind_param("s", $lastname);
$stmt->execute();
$stmt->bind_result($lastname);
$stmt->store_result();
$rnum = $stmt->num_rows;
if ($rnum == 0) {
$stmt->close();
$stmt = $conn->prepare($INSERT);
$stmt->bind_param("ssddssds", $firstname, $lastname, $phonenumber, $cnp, $address, $email, $discount, $sex);
$stmt->execute();
header("Location: insertpacientconfirmationpage.php");
} else {
echo "NAH";
}
$stmt->close();
$conn->close();
}
} else {
echo 'ERROR';
die();
}
?>
它确实进入了确认页面,但是由于某种原因数据没有存储在数据库中。
不得不提的是,我使用了完全相同的代码来收集另一个表的数据,但是我不得不更改某些输入类型。
谢谢。
答案 0 :(得分:2)
问题1:。您正在发送名称为$_POST['discount']
的折扣(producername
)。因此,它应该是$_POST['producername']
。
问题2:,因为您可以使lastname
或其他诸如email
或phonenumber
之类的列变为唯一,所以您的第一个查询是不必要的。
问题3::您应该在&&
语句中使用||
而不是if
。或者,您可以重构嵌套的if
语句(箭头类型代码)。
<?php
$firstname = $_POST['firstname'];
$lastname = $_POST['lastname'];
$phonenumber = $_POST['phonenumber'];
$cnp = $_POST['cnp'];
$address = $_POST['address'];
$email = $_POST['email'];
$discount = $_POST['producername'];
$sex = $_POST['sex'];
if (empty($firstname) || empty($lastname) || $sex == 'Selectați o variantă' || $discount == 'Selectați o variantă' || empty($phonenumber) || empty($cnp) || empty($address) || empty($email)) {
die('Error');
}
$host = 'localhost';
$dbUsername = 'root';
$dbPassword = '';
$dbname = 'test';
$conn = new mysqli($host, $dbUsername, $dbPassword, $dbname);
if ($conn->connect_error) {
die('Connect Error(' . mysqli_connect_errno() . ')' . mysqli_connect_error());
}
$INSERT = 'INSERT INTO pacients (firstname, lastname, phonenumber, cnp, address, email, discount, sex) VALUES (?, ?, ?, ?, ?, ?, ?, ?)';
$stmt = $conn->prepare($INSERT);
if ($stmt->bind_param("ssddssds", $firstname, $lastname, $phonenumber, $cnp, $address, $email, $discount, $sex) && $stmt->execute()) {
$stmt->close();
$conn->close();
header('Location: insertpacientconfirmationpage.php');
}