2014-06-19 50 views
-1

我想要做的是如果用戶填寫表單中的信息並單擊提交按鈕,它會將數據存儲在數據庫中。在我測試的數據庫連接沒有問題。我在PDO中插入數據不會工作

我現在的問題是不會保存在任何人都可以幫助我解決什麼是錯誤的代碼?

的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

是否有任何呃在你的瀏覽器的控制檯中漫遊?服務器端的error.log? –

+0

表單說'action =「index.php」',但插入腳本是'header.php'。 – Barmar

+0

這是您第三次發帖,其中2人被刪除了,爲什麼? –

回答

1

改變這一點 -

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

本 -

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

讓我們希望這將解決OP的問題。這是他/她發佈相同問題的第三次。 –

+0

header.php已包含在index.php中。這對我來說不是問題。 –

+1

@ThinkDifferent然後使用:'

」enctype =「multipart/form-data」>' – TiMESPLiNTER