【问题标题】:input form data into database through php通过php将表单数据输入数据库
【发布时间】:2018-08-12 21:41:35
【问题描述】:

我无法弄清楚我的代码有什么问题。数据未存储在 phpmyadmin 中。可能是语法错误。如果有人能找到我的代码的问题,我会很高兴。请帮我解决这个问题!

这是一个简单的表格,询问需要血液的人的详细信息。我需要将详细信息存储到名为“blood_share_system”的数据库和表名“acceptors”中。

我的 html 文件:

<form action="needblood.inc.php"method="post">
    <fieldset>
        <legend>Personal Information</legend>
            <label>FirstName</label><br>
              <input type="text"name="first"required><br>
            <label>LastName</label><br>
              <input type="text"name="last"required><br>
            <label>PatientFirstName</label><br>
              <input type="text"name="pfirst" required><br> 
              <lable>PatientLastName</label><br>
              <input type="text"name="plast"<br>

        </legend>
    </fieldset> 
    <fieldset>
        <legend>Contact Information</legend>
        <lable>Phone Number</label><br>
        <input type="tel"placeholder="10-digit PhoneNumber"name="phno" required><br>
        <lable>Email Address</label><br>
        <input type="email" placeholder="Valid email address"name="email"><br>
    </fieldset>
    <fieldset>   
           <legend>Blood Group Information</legend>
           <label>Blood Group</label><br>
            <select name="bgroup">
                <option value="1">O-positive</option>
                <option value="2">O-negative</option>
                <option value="3">A-positive</option>
                <option value="4">A-negative</option>
                <option value="5">B-positive</option>
                <option value="6">B-negative</option>
                <option value="7">AB-positive</option>
                <option value="8">AB-negative</option>
             </select><br>
            <label>Quantity required(1Unit:350ml):</label><br>
            <button  class="nbtn"id="snbtn"onclick="nbuttonClick()">-</button>
        <input type="number" readonly="true" id="inc" value="0"name="quantity"required>
        <button class="pbtn"id="sbtn"onclick="buttonClick()">+</button>Unit</h4><br>
        <input type="number" id="result">ml
    </fieldset>
    <fieldset>
        <legend>Location Details</legend>
        <label>Locality</lable><br>
        <input type="text"name="loc"required><br>
        <label>Pincode</label><br>
        <input type="number"name="pincode"required>
    </fieldset>
<input type="submit" name="submit"value="Submit">
</form>

我的 php 文件:

<?php
//first if
if(isset($_POST['submit'])){
    $dbserverName = "localhost";
    $dbuserName = "root";
    $dbpassword = "";
    $dbname = "blood_share_system";

    $conn = mysqli_connect( $dbserverName , $dbuserName , $dbpassword , $dbname);

    //2nd if
    if(!$conn){
        echo "connection was not established with server";
    }//2nd if close

    //3rd if
    if(!mysqli_select_db($conn,'blood_share_system')){
        echo "not connected to the database";
    }//3rd if close

    $first=mysqli_real_escape_string($conn,$_POST['first']);
    $last=mysqli_real_escape_string($conn,$_POST['last']);
    $pfirst=mysqli_real_escape_string($conn,$_POST['pfirst']);
    $plast=mysqli_real_escape_string($conn,$_POST['plast']);
    $phno=mysqli_real_escape_string($conn,$_POST['phno']);
    $email=mysqli_real_escape_string($conn,$_POST['email']);
    $bgroup=mysqli_real_escape_string($conn,$_POST['bgroup']);
    $quantity=mysqli_real_escape_string($conn,$_POST['quantity']);
    $locality=mysqli_real_escape_string($conn,$_POST['loc']);
    $pincode=mysqli_real_escape_string($conn,$_POST['pincode']);

    if (!preg_match("/^[a-zA-Z]*$/" , $first) || !preg_match("/^[a-zA-Z]*$/" , $last)||!preg_match("/^[a-zA-Z]*$/" , $pfirst)||!preg_match("/^[a-zA-Z]*$/" , $plast) ){
        trigger_error('Enter valid names!',E_USER_ERROR);
    } else {
        if(!preg_match("/^[0-9]{10}$/", $phno)){
            trigger_error('Enter 10 digit phone number!');
        } else{
            if (!filter_var( $email , FILTER_VALIDATE_EMAIL )){
                trigger_error('Enter valid email address!',E_USER_ERROR);
            } else{
                if(!preg_match("/^[0-9]{6}$/", $pincode)){
                    trigger_error('Enter valid pincode!',E_USER_ERROR);
                } else {
                    $sql="INSERT INTO acceptors('$first','$last','$pfirst','$plast','$phno','$email','$bgroup','$quantity','$locality','$pincode');";
                    if(!mysqli_query($conn, $sql)){
                        echo "Not inserted";
                    } else {
                        echo"Inserted";
                    }
                }
            }
        }
    }
}

【问题讨论】:

  • 任何错误信息,如无效的 csrf 令牌?
  • trigger_error.. 检查你的错误日志,它说了什么?
  • 初始化 $sql 后 echo 它以字符串形式获取 SQL 查询。拥有它后,您可以在 phpmyadmin 中执行查询以获取错误。
  • 查看 SQL 查询,我可以想到您可能放错了列值或没有为所有列分配值。使用“insert into values”语句总是安全的,而不是简单的“insert into”语句。
  • 尝试删除所有 if 并直接跳转到插入而不检查任何 preg_match,看看是否有效,如果有效,则逐个添加每个 if 和验证。此外,避免嵌套 if/else 语句......这使得阅读和维护代码变得非常困难。我建议在每一个之后使用它们,并在任何东西被破坏的情况下返回错误。

标签: php html sql


【解决方案1】:

其中一个可能的原因是使用 insert into 语句而不是 insert into... values 语句。

如果您使用 insert into 语句,则需要以正确的列顺序提供所有列值。

【讨论】:

  • 但是我在这里插入一整行,考虑到除主键之外的所有属性......这不应该吗?
  • 或者可能是由于数据库中的任何类型不匹配。除了 blood_quantity 和 pincode 以及 id 之外,一切都是 varchar。
  • 就是这样,当使用insert into 语句时,您不能假设任何内容,因此您需要使用 null 作为主键的占位符。即使您的主键是自动增量的,使用 null 作为占位符也没关系。但作为一种良好做法,应坚持使用insert into... values 声明。
  • 在这种情况下无需担心数据类型,引用的整数在插入/更新到数据库时会隐式转换为整数。
  • 好的,我也尝试了备用 insert into 语句...没有任何改变...除了语法之外,我还需要验证其他标准吗?我已经创建了同名的数据库,并且表,apache服务器和sql也启动了。
猜你喜欢
  • 2017-01-31
  • 2015-09-28
  • 1970-01-01
  • 2016-06-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多