mysql查询在表单页中存储的空白数据

g9icjywg  于 2021-06-20  发布在  Mysql
关注(0)|答案(3)|浏览(222)

该表单旨在捕获新用户并将用户数据存储在数据库中,只是它不存储任何数据,尽管该表单仍然返回一条成功消息。
表单页:

<?php

$servername = "*****";
$username = "*****";
$password = "*****";
$database = "*****";

// Create connection
$conn = new mysqli($servername, $username, $password, $database);

// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} 
echo "Connected successfully";

 ?>

<div class="row">
<div class="col-xs-12">
    <section class="panel">
        <header class="panel-heading">
            <h2 class="panel-title">Laai Nuwe Lid</h2>
        </header>
        <div class="panel-body">
            <form class="form-horizontal form-bordered" action="" 
method="post">

                <p><strong>ID:</strong> Nuwe lid</p>

                                    <div class="form-group">
                    <label class="col-md-3 control-label" 
for="FirstName">Naam:</label>
                        <div class="col-md-6">
                            <input type="text" class="form-control" 
name="FirstName" id="FirstName" value="<?php echo $firstname; ?>">
                        </div>
                </div>  

                                    <div class="form-group">
                    <label class="col-md-3 control-label" 
for="LastName">Van:</label>
                        <div class="col-md-6">
                            <input type="text" class="form-control" 
name="LastName" id="LastName" value="<?php echo $lastname; ?>"'>
                        </div>
                </div>

                <div class="form-group">
                    <label class="col-md-3 control-label" 
for="Cell">Selfoon:</label>
                        <div class="col-md-6">
                            <input type="text" class="form-control" 
name="Cell" id="Cell" value="<?php echo $cell; ?>">
                        </div>
                </div>

                <div class="form-group">
                    <label class="col-md-3 control-label" 
for="Address">Addres:</label>
                        <div class="col-md-6">
                            <input type="text" class="form-control" 
name="Address" id="Address" value="<?php echo $adress; ?>">
                        </div>
                </div>

                                    <div class="row">
                        <div class="col-sm-9 col-sm-offset-3">
                            <button value="submit" type="submit" 
name="submit" class="btn btn-primary">Stoor nuwe lid</button>
                            <button type="reset" class="btn btn- 
default">Kanselleer</button>
                        </div>
                </div>

            </form>
        </div>
    </section>
</div>
</div>

<?php

// check if the form has been submitted. If it has, start to process the 
form and save it to the database

if (isset($_POST['submit'])) {

// get form data, making sure it is valid

$firstname = 
mysql_real_escape_string(htmlspecialchars($_POST['firstname']));
$lastname = 
mysql_real_escape_string(htmlspecialchars($_POST['lastname']));
$cell = mysql_real_escape_string(htmlspecialchars($_POST['cell']));
$address = mysql_real_escape_string(htmlspecialchars($_POST['address']));

$sql = "INSERT INTO `tblusers` (FirstName, LastName, Cell, Address) VALUES 
('$firstname','$lastname', '$cell','$address')";

if ($conn->query($sql) === TRUE) {
    echo "New record created successfully";
} else {
 echo "Error: " . $sql . "<br>" . $conn->error;
}

// once saved, redirect back to the view page

header("Location: index.php");

}      
?>

我不确定问题是php还是sql代码,因为我没有收到错误消息。
数据库连接良好。该查询直接在mysql中工作,但是当我将php与html表单结合使用时,它会存储空行。

u2nhd7ah

u2nhd7ah1#

代码不起作用,因为当您试图注解掉验证部分“if condition”时,忘记注解掉它的“else condition”。
我说的是这句话:

//if ($firstname == '' || $lastname == '' || $cell == '' || $address == '') {
irtuqstp

irtuqstp2#

它不起作用的原因是变量不一样,php是区分大小写的。e、 g lastname,而html是lastname。

$firstname = 
mysql_real_escape_string(htmlspecialchars($_REQUEST['FirstNameirstname']));
$lastname = mysql_real_escape_string(htmlspecialchars($_REQUEST['LastName']));
$cell = mysql_real_escape_string(htmlspecialchars($_REQUEST['Cell']));
$address = mysql_real_escape_string(htmlspecialchars($_REQUEST['Address']));
t40tm48m

t40tm48m3#

请使用以下功能:

$con->mysqli_real_escape_string ( $_POST['...'])

代替

mysql_real_escape_string(htmlspecialchars($_POST['...']))

相关问题