php& mysql表单数据未插入数据库中

时间:2016-09-04 08:01:22

标签: php html mysql css

我正在尝试将表单数据插入到mysql中,但是当我输入详细信息并提交它时,我无法获取数据库中的数据。我可以知道我错在哪里吗?

<?php
// Same as error_reporting(E_ALL);
ini_set('error_reporting', E_ALL);

// Report all PHP errors (see changelog)
error_reporting(E_ALL);
 echo "jgj"
$servername = "localhost";
$username = "username";
$password = "";
$dbname = "sudhar";

// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
    die("Connection failed: " . $conn->connect_error);
} 
else
{
	echo "connected"
}
if(isset($_POST['submit']))
{
$Firstname=mysqli_real_escape_string($conn,$_POST['Firstname']);
$Lastname=mysqli_real_escape_string($conn,$_POST['Lastname']);
$Username=mysqli_real_escape_string($conn,$_POST['Username']);
$email=mysqli_real_escape_string($conn,$_POST['email']);
$Password=mysqli_real_escape_string($conn,$_POST['Password']);

$sql="insert into useraccount(Firstname,Lastname,Username,email,Password)values('$Firstname','$Lastname','$Username','$Password','$email')";
$result=mysqli_query($conn,$sql)
echo "data inserted"
or die(mysql_error());
}
   ?>
html,body
{
margin:0px;	
height:100%;
}
 .carousel-inner > .item > img,
  .carousel-inner > .item > a > img {
      width: 60%;
      margin: auto;
  }
 .content
 {
width:100%;
height:400px;
 }

 .signup
 {
height:500px;

 }
  .footer
  {
    position:relative;
  	background-color:black;
  }
 <!DOCTYPE html>
<html lang="en">
<head>
  
  <title>ASK</title>
  <meta charset="utf-8">
  <meta name="viewport" content="width=device-width, initial-scale=1">
  <link rel="stylesheet" type="text/css" href="boot.css">
  <link rel="stylesheet" href="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css">
  <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js"></script>
  <script src="http://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
</head>
<body>
<div class="jumbotron">
    <div class="container">
        <div class="row">
         
            <div class="col-md-6">
                <h1>school name</h1>                    
            </div>
            <div class="col-md-6">
                <img src="../project/photo/l.png"  height="150px"/>
            </div>
        </div>
      </div>
</div> 

<nav class="navbar navbar-inverse navbar-fixed-top">
  <div class="container-fluid">
    <div class="navbar-header">
      <button type="button" class="navbar-toggle" data-toggle="collapse" data-target="#myNavbar">
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
        <span class="icon-bar"></span>
      </button>
      <a class="navbar-brand" href="#mynavbar">schoolName</a>
    </div>
    <div class="collapse navbar-collapse" id="myNavbar">
      <ul class="nav navbar-nav">
        <li class="active"><a href="#">Home</a></li>
        <li><a href="about.html" target=_self>About Us</a></li>
        <li><a href="infra.html" target=_self>Infrastructure</a></li>
        <li class="dropdown">
          <a class="dropdown-toggle" data-toggle="dropdown" href="">Administration<span class="caret"></span></a>
           <ul class="dropdown-menu">
            <li><a href="stlogin.html">staff login</a></li>
            <li><a href="stdetails.html">staff details</a></li>
            <li><a href="class.html">class handling</a></li>
          </ul>
        </li>
        
      </ul>
      <ul class="nav navbar-nav navbar-right">
        <li><a href="signup.html"><span class="glyphicon glyphicon-user"></span> Sign Up</a></li>
        <li><a href="login.html"><span class="glyphicon glyphicon-log-in"></span> Login</a></li>
      </ul>
    </div>
  </div>
</nav>
<div class="container">
<div class="signup">
 <form method="post" class="form-horizontal" action="user.php">
   <div class="form-group">
    <div class="col-xs-3">
      <label for="Firstname">Firstname:</label>
      <input type="text" name="Firstname" class="form-control" id="fnmae" placeholder="Enter Firstname">
    </div></div>
    
    <div class="form-group">
    <div class="col-xs-3">
      <label for="Lastname">Lastname:</label>
      <input type="text" name="Lastname" class="form-control" id="lnmae" placeholder="Enter Lastname">
    </div></div>
    
    <div class="form-group">
    <div class="col-xs-3">
      <label for="Username">Username:</label>
      <input type="text" name="Username" class="form-control" id="unmae" placeholder="Enter Username">
    </div></div>

    <div class="form-group">
    <div class="col-xs-3">
      <label for="email">Email:</label>
      <input type="email" name="email" class="form-control" id="email" placeholder="Enter email">
    </div></div>        
    
    <div class="form-group">
    <div class="col-xs-3">
      <label for="pwd">Password:</label>
      <input type="Password" name="Password" class="form-control" id="pwd" placeholder="Enter password">
    </div></div>

    <button type="submit" class="btn btn-default">Submit</button><br>
    
  </form>
  </div>
  </div>
  <div class="footer nav">
  <p> Copyrights@ &copy; WWW.schools.com</p>
  </div>   
  </body>
  </html>

3 个答案:

答案 0 :(得分:1)

$query=mysqli_query($con,"insert into useraccount('Firstname','Lastname','Username','email','Password')values($Firstname,$Lastname,$Username,$email,$Password)");

并更改按钮名称

答案 1 :(得分:0)

如果不考虑其他错误,并且您只是猜测如何运行查询: 有一些保留的命名约定,也许你的代码可以在某些情况下工作,将列名放在引号之间。

{{1}}

我建议您在查询中使用的变量中转义字符。 这对您来说是一篇很有价值的文章:

http://php.net/manual/en/function.mysql-real-escape-string.php

但是:不要将此解决方案用于生产,因为在学习代码时使用登录凭证是最糟糕的方式。

答案 2 :(得分:0)

更改

<button type="submit" class="btn btn-default">Submit</button><br>

<button type="submit" name="submit class="btn btn-default">Submit</button><br>

因为忘记设置名称

,因此未设置$_POST['submit']