英文:
Data not stored in database
问题
我在这里找不到错误。我通过以下*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>选择一个选项</option>
<option value="Feminin">女性</option>
<option value="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>选择一个选项</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="提交">
</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 == '选择一个选项' || $discount == '选择一个选项' || !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('连接错误(' . 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 '错误';
die();
}
?>
希望这些代码有助于解决您的问题。
英文:
I'm having trouble finding the error here. I'm receiving data through the following html form:
<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>
And this is the insertpacient.php file:
<?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();
}
?>
It does go to the confirmation page, but data is not stored in the database, for some reason.
I have to mention i have used the very same code for gathering data for another table, but I had to change some input types.
Thank you in advance.
答案1
得分: 2
问题 1: 您正在使用名称producername
发送折扣($_POST['discount']
)。因此,它应该是$_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');
}
英文:
Problem 1: You're sending the discount ($_POST['discount']
) with the name producername
. So, it should be $_POST['producername']
.
Problem 2: Your first query is unnecessary since you can make the lastname
unique or some other columns like email
or phonenumber
.
Problem 3: You should be using &&
instead of ||
in your if
statement. Or you can refactor nested if
statements (arrow type code).
<?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');
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论