我想要做的是,如果用户填写表单中的信息并单击提交按钮,它将把数据存储在数据库中。在我测试它的数据库连接没问题。
我现在的问题是它根本不会保存,任何人都可以帮我解决代码错误吗?
的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();
}
?>
答案 0 :(得分:1)
改变这个 -
<form method="post" action="index.php" enctype="multipart/form-data">
到此 -
<form method="post" action="header.php" enctype="multipart/form-data">