通过php将表单数据输入数据库

时间:2018-08-12 09:52:19

标签: php html sql

我无法弄清楚我的代码出了什么问题。数据未存储在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";
                    }
                }
            }
        }
    }
}

1 个答案:

答案 0 :(得分:0)

可能的原因之一是使用插入语句而不是插入...值语句。

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