我正在尝试将通过表单发送的信息存储到 MySQL 数据库中。它不加载下一页也不存储信息。
我的标头需要 init.php,我可以确认它具有正确的数据库连接凭据。
以下是我的 HTML 和“上传器”脚本 - 我知道它一定很愚蠢,但这就是问题所在,在 html 和/或 uploader.php 中。
如果有人可以运行我的代码并指出每个问题(并且可能是我的代码的返工),那将非常感激!谢谢!!
HTML (我减少了出生日期选项,所以这里的代码更少)
<h2>Choose Your File</h2>
<form id="submit-photo" action="index.php?p=uploader" enctype="multipart/form-data" method="POST">
<input type="hidden" name="MAX_FILE_SIZE" value="5242880" />
<div id="FileUpload">
<input type="file" name="photo" id="BrowserHidden" onchange="getElementById('FileField').value = getElementById('BrowserHidden').value;" />
<div id="BrowserVisible"><input type="text" id="FileField" /></div>
<span class="error"><?php if(isset($_SESSION['flash_message']['photo'])) echo $_SESSION['flash_message']['photo'] ?>
</span></div>
<fieldset>
<label for="name">Name</label>
<input type="text" name="name" id="name">
</fieldset>
<fieldset>
<label for="dob">DOB</label>
<div class="dob-select">
<select name="dob_day" id="dob_day">
<option value="01">01</option>
<option value="02">02</option>
<option value="03">03</option>
<option value="04">04</option>
<option value="05">05</option>
</select>
</div>
<div class="dob-select">
<select name="dob_month" id="dob_month">
<option value="01">Jan</option>
<option value="02">Feb</option>
<option value="03">Mar</option>
<option value="04">Apr</option>
</select>
</div>
<div class="dob-select">
<select name="dob_year" id="dob_year">
<option value="2012">2012</option>
<option value="2011">2011</option>
<option value="2010">2010</option>
</select>
</div>
</fieldset>
<fieldset>
<label for="postcode">Postcode</label>
<input type="text" class="short" name="postcode" id="postcode">
</fieldset>
<fieldset>
<label for="email">Email</label>
<input type="email" name="email" id="email">
</fieldset>
<fieldset>
<label for="subscribe"><input type="checkbox" class="left" id="subscribe"> <p class="left">subscribe</p></label>
<input type="submit" name="submit">
</fieldset>
</form>
数据库列
- id (自动递增)
- 姓名
- 照片(文件路径)
- 电子邮件
- 日期(出生日期:日、月、年组合起来形成这个)
- 邮政编码
- 订阅(应该是 0 或 1)
- 批准
- 创建(时间戳)
上传器 PHP
<?php $error = array();
require_once 'init.php';
//Is request?
if(strtolower($_SERVER['REQUEST_METHOD']) == 'post') {
//$friend = ( $_POST['friend'] == 'true' ) ? 1 : 0;
$required_array = array(
'name' => 'Name',
'dob_day' => 'Day',
'dob_month' => 'Month',
'dob_year' => 'Year',
'postcode' => 'Postcode',
'email' => 'Email Address',
'subscribe' => 'subscribe'
);
$required_error = array();
foreach( $required_array as $field_name => $field ) {
if(!isset($_POST[$field_name]) OR empty($_POST[$field_name]) OR $_POST[$field_name] == '') {
$required_error[$field_name] = 'Please insert your '. $field;
}
}
$_POST['email'] = verify_email($_POST['email']);
if($_POST['email'] == FALSE && !isset($error['email']))
$error['email'] = 'Please use a valid email address';
//Validate the form key
if(!isset($_POST['form_key']) || !$formKey->validate()) {
//Form key is invalid, show an error
$error['general'] = 'Use the real form!';
} else {
if((!empty($_FILES["photo"])) && ($_FILES['photo']['error'] == 0)) {
$filename = basename($_FILES['photo']['name']);
$ext = substr($filename, strrpos($filename, '.') + 1);
//Check if the file is JPEG image and it's size is less than 1Mb
if ( ($ext == "jpg") && ($_FILES["photo"]["type"] == "image/jpeg") && ($_FILES["photo"]["size"] <= 5242880) ) {
//Determine the path to which we want to save this file
$newname = str_replace( ' ', '_', trim( strip_tags( $_POST['name'] ) ) ) . _ . $formKey->generateKey() . '_' . time() . '.jpg';
//Check if the file with the same name is already exists on the server
if (!file_exists($newname)) {
if (sizeof($required_error) == 0) {
//Attempt to move the uploaded file to it's new place
if ((move_uploaded_file($_FILES['photo']['tmp_name'], './photos/'. $newname))) {
$move_status = 'done';
} else {
$error['photo'] = "A problem occurred during file upload!";
}
}
} else {
$error['photo'] = "File ".$_FILES["photo"]["name"]." already exists";
}
} else {
$error['photo'] = "Only .jpg images under 5Mb are accepted for upload". $_FILES["photo"]["size"] . $_FILES["photo"]["type"] . '====' . $ext;
}
} else {
$error['photo'] = "No photo uploaded";
}
}
$error = $error + $required_error;
if (sizeof($error) == 0 AND $move_status == 'done') {
$_POST['date'] = $_POST['dob_day'].'-'.$_POST['dob_month'].'-'.$_POST['dob_year'];
$query = sprintf("INSERT INTO `$db_name`.`submissionform` (`id` , `name` , `photo` , `email` , `date` , `postcode` , `subscribe` , `approve` , `created` )
VALUES ( NULL , '%s', '%s', '%s', '%s', '%s', '%s', '0', CURRENT_TIMESTAMP );",
mysql_real_escape_string($_POST['name']),
mysql_real_escape_string($newname),
mysql_real_escape_string($_POST['email']),
mysql_real_escape_string($_POST['date']),
mysql_real_escape_string($_POST['postcode']),
mysql_real_escape_string($_POST['subscribe']),
mysql_real_escape_string($_POST['approve']),
mysql_real_escape_string($_POST['message'])
);
mysql_query('SET AUTOCOMMIT=0');
$result1 = mysql_query($query);
$last_id = mysql_insert_id();
if ($result1)
$success = 'Done';
else
$error['general'] = 'Error when submitting your form, please try again.';
//mysql_free_result($result);
mysql_close();
}
}
if ($success == 'Done') {
$page = 'uploader';
include 'header.php';
echo '<img height="782" style="float:left;" src="./assets/img/success.png" />';
include 'footer.php';
} else {
$_SESSION['flash_message'] = $error;
$_SESSION['recent_field'] = $_POST;
header('Location: ./index.php');
}
?>
编辑:我做了一些调试——放在ini_set('display_errors', 'On'); error_reporting(E_ALL);
uploader.php的顶部
错误是:
注意:未定义的变量:成功在 ..../uploader.php 第 100 行
警告:无法修改标头信息 - 标头已由第 110 行 ....uploader.php 中的(输出开始于 ..../uploader.php:100)发送