Javascript 我在PDO中插入的数据无效
我想做的是,如果用户在表单中填写信息,然后单击submit按钮,它将在数据库中存储数据。数据库中的连接没有问题,我测试了它 我现在的问题是它根本无法保存。有人能帮我修复代码的错误吗 index.php:Javascript 我在PDO中插入的数据无效,javascript,php,jquery,html,css,Javascript,Php,Jquery,Html,Css,我想做的是,如果用户在表单中填写信息,然后单击submit按钮,它将在数据库中存储数据。数据库中的连接没有问题,我测试了它 我现在的问题是它根本无法保存。有人能帮我修复代码的错误吗 index.php: <?php include_once('header.php'); ?> <html lang="en"> <head> <script src="bootstrap/js/jquery-1.11.0.min.js"></script>
<?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>
#图画{
宽度:120px;
高度:120px;
}
#imgInp{显示:无;}
$(文档).ready(函数(){
var currentSrc=$('#Picture').attr('src');
如果(currentSrc==null | | currentSrc==“”){
$('#Picture').attr('src','http://i38.photobucket.com/albums/e149/eloginko/profile_male_large_zpseedb2954.jpg');
$(“#图片”)。在('click',function()上{
$(“#imgInp”).trigger('click')}
)}
函数readURL(输入){
if(input.files&&input.files[0]){
var reader=new FileReader();
reader.onload=函数(e){
$('#Picture').attr('src',e.target.result);
}
reader.readAsDataURL(input.files[0]);
}
}
$(“#imgInp”).change(函数(){
readURL(this);
});
});
名称:
年龄:
地址:
男性
女性
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">
为此—
<form method="post" action="header.php" enctype="multipart/form-data">
您的浏览器控制台中是否有任何错误?服务器端错误.log?表单上写着action=“index.php”
,但插入脚本是header.php
。这是您第三次发布此消息,其中两个被删除,为什么?添加$dbc->setAttribute(PDO::ATTR_ERRMODE,PDO::ERRMODE_异常)代码>在连接打开后立即执行。您没有检查错误并假设一切正常。如果您尝试在没有JS的情况下对代码进行故障排除,并将其作为纯PHP/SQL进行尝试,那么您将能够准确地指出代码的错误所在。希望这能解决OP的问题。这是他/她第三次发布相同的问题。index.php中已经包含header.php。这对我来说似乎不是问题。@ThinkDifferent然后使用:我看到了@ThinkDifferent,这就是为什么我早些时候问过他控制台和日志错误的原因。@TiMESPLiNTER您应该转义输出echo htmlspecialchars($_SERVER['PHP_SELF')代码>