引导表单不会将数据发布到 phpmyadmin 数据库中


Bootstrap form won't post data into phpmyadmin database

我的引导表单不会将数据发布到 phpmyadmin 数据库中,我单击提交,没有提交任何内容,我已经包含了配置文件和我的表单文件。配置文件适用于注册表单,所以我认为它与我的表单有关。我的网站在托管器上运行,在 www.grademanager.16mb.com.没有弹出错误消息,当您按提交页面重新加载时。

更新 : 我已将 sql 语句更改为 pdo 语句,但它只会将前两个语句、学生姓名和第一个主题插入我的 php 我的管理数据库。它不会插入其他两个语句。

另外,如果数据已成功提交,我如何确保它只进入感谢.html文件?

<?php
ob_start();
session_start();
//set timezone
date_default_timezone_set('Europe/London');
//database credentials
define('DBHOST','mysql.hostinger.co.uk');
define('DBUSER','u717042829_mac');
define('DBPASS','********');
define('DBNAME','u717042829_db1');
//application address
define('DIR','http://grademanager.16mb.com/');
define('SITEEMAIL','noreply@grademanager.comlu.com');
try {
    //create PDO connection 
    $db = new PDO("mysql:host=".DBHOST.";port=3306;dbname=".DBNAME, DBUSER, DBPASS);
    $db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
    //show error
    echo '<p class="bg-danger">'.$e->getMessage().'</p>';
    exit;
}
//include the user class, pass in the database connection
include('classes/user.php');
$user = new User($db); 
?>

<?php 
error_reporting(E_ALL); ini_set('display_errors', 1);
require("includes/config.php");
//if not logged in redirect to login page
if(!$user->is_logged_in()){ header('Location: login.php'); } 
//define page title
$title = 'Register Past Student';
?>
<html>
 
<head>
    <meta charset="utf-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="description" content="">
    <meta name="author" content="">
    <title>Past Student</title>
    <!-- Bootstrap Core CSS -->
    <link href="css/bootstrap.min.css" rel="stylesheet">
  
    <!-- Custom Fonts -->
    <link href="font-awesome/css/font-awesome.min.css" rel="stylesheet" type="text/css">
    <!-- HTML5 Shim and Respond.js IE8 support of HTML5 elements and media queries -->
    <!-- WARNING: Respond.js doesn't work if you view the page via file:// -->
    <!--[if lt IE 9]>
        <script src="https://oss.maxcdn.com/libs/html5shiv/3.7.0/html5shiv.js"></script>
        <script src="https://oss.maxcdn.com/libs/respond.js/1.4.2/respond.min.js"></script>
    <![endif]-->
</head>
<form class="form-horizontal" method="post" action= "">
<fieldset>

<!-- Form Name -->
<legend> <a href= "memberpage.php"> Register A Past Student </a> </legend>

<!-- Text input-->
<div class="form-group">
  <label class="col-md-4 control-label" for="name">Student Name</label>  
  <div class="col-md-4">
  <input id="name" name="name" type="text" placeholder="First and Last Name" class="form-control input-md" required="">
    
  </div>
</div>
  <div class="form-group">  
  <label class="col-md-4 control-label" for="subject1">Subject 1</label>
  <div class="col-md-4">
    <select id="subject1" name="subject1" class="form-control">
      <option value="Chemistry">Chemistry</option>
      <option value="Computing">Computing</option>
      <option value="Economics">Economics</option>
      <option value="Further Maths">Further Maths</option>
      <option value="Physics">Physics</option>
    </select>
  </div>
</div>
<!-- Select Basic -->
<div class="form-group">
  <label class="col-md-4 control-label" for="grade1">Grade 1</label>
  <div class="col-md-4">
    <select id="grade1" name="grade1" class="form-control">
      <option value="A">A</option>
      <option value="B">B</option>
      <option value="C">C</option>
      <option value="D">D</option>
      <option value="E">E</option>
      <option value="U">U</option>
    </select>
  </div>
</div>
<!-- Select Basic -->
<div class="form-group">
  <label class="col-md-4 control-label" for="subject2">Subject 2</label>
  <div class="col-md-4">
    <select id="subject2" name="subject2" class="form-control">
      <option value="Chemistry">Chemistry</option>
      <option value="Computing">Computing</option>
      <option value="Economics">Economics</option>
      <option value="Further Maths">Further Maths</option>
      <option value="Physics">Physics</option>
    </select>
  </div>
</div>
<!-- Select Basic -->
<div class="form-group">
  <label class="col-md-4 control-label" for="grade2">Grade 2</label>
  <div class="col-md-4">
    <select id="grade2" name="grade2" class="form-control">
      <option value="A">A</option>
      <option value="B">B</option>
      <option value="C">C</option>
      <option value="D">D</option>
      <option value="E">E</option>
      <option value="U">U</option>
    </select>
  </div>
</div>
<!-- Select Basic -->
<div class="form-group">
  <label class="col-md-4 control-label" for="subject3">Subject 3</label>
  <div class="col-md-4">
    <select id="subject3" name="subject3" class="form-control">
      <option value="Chemistry">Chemistry</option>
      <option value="Computing">Computing</option>
      <option value="Economics">Economics</option>
      <option value="Further Maths">Further Maths</option>
      <option value="Physics">Physics</option>
    </select>
  </div>
</div>
<!-- Select Basic -->
<div class="form-group">
  <label class="col-md-4 control-label" for="grade3">Grade 3</label>
  <div class="col-md-4">
    <select id="grade3" name="grade3" class="form-control">
      <option value="A">A</option>
      <option value="B">B</option>
      <option value="C">C</option>
      <option value="D">D</option>
      <option value="E">E</option>
      <option value="U">U</option>
    </select>
  </div>
</div>
<!-- Select Basic -->
<div class="form-group">
  <label class="col-md-4 control-label" for="subject4">Subject 4</label>
  <div class="col-md-4">
    <select id="subject3" name="subject4" class="form-control">
      <option value="Chemistry">Chemistry</option>
      <option value="Computing">Computing</option>
      <option value="Economics">Economics</option>
      <option value="Further Maths">Further Maths</option>
      <option value="Physics">Physics</option>
    </select>
  </div>
</div>
<div class="form-group">
  <label class="col-md-4 control-label" for="grade4">Grade 4</label>
  <div class="col-md-4">
    <select id="grade1" name="grade4" class="form-control">
      <option value="A">A</option>
      <option value="B">B</option>
      <option value="C">C</option>
      <option value="D">D</option>
      <option value="E">E</option>
      <option value="U">U</option>
    </select>
  </div>
</div>
<!-- Multiple Radios (inline) -->
<div class="form-group">
  <label class="col-md-4 control-label" for="attendanc">Attendance %</label>
  <div class="col-md-4"> 
    <label class="radio-inline" for="attendanc-0">
      <input type="radio" name="attendanc" id="attendanc-0" value="99-100" checked="checked">
      99-100
    </label> 
    <label class="radio-inline" for="attendanc-1">
      <input type="radio" name="attendanc" id="attendanc-1" value="97-98">
      97-98
    </label> 
    <label class="radio-inline" for="attendanc-2">
      <input type="radio" name="attendanc" id="attendanc-2" value="95-96">
      95-96
    </label> 
    <label class="radio-inline" for="attendanc-3">
      <input type="radio" name="attendanc" id="attendanc-3" value="">
      93-94
    </label> 
    <label class="radio-inline" for="attendanc-4">
      <input type="radio" name="attendanc" id="attendanc-4" value="">
      91-92
    </label> 
    <label class="radio-inline" for="attendanc-5">
      <input type="radio" name="attendanc" id="attendanc-5" value="">
      90 or less
    </label>
  </div>
</div>
<!-- Multiple Radios -->
<div class="form-group">
  <label class="col-md-4 control-label" for="gender">Gender</label>
  <div class="col-md-4">
  <div class="radio">
    <label for="gender-0">
      <input type="radio" name="gender" id="gender-0" value="Male" checked="checked">
      Male
    </label>
  </div>
  <div class="radio">
    <label for="gender-1">
      <input type="radio" name="gender" id="gender-1" value="Female">
      Female
    </label>
  </div>
  </div>
</div>
<!-- Button -->
<div class="form-group">
  <label class="col-md-4 control-label" id="submit">Click Submit</label>
  <div class="col-md-4">
    <button id="singlebutton" type="submit" name="submit" class="btn btn-success">Submit</button>
  </div>
</div>
</fieldset>
</form>
<?php 
//require("includes/config.php");
if (isset($_POST['name'])) { //checks if the name has posted value then inserts the data
      $name     = $_POST['name'];
      $subject1 = $_POST['subject1'];
      $grade1   = $_POST['grade1']; 
      $subject2 = $_POST['grade2'];
      $q        = "INSERT INTO paststudent (name, subject1,grade1,subject2) VALUES (:name, :subject1,:grade1,:subject2);"; //sql statement add a colon intead of variables so pdo can prepare the statement and prevent any sql injections 
      $query    = $db ->prepare ($q);
      $results  = $query ->execute (array(  
                ":name" => $name,  //points to the variable 
                ":subject1" => $subject1,
                ":grade1"   => $grade1,
                ":subject2" => $subject2
      )); 
              header ('location:thanks.html');
            }
  
?>
</html>

错误 1:

你错过了type="submit"

 <button id="singlebutton"  type="submit" name="submit" class="btn btn-success">Submit</button

错误 2:

您正在混合所有mysql,mysqli和PDO扩展。这就是为什么你 无法插入数据库。

在连接文件中,您已使用 PDO。在插入时,您编写了 mysqli,但您的语法似乎是针对 mysql 的。

错误 3:

插入查询语法不正确。

$sql = "INSERT INTO paststudent (name,subject1,grade,subject2,grade2,subject2,grade2,subject3,grade3,subject4,grade4,attendance,gender) VALUES ('$_POST[name]','$_POST[subject1]','$_POST[grade1]','$_POST[subject2]','$_POST[grade2]','$_POST[subject3]','$_POST[grade3]','$_POST[subject4]','$_POST[grade4]','$_POST[attendance]','$_POST[gender]')";
  • 你已经给出了 13 个库姆和 11 个值......???
  • 为什么要按原样插入值..???
  • 你为什么不消毒它???
  • 你知道mysql_injections???吗

不确定这会有什么效果,但我看到 2 个结束表单标签

  • 在参数和值之前输入空格
  • 使用mysql_query
<?php
     require("includes/config.php");
if(isset($_POST['submit'])) {
    $sql = "INSERT INTO paststudent (name, subject1, grade, subject2, grade2, subject2, grade2, subject3, grade3, subject4, grade4, attendance ,gender) VALUES ('$_POST[name]', '$_POST[subject1]', '$_POST[grade1]', '$_POST[subject2]', '$_POST[grade2]', '$_POST[subject3]', '$_POST[grade3]', '$_POST[subject4]', '$_POST[grade4]', '$_POST[attendance]', '$_POST[gender]')";

    if (mysql_query($sql)) {
        echo "succesfully submitted to the database";
    } else {
        echo "Well this is embarising";
    }

}
?>
  • 如果不起作用,请手动执行插入查询到数据库并检查错误