保存到mysql数据库时如何获取正确的数据?

时间:2014-09-11 04:28:18

标签: php mysql

我有一个似乎连接到mysql数据库的表单。当我在表单中输入信息并提交时,它会在数据库中注册为" 0000-00-00"并将数据作为" 0000-00-00"返回。没有实际数据出现。任何想法?

connection.php:

<?php
$dbhost = 'localhost';
$dbuser = 'root';
$dbpass = 'root';
$db = 'sm_residents';

$conn = mysql_connect($dbhost,$dbuser,$dbpass);
mysql_select_db($db);
?>

Create.php:

<?php
include ('connection.php');

$FirstName= $_POST['inputFirstName'];
$LastName= $_POST['inputLastName'];
$Address= $_POST['inputAddress'];
$Birthday= $_POST['inputBirthday'];
$FormerResidence= $_POST['inputFormerResidence'];
$Career= $_POST['inputCareer'];
$Education= $_POST['inputEducation'];
$SpecialInterests= $_POST['inputSpecialInterests'];

if ($_FILES["file"]["error"] > 0) {
} else {
    if (file_exists("upload/" . $_FILES["file"]["name"])) {
    } else {
        move_uploaded_file($_FILES["file"]["tmp_name"],
                           "upload/" . $_FILES["file"]["name"]);
    }
}

Picture= $_FILES["file"]["name"];

mysql_query("INSERT INTO residents             (`ID`,`FirstName`,`LastName`,`Address`,`Birthday`,`FormerResidence`,`Career`,`Education`,`SpecialInterests`,`Picture`)
                         VALUES(NULL,'$FirstName','$LastName','$Address','$Birthday','$FormerResidence','$Career','$Education','$SpecialInterests','$Picture')") or die(mysql_error());

?>
<script> window.location = "index.php"; </script>

的index.php:

<?php
 include ('connection.php');

if(isset($_POST['submit'])) {
    echo "Please Fill Out The Form";
    //header ('Location: create.php');
} else {
    //echo "User Has Been Added";
    //header('Location: create.php');
}
?>
<h1>Add A Resident</h1>
    <form action="create.php" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="inputFirstName">First Name</label>
<input type="text" class="form-control" id="inputFirstName" placeholder="First Name">
</div>
<div class="form-group">
<label for="inputLastName">Last Name</label>
<input type="text" class="form-control" id="inputLastName" placeholder="Last Name">
</div>
<div class="form-group">
<label for="inputAddress">Address</label>
<input type="text" class="form-control" id="inputAddress" placeholder="Address">
</div>
<div class="form-group">
<label for="inputBirthday">Birthday</label>
<input type="date" class="form-control" id="inputBirthday">
</div>
<div class="form-group">
<label for="inputFormerResidence">Former Residence</label>
<input type="text" class="form-control" id="inputFormerResidence" placeholder="Former        Residence">
</div>
<div class="form-group">
<label for="inputCareer">Career</label>
<input type="text" class="form-control" id="inputCareer" placeholder="Career">
</div>
<div class="form-group">
<label for="inputEducation">Education</label>
<input type="text" class="form-control" id="inputEducation" placeholder="Education">
</div>
<div class="form-group">
<label for="inputSpecialInterests">Special Interests</label>
<input type="text" class="form-control" id="inputSpecialInterests" placeholder="Special Interests">
</div>
<div class="form-group">
<label for="inputFile">File input</label>
<input type="file" id="inputFile">
</div>
<button type="submit" class="btn btn-default">Submit</button>
</form>

2 个答案:

答案 0 :(得分:6)

我会给出一个答案:(或者最好是90% / 95%部分)

  

没有实际数据显示

输入中甚至没有任何命名元素,因此没有任何内容可以通过,它们都是所有ID。

<input type="text" class="form-control" id="inputFirstName" placeholder="First Name">

应该读作

<input type="text" name="inputFirstName" class="form-control" id="inputFirstName" placeholder="First Name">
                   ^^^^^^^^^^^^^^^^^^^^^

然后通过&#34;命名&#34;为其他人做同样的事情。就像我上面那样。

PHP正在寻找命名元素,而不是无法依赖的ID。

这也没有名称<input type="file" id="inputFile">

更改为:<input type="file" name="file" id="inputFile">

根据$_FILES["file"]


将以下内容放在您的开场<?php标记下方:

error_reporting(E_ALL);
ini_set('display_errors', 1);

将使用您现有的代码抛出许多Undefined index警告。


正如Barmar in his comment指出的那样,你$错过了Picture= $_FILES["file"]["name"];,除非这是一个错字,它应该是:

$Picture= $_FILES["file"]["name"];

关于,它在数据库中注册为&#34; 0000-00-00&#34; 确保您的列具有正确的类型以容纳DATE。

答案 1 :(得分:1)

首先尝试格式化您的出生日期:

 $Birthday = date("Y-m-d", strtotime($_POST['inputBirthday']));