我的插入数据在PDO不会工作


My Insert data in PDO wont work

我想做的是,如果用户填写表单中的信息并单击提交按钮,它将数据存储在数据库中。我测试了一下,数据库连接没有问题。

我现在的问题是它不会保存在所有谁可以帮助我修复什么错误的代码?

index . php:

<?php include_once('header.php'); ?>
<html lang="en">
<head>
<script src="bootstrap/js/jquery-1.11.0.min.js"></script>
<style>
#Picture {
    width: 120px;
    height: 120px;
}
#imgInp { display:none;}
</style>
<script type="text/javascript">
$(document).ready(function() {
    var currentSrc = $('#Picture').attr('src');
    if(currentSrc==null || currentSrc==""){        
    $('#Picture').attr('src','http://i38.photobucket.com/albums/e149/eloginko/profile_male_large_zpseedb2954.jpg');
   $("#Picture").on('click', function() {
       $("#imgInp").trigger('click')}
   )}

    function readURL(input) {
        if (input.files && input.files[0]) {
            var reader = new FileReader();
            reader.onload = function (e) {
                $('#Picture').attr('src', e.target.result);
            }
            reader.readAsDataURL(input.files[0]);
        }
    }
    $("#imgInp").change(function(){
        readURL(this);
    });
});
</script>
</head>
<body>
<form method="post" action="index.php" enctype="multipart/form-data">   
<img id="Picture" name="Picture" data-src="#" /> <br />
<input type='file' id="imgInp" accept="image/*" /><br />
Name: <input type="text" id="name" name="name" /><br />
Age: <input type="text" id="age" name="age" /><br />
Address: <input type="text" id="address" name="address" /><br />
<input type="radio" name="gender" id="gender" value="male" />Male
<input type="radio" name="gender" id="gender" value="Female" />Female<br />
<input type="submit" name="add" value="Add" />
</form>
</body>
</html>

header。php:

<?php
$host = "localhost";
$user = "root";
$pass = "";
$db = "test";
$dbc = new PDO("mysql:host=" . $host . ";dbname=" . $db, $user, $pass);
if(isset($_POST['add'])){
    $name = isset($_POST['name']) ? $_POST['name'] : null;
    $age = isset($_POST['age']) ? $_POST['age'] : null;
    $address = isset($_POST['address']) ? $_POST['address'] : null;
    $gender = isset($_POST['gender']) ? $_POST['gender'] : null;
    $picture = isset($_FILES['Picture']) ? $_FILES['Picture'] : null;
    $q = "INSERT INTO students(name, age, address, gender, profile_pic) VALUES(:name, :age, :address, :gender, :Picture)";
    $query = $dbc->prepare($q);
    $query->bindParam(':name', $name);
    $query->bindParam(':age', $age);
    $query->bindParam(':address', $address);
    $query->bindParam(':gender', $gender);
    $query->bindParam(':Picture', $picture);
    $results = $query->execute();
}
?>

修改

<form method="post" action="index.php" enctype="multipart/form-data">   

to this -

<form method="post" action="header.php" enctype="multipart/form-data">