数据未存储在数据库中

huangapple go评论81阅读模式
英文:

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:

&lt;form action=&quot;insertpacient.php&quot; method=&quot;POST&quot;&gt;
&lt;br&gt; Prenume:
&lt;br&gt;
&lt;textarea name=&quot;firstname&quot; required&gt;&lt;/textarea&gt;
&lt;br&gt;
&lt;br&gt; Nume:
&lt;br&gt;
&lt;textarea name=&quot;lastname&quot; required&gt;&lt;/textarea&gt;
&lt;br&gt;
&lt;br&gt; Sex:
&lt;br&gt;
&lt;select name=&quot;sex&quot; size=&quot;1&quot; required&gt;
&lt;option value=&quot;&quot; selected hidden&gt;Selectați o variantă&lt;/option&gt;
&lt;option value=&quot;Feminin&quot;&gt;Feminin&lt;/option&gt;
&lt;option value=&quot;Masculin&quot;&gt;Masculin&lt;/option&gt;
&lt;/select&gt;
&lt;br&gt;
&lt;br&gt; CNP:
&lt;br&gt;
&lt;textarea name=&quot;cnp&quot; required&gt;&lt;/textarea&gt;
&lt;br&gt;
&lt;br&gt; Număr de telefon:
&lt;br&gt;
&lt;textarea name=&quot;phonenumber&quot; required&gt;&lt;/textarea&gt;
&lt;br&gt;
&lt;br&gt; E-mail:
&lt;br&gt;
&lt;textarea name=&quot;email&quot; required&gt;&lt;/textarea&gt;
&lt;br&gt;
&lt;br&gt; Adresă:
&lt;br&gt;
&lt;textarea name=&quot;address&quot; style=&quot;height:100px;&quot; required&gt;&lt;/textarea&gt;
&lt;br&gt;
&lt;br&gt; Discount:
&lt;br&gt;
&lt;select name=&quot;producername&quot; size=&quot;1&quot; required&gt;
&lt;option value=&quot;&quot; selected hidden&gt;Selectați o variantă&lt;/option&gt;
&lt;option value=&quot;5&quot;&gt;5%&lt;/option&gt;
&lt;option value=&quot;10&quot;&gt;10%&lt;/option&gt;
&lt;option value=&quot;15&quot;&gt;15%&lt;/option&gt;
&lt;/select&gt;
&lt;br&gt;
&lt;br&gt;
&lt;input style=&quot;width:40%;background-color:#F4FDFE;border-radius: 5px; border:2px outset #FFFFFF; height:24px&quot; type=&quot;submit&quot; value=&quot;Submit&quot;&gt;
&lt;/form&gt;

And this is the insertpacient.php file:

&lt;?php
$firstname   = $_POST[&#39;firstname&#39;];
$lastname    = $_POST[&#39;lastname&#39;];
$phonenumber = $_POST[&#39;phonenumber&#39;];
$cnp         = $_POST[&#39;cnp&#39;];
$address     = $_POST[&#39;address&#39;];
$email       = $_POST[&#39;email&#39;];
$discount    = $_POST[&#39;discount&#39;];
$sex         = $_POST[&#39;sex&#39;];
if (!empty($firstname) || !empty($lastname) || $sex == &#39;Selectați o variantă&#39; || $discount == &#39;Selectați o variantă&#39; || !empty($phonenumber) || !empty($cnp) || !empty($address) || !empty($email)) {
$host       = &quot;localhost&quot;;
$dbUsername = &quot;root&quot;;
$dbPassword = &quot;&quot;;
$dbname     = &quot;pharmagest&quot;;
$conn = new mysqli($host, $dbUsername, $dbPassword, $dbname);
if (mysqli_connect_error()) {
die(&#39;Connect Error(&#39; . mysqli_connect_errno() . &#39;)&#39; . mysqli_connect_error());
} else {
$SELECT = &quot;SELECT lastname From pacients Where lastname = ? Limit 1&quot;;
$INSERT = &quot;INSERT Into pacients (firstname,lastname,phonenumber,cnp,address,email,discount,sex) values(?,?,?,?,?,?,?,?)&quot;;
$stmt = $conn-&gt;prepare($SELECT);
$stmt-&gt;bind_param(&quot;s&quot;, $lastname);
$stmt-&gt;execute();
$stmt-&gt;bind_result($lastname);
$stmt-&gt;store_result();
$rnum = $stmt-&gt;num_rows;
if ($rnum == 0) {
$stmt-&gt;close();
$stmt = $conn-&gt;prepare($INSERT);
$stmt-&gt;bind_param(&quot;ssddssds&quot;, $firstname, $lastname, $phonenumber, $cnp, $address, $email, $discount, $sex);
$stmt-&gt;execute();
header(&quot;Location: insertpacientconfirmationpage.php&quot;);
} else {
echo &quot;NAH&quot;;
}
$stmt-&gt;close();
$conn-&gt;close();
}
} else {
echo &#39;ERROR&#39;;
die();
}
?&gt;

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设置为唯一或其他某些列,如emailphonenumber

问题 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[&#39;discount&#39;]) with the name producername. So, it should be $_POST[&#39;producername&#39;].

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 &amp;&amp; instead of || in your if statement. Or you can refactor nested if statements (arrow type code).

&lt;?php
$firstname   = $_POST[&#39;firstname&#39;];
$lastname    = $_POST[&#39;lastname&#39;];
$phonenumber = $_POST[&#39;phonenumber&#39;];
$cnp         = $_POST[&#39;cnp&#39;];
$address     = $_POST[&#39;address&#39;];
$email       = $_POST[&#39;email&#39;];
$discount    = $_POST[&#39;producername&#39;];
$sex         = $_POST[&#39;sex&#39;];
if (empty($firstname) || empty($lastname) || $sex == &#39;Selectați o variantă&#39; || $discount == &#39;Selectați o variantă&#39; || empty($phonenumber) || empty($cnp) || empty($address) || empty($email)) {
die(&#39;Error&#39;);
}
$host       = &#39;localhost&#39;;
$dbUsername = &#39;root&#39;;
$dbPassword = &#39;&#39;;
$dbname     = &#39;test&#39;;
$conn = new mysqli($host, $dbUsername, $dbPassword, $dbname);
if ($conn-&gt;connect_error) {
die(&#39;Connect Error(&#39; . mysqli_connect_errno() . &#39;)&#39; . mysqli_connect_error());
}
$INSERT = &#39;INSERT INTO pacients (firstname, lastname, phonenumber, cnp, address, email, discount, sex) VALUES (?, ?, ?, ?, ?, ?, ?, ?)&#39;;
$stmt = $conn-&gt;prepare($INSERT);
if ($stmt-&gt;bind_param(&quot;ssddssds&quot;, $firstname, $lastname, $phonenumber, $cnp, $address, $email, $discount, $sex) &amp;&amp; $stmt-&gt;execute()) {
$stmt-&gt;close();
$conn-&gt;close();
header(&#39;Location: insertpacientconfirmationpage.php&#39;);
}

huangapple
  • 本文由 发表于 2020年1月6日 02:51:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/59603137.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定