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

IGr*_*rds -3 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);
?>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

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>
Run Code Online (Sandbox Code Playgroud)

Fun*_*ner 6

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

没有实际数据出现

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

<input type="text" class="form-control" id="inputFirstName" placeholder="First Name">
Run Code Online (Sandbox Code Playgroud)

应该读作

<input type="text" name="inputFirstName" class="form-control" id="inputFirstName" placeholder="First Name">
                   ^^^^^^^^^^^^^^^^^^^^^
Run Code Online (Sandbox Code Playgroud)

然后像上面那样"命名"它们,为其他人做同样的事情.

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);
Run Code Online (Sandbox Code Playgroud)

这会给Undefined index你现在的代码带来许多警告.


另外,作为由Barmar指出的in his comment,你错过了一个$用于Picture= $_FILES["file"]["name"];除非这是一个笔误,应为已读:

$Picture= $_FILES["file"]["name"];
Run Code Online (Sandbox Code Playgroud)

关于它在数据库中注册为" 0000-00-00",请确保您的列具有正确的类型以容纳DATE.