插入数据库

时间:2015-12-11 23:52:05

标签: php html mysql css

嘿,我正在努力让这段代码在过去几天运行。我不知道是什么问题。每当我运行代码时,我都可以看到它正在运行,但是会插入一个空行。基本上我试图硬编码数据并插入数据。这是HTML表单:

                        <form action="register.php" id="contactForm" type="post">
                            <div class="row">
                                <div class="form-group">
                                    <div class="col-md-6">
                                        <label>First name *</label>
                                        <input type="text" class="form-control" name="fname" >
                                    </div>
                                    <div class="col-md-6">
                                        <label>Last name *</label>
                                        <input type="text" class="form-control" name="lname" >
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="form-group">
                                    <div class="col-md-6">
                                        <label>Gender *</label><br>
                                        <select name="gender">
                                            <option> Male </option>
                                            <option> Female </option>
                                        </select>

                                    </div>

                                    <div class="col-md-6">
                                        <label>Stream *</label><br>
                                        <select name="stream">
                                            <option> B-Tech </option>
                                            <option> M-Tech </option>
                                        </select>

                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="form-group">
                                    <div class="col-md-6">
                                        <label>Email *</label>
                                        <input type="text"  class="form-control" name="email" >
                                    </div>
                                    <div class="col-md-6">
                                        <label>Mobile *</label>
                                        <input type="text"  class="form-control" name="mobile">
                                    </div>
                                </div>
                            </div>
                            <div class="row">
                                <div class="form-group">
                                    <div class="col-md-6">
                                        <label>College *</label>
                                        <input type="text"  class="form-control" name="college" >
                                    </div>
                                    <div class="col-md-6">
                                        <label>Job Kind *</label>
                                        <input type="text" class="form-control" name="job" >
                                    </div>

                                </div>
                            </div>
                            <div class="row">
                                <div class="col-md-12">
                                    &nbsp&nbsp&nbsp&nbsp

                                    <input type="submit" value="Register" class="btn btn-primary btn-lg" 
                                    data-loading-text="Loading..." name="submit">

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

这是registration.php

<?php
$connection = mysql_connect("EDITED by billy, was an I.P and port number", "user", "password"); // Establishing Connection with Server
$db = mysql_select_db("Registrations_connect", $connection); // Selecting Database from Server

            $first_name = $_POST["fname"];
            $last_name = $_POST["lname"];
            $sex = $_POST["gender"];
            $field = $_POST["stream"];
            $contact = $_POST["mobile"];
            $eaddress = $_POST["email"];
            $institute = $_POST["college"];
            $naukri = $_POST["job"];

            $query = mysql_query("insert into students(fname, lname, gender, stream, mobile, email, college, job) 
            values ('$name', '$last_name', '$sex', '$field','$contact', '$eaddress', '$intitute', '$naukri')");

            echo "<br/><br/><span>Data Inserted successfully...!!</span>";


mysql_close($connection); // Closing Connection with Server
?>

跑步后;在inspect元素中,我检查了响应: - 它显示Data Inserted successfully,但实际上插入了一个空行。基本上我认为我无法从表单中正确地获取数据。有人可以检查一下是什么问题。这将是一个很大的帮助。

1 个答案:

答案 0 :(得分:1)

该属性为method,而非type。此拼写错误导致您的表单处理GET而不是POST。所以你的所有变量赋值都是错误的。

$first_name = $_POST["fname"];

将是

$first_name = $_GET["fname"];

或者您可以使用$_REQUEST;或者你可以纠正属性,

<form action="register.php" id="contactForm" method="post">

您的代码也对SQL注入持开放态度,并使用已弃用的mysql_函数。您应该更新到mysqlipdo并使用带参数化查询的预准备语句。

有关SQL注入的更多信息:
http://php.net/manual/en/security.database.sql-injection.php {
{3}} {
{3}}