PHP, Html5, xampp

Posted

技术标签:

【中文标题】PHP, Html5, xampp【英文标题】:Php, Html5, xampp 【发布时间】:2018-08-26 22:10:19 【问题描述】:

我正在尝试执行我在 XAMPP 中拥有的这个 php 文件。我正在尝试通过表单将数据更新到数据库,当我输入信息并提供提交按钮时,页面刚刚重新加载,或者我收到消息尽管我填写了所有字段,但所有字段都是必需的。对代码进行检查。我输入的数据只是没有更新到数据库中。我已经检查了数据库名称和表名称,如果我正确提及它,并且我尝试检查我是否能够连接到数据库。在这种情况下,它会正确连接到数据库。但只是没有更新表中的信息。

<form action="" method="POST">
            <div class="main">
            <div class="icon-head-wthree">
                    <h2><span class="fa fa-heart t-w3" aria-hidden="true"></span></h2>
                    <h4>Donor?</h4>
                    </div>
                                    <div class="form-left-to-w3l">

                            <input type="text" name="name" placeholder="First Name">
                            <div class="clear"></div>
                        </div>
                                    <div class="form-left-to-w3l">

                            <input type="text" name="lname" placeholder="Last Name">
                            <div class="clear"></div>
                        </div>
                        <div class="form-left-to-w3l">

                            <input type="text" name="city" placeholder="City" >
                            <div class="clear"></div>
                        </div>
                        <div class="form-left-to-w3l">

                            <input type="text" name="district" placeholder="District" >
                            <div class="clear"></div>
                        </div>
                        <div class="form-left-to-w3l">

                            <input type="text" name="state" placeholder="State" >
                            <div class="clear"></div>
                        </div>
                        <div class="form-left-to-w3l">

                            <input type="text" name="Phone" placeholder="Phone 1" >
                            <div class="clear"></div>
                        </div>
                        <div class="form-left-to-w3l">

                            <input type="text" name="p1" placeholder="Phone 2">
                            <div class="clear"></div>
                        </div>
                        <div class="form-left-to-w3l">

                            <input type="text" name="pincode" placeholder="Pincode" >
                            <div class="clear"></div>
                        </div>
                        <div class="form-left-to-w3l">

                            <input type="text" name="bg " placeholder="Blood Group" >
                            <div class="clear"></div>
                        </div>
                        <div class="form-left-to-add">

                            <input type="text" name="add1 " placeholder="Address 1" >
                            <div class="clear"></div>
                        </div>
                        <div class="form-left-to-add">

                            <input type="text" name="add2 " placeholder="Address 2" >
                            <div class="clear"></div>
                        </div>
                        <input type="checkbox" name="check" checked="checked">
                        <span class="checkmark"></span>
                        <label class="cont">I agree to the terms and conditions 
                              </label>
                        <div class="btnn">
                <input type="submit" value="submit" name="submit">Submit Request </button><br>
                <div class="clear"></div>
            </div>
            </div>

        </form>
        <?php
if(isset($_POST["submit"]))
    $db= 'bank';

if(!empty($_POST['name']) && !empty($_POST['lname']) && !empty($_POST['city']) && !empty($_POST['district']) && !empty($_POST['state']) && !empty($_POST['Phone']) && !empty($_POST['p1']) && !empty($_POST['pincode']) && !empty($_POST['bg']) && !empty($_POST['add1']) && !empty($_POST['add2'])) 
 //added
    $name=$_POST['name'];
    $lname=$_POST['lname'];
    $city= $_POST['city'];
    $district= $_POST['district'];
    $state= $_POST['state'];
    $Phone= $_POST['Phone'];
    $p1= $_POST['p1'];
    $pincode= $_POST['pincode'];
    $bg= $_POST['bg'];
    $add1= $_POST['add1'];
    $add2= $_POST['add2'];
    $con=mysqli_connect('localhost','root','',$db) or die(mysql_error());


    $sql="INSERT INTO donor VALUES('$name','$lname','$city','$district','$state','$Phone','$p1','$pincode','$bg','$add1','$add2')";

    $result=mysqli_query($con,$sql);

     if($result)
    echo "Account Successfully Created";
     else 
    echo "Failure!";
    




else 
    echo "All fields are required!";

 
  ?>

【问题讨论】:

【参考方案1】:

您的某些输入标签名称有空格,例如 name="bg" 而不是 name="bg"。输入的情况相同 - add1 和 add2。

更新了下面的代码。它应该可以正常工作。

<form action="" method="POST">
            <div class="main">
            <div class="icon-head-wthree">
                    <h2><span class="fa fa-heart t-w3" aria-hidden="true"></span></h2>
                    <h4>Donor?</h4>
                    </div>
                                    <div class="form-left-to-w3l">

                            <input type="text" name="name" placeholder="First Name">
                            <div class="clear"></div>
                        </div>
                                    <div class="form-left-to-w3l">

                            <input type="text" name="lname" placeholder="Last Name">
                            <div class="clear"></div>
                        </div>
                        <div class="form-left-to-w3l">

                            <input type="text" name="city" placeholder="City" >
                            <div class="clear"></div>
                        </div>
                        <div class="form-left-to-w3l">

                            <input type="text" name="district" placeholder="District" >
                            <div class="clear"></div>
                        </div>
                        <div class="form-left-to-w3l">

                            <input type="text" name="state" placeholder="State" >
                            <div class="clear"></div>
                        </div>
                        <div class="form-left-to-w3l">

                            <input type="text" name="Phone" placeholder="Phone 1" >
                            <div class="clear"></div>
                        </div>
                        <div class="form-left-to-w3l">

                            <input type="text" name="p1" placeholder="Phone 2">
                            <div class="clear"></div>
                        </div>
                        <div class="form-left-to-w3l">

                            <input type="text" name="pincode" placeholder="Pincode" >
                            <div class="clear"></div>
                        </div>
                        <div class="form-left-to-w3l">

                            <input type="text" name="bg" placeholder="Blood Group" >
                            <div class="clear"></div>
                        </div>
                        <div class="form-left-to-add">

                            <input type="text" name="add1" placeholder="Address 1" >
                            <div class="clear"></div>
                        </div>
                        <div class="form-left-to-add">

                            <input type="text" name="add2" placeholder="Address 2" >
                            <div class="clear"></div>
                        </div>
                        <input type="checkbox" name="check" checked="checked">
                        <span class="checkmark"></span>
                        <label class="cont">I agree to the terms and conditions 
                              </label>
                        <div class="btnn">
                <input type="submit" value="submit" name="submit">Submit Request </button><br>
                <div class="clear"></div>
            </div>
            </div>

        </form>
        <?php
if(isset($_POST["submit"]))
    $db= 'bank';

if(!empty($_POST['name']) && !empty($_POST['lname']) && !empty($_POST['city']) && !empty($_POST['district']) && !empty($_POST['state']) && !empty($_POST['Phone']) && !empty($_POST['p1']) && !empty($_POST['pincode']) && !empty($_POST['bg']) && !empty($_POST['add1']) && !empty($_POST['add2'])) 
 //added
    $name=$_POST['name'];
    $lname=$_POST['lname'];
    $city= $_POST['city'];
    $district= $_POST['district'];
    $state= $_POST['state'];
    $Phone= $_POST['Phone'];
    $p1= $_POST['p1'];
    $pincode= $_POST['pincode'];
    $bg= $_POST['bg'];
    $add1= $_POST['add1'];
    $add2= $_POST['add2'];
    $con=mysqli_connect('localhost','root','',$db) or die(mysql_error());


    $sql="INSERT INTO donor VALUES('$name','$lname','$city','$district','$state','$Phone','$p1','$pincode','$bg','$add1','$add2')";

    $result=mysqli_query($con,$sql);

     if($result)
    echo "Account Successfully Created";
     else 
    echo "Failure!";
    




else 
    echo "All fields are required!";

 
  ?>

【讨论】:

【参考方案2】:

作为最后一个答案,删除 (blood group : name="bg", address1: name="add1", address2: name="add2") 一些输入标签名称中的空格。

<form action="" method="POST">
        <div class="main">
        <div class="icon-head-wthree">
                <h2><span class="fa fa-heart t-w3" aria-hidden="true"></span></h2>
                <h4>Donor?</h4>
                </div>
                                <div class="form-left-to-w3l">

                        <input type="text" name="name" placeholder="First Name">
                        <div class="clear"></div>
                    </div>
                                <div class="form-left-to-w3l">

                        <input type="text" name="lname" placeholder="Last Name">
                        <div class="clear"></div>
                    </div>
                    <div class="form-left-to-w3l">

                        <input type="text" name="city" placeholder="City" >
                        <div class="clear"></div>
                    </div>
                    <div class="form-left-to-w3l">

                        <input type="text" name="district" placeholder="District" >
                        <div class="clear"></div>
                    </div>
                    <div class="form-left-to-w3l">

                        <input type="text" name="state" placeholder="State" >
                        <div class="clear"></div>
                    </div>
                    <div class="form-left-to-w3l">

                        <input type="text" name="Phone" placeholder="Phone 1" >
                        <div class="clear"></div>
                    </div>
                    <div class="form-left-to-w3l">

                        <input type="text" name="p1" placeholder="Phone 2">
                        <div class="clear"></div>
                    </div>
                    <div class="form-left-to-w3l">

                        <input type="text" name="pincode" placeholder="Pincode" >
                        <div class="clear"></div>
                    </div>
                    <div class="form-left-to-w3l">

                        <input type="text" name="bg " placeholder="Blood Group" > // remoce white space
                        <div class="clear"></div>
                    </div>
                    <div class="form-left-to-add">

                        <input type="text" name="add1 " placeholder="Address 1" > // remoce white space
                        <div class="clear"></div>
                    </div>
                    <div class="form-left-to-add">

                        <input type="text" name="add2 " placeholder="Address 2" > // remoce white space
                        <div class="clear"></div>
                    </div>
                    <input type="checkbox" name="check" checked="checked">
                    <span class="checkmark"></span>
                    <label class="cont">I agree to the terms and conditions 
                          </label>
                    <div class="btnn">
            <input type="submit" value="submit" name="submit">Submit Request </button><br>
            <div class="clear"></div>
        </div>
        </div>

    </form>
    <?php
if(isset($_POST["submit"]) == "submit")
$db= 'bank';

if(!empty($_POST['name']) && !empty($_POST['lname']) && !empty($_POST['city']) && !empty($_POST['district']) && !empty($_POST['state']) && !empty($_POST['Phone']) && !empty($_POST['p1']) && !empty($_POST['pincode']) && !empty($_POST['bg']) && !empty($_POST['add1']) && !empty($_POST['add2'])) 
 //added
$name=$_POST['name'];
$lname=$_POST['lname'];
$city= $_POST['city'];
$district= $_POST['district'];
$state= $_POST['state'];
$Phone= $_POST['Phone'];
$p1= $_POST['p1'];
$pincode= $_POST['pincode'];
$bg= $_POST['bg'];
$add1= $_POST['add1'];
$add2= $_POST['add2'];
$con=mysqli_connect('localhost','root','',$db) or die(mysql_error());


$sql="INSERT INTO donor VALUES('$name','$lname','$city','$district','$state','$Phone','$p1','$pincode','$bg','$add1','$add2')";

$result=mysqli_query($con,$sql);

 if($result)
echo "Account Successfully Created";
 else 
echo "Failure!";





else 
echo "All fields are required!";
     
  ?>

此代码复制并粘贴。

当您使用 isset 功能时,请使用帖子的值来完善状态,例如

if(isset($_POST["submit"]) == "submit")

如果您在一页中有很多按钮,那将非常有帮助,非常感谢。

【讨论】:

【参考方案3】:

可以用两种方式编写 INSERT INTO 语句。

第一种方式同时指定列名和要插入的值:

第 1 步:

INSERT INTO table_name (column1, column2, column3, ...) VALUES (value1, value2, value3, ...);

如果要为表的所有列添加值,则无需在 SQL 查询中指定列名。但是,请确保值的顺序与表中列的顺序相同。 INSERT INTO 语法如下:

第 2 步:

INSERT INTO table_name VALUES (value1, value2, value3, ...);

在您的代码中,我想您正在尝试使用第二种方法,但您没有使用所有列。 (例如 id 列可能是自动递增的)

所以改变你的这个插入代码

INSERT INTO donor VALUES('$name','$lname','$city','$district','$state','$Phone','$p1','$pincode','$bg','$add1','$add2');

第 1 步,如上所示,如下所示。

INSERT INTO donor (name, lname, city, district, state, phone, p1, pincode, bg, add1, add2) VALUES('$name','$lname','$city','$district','$state','$Phone','$p1','$pincode','$bg','$add1','$add2');

【讨论】:

以上是关于PHP, Html5, xampp的主要内容,如果未能解决你的问题,请参考以下文章

PHP HTML5 Shim in functions.php

PHP HTML5 header.php WordPress主题修改

使用 PHP Html5 流式传输视频

PHP, Html5, xampp

php 用于HTML5视频流的PHP VideoStream类

HTML5填充函数.php