嗨,这是我的代码,我试图从表单中获取数据,包括上传文件并将其移动到图像文件夹,如果成功,它应该显示消息。
<?php
session_start();
// If the session vars aren't set, try to set them with a cookie
if (!isset($_SESSION['user_id'])) {
if (isset($_COOKIE['user_id']) && isset($_COOKIE['username'])) {
$_SESSION['user_id'] = $_COOKIE['user_id'];
$_SESSION['username'] = $_COOKIE['username'];
}
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Create dogs page</title>
</head>
<body>
<?php
require_once('../../appvars.php');
require_once('../../connectvars.php');
// Make sure the user is logged in before going any further.
if (!isset($_SESSION['user_id'])) {
echo '<p class="login">Please <a href="../../login.php">log in</a> to access this page.</p>';
exit();
}
else {
echo('<p class="login">You are logged in as ' . $_SESSION['username'] . '. <a href="../../logout.php">Log out</a>.</p>');
}
// Connect to the database
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
if (isset($_POST['submit'])) {
// Grab the profile data from the POST
$breed_name = mysqli_real_escape_string($dbc, trim($_POST['breed_name']));
$page_title = mysqli_real_escape_string($dbc, trim($_POST['page_title']));
$description = mysqli_real_escape_string($dbc, trim($_POST['description']));
$history = mysqli_real_escape_string($dbc, trim($_POST['history']));
$dimension = mysqli_real_escape_string($dbc, trim($_POST['dimension']));
$health = mysqli_real_escape_string($dbc, trim($_POST['health']));
$maintainance = mysqli_real_escape_string($dbc, trim($_POST['maintainance']));
$living_conditions = mysqli_real_escape_string($dbc, trim($_POST['living_conditions']));
$life = mysqli_real_escape_string($dbc, trim($_POST['life']));
$litter_size = mysqli_real_escape_string($dbc, trim($_POST['litter_size']));
$File = $breed_name.".html";
$link = $File;
$picture = mysqli_real_escape_string($dbc, trim($_FILES['picture']['name']));
$picture_type = $_FILES['picture']['type'];
$picture_size = $_FILES['picture']['size'];
// Move the file to the target upload folder
$target = MM_UPLOADPATH . basename($picture);
/*if (move_uploaded_file($_FILES['picture']['tmp_name'], $target))
$error = false;
else {
// The new picture file move failed, so delete the temporary file and set the error flag
@unlink($_FILES['picture']['tmp_name']);
$error = true;
echo '<p class="error">Sorry, there was a problem uploading your picture.</p>';
}
*/
// Update the profile data in the database
if (!empty($breed_name) && !empty($page_title) && !empty($description) && !empty($history) && !empty($dimension) && !empty($health) && !empty($maintainance)&& !empty($living_conditions)&& !empty($life)&& !empty($litter_size)&& !empty($link) && !empty($picture)) {
$dbc = mysqli_connect(DB_HOST, DB_USER, DB_PASSWORD, DB_NAME);
$query = "INSERT INTO dogs_db (0,'$breed_name','$page_title','$description','$history','$dimension','$health','$maintainance','$living_conditions','$life','$litter_size','$picture','$link')";
mysqli_query($dbc, $query) or die('error querying');
// Confirm success with the user
echo '<p>Your profile has been successfully updated</p>';
mysqli_close($dbc);
exit();
}
else {
echo '<p class="error">You must enter all of the profile data .</p>';
}
}
// End of check for form submission
mysqli_close($dbc);
?>
<form enctype="multipart/form-data" method="post" action="adddogs.php">
<input type="hidden" name="MAX_FILE_SIZE" value="<?php echo MM_MAXFILESIZE; ?>" />
<fieldset>
<legend>Personal Information</legend>
<label for="breedname">Dog name:</label>
<input type="text" id="breed_name" name="breed_name" value="<?php if (!empty($breed_name)) echo $breed_name; ?>" /><br />
<label for="page title">Page title:</label>
<input type="text" id="title" name="page_title" value="<?php if (!empty($page_title)) echo $page_title; ?>" /><br />
<label for="description">Description:</label>
<input type="text" id="description" name="description" value="<?php if (!empty($description)) echo $description; ?>" /><br />
<label for="history">History:</label>
<input type="text" id="history" name="history" value="<?php if (!empty($history)) echo $history; ?>" /><br />
<label for="dimension">Dimension:</label>
<input type="text" id="dimension" name="dimension" value="<?php if (!empty($dimension)) echo $dimension; ?>" /><br />
<label for="health">Health:</label>
<input type="text" id="health" name="health" value="<?php if (!empty($health)) echo $health; ?>" /><br />
<label for="maintainance">Maintainance:</label>
<input type="text" id="maintainance" name="maintainance" value="<?php if (!empty($maintainance)) echo $maintainance; ?>" /><br />
<label for="living conditions">Living conditions:</label>
<input type="text" id="living conditions" name="living_conditions" value="<?php if (!empty($living_conditions)) echo $living_conditions; ?>" /><br />
<label for="life">Life:</label>
<input type="text" id="life" name="life" value="<?php if (!empty($life)) echo $life;?>" /><br />
<label for="Litter size">Litter Size:</label>
<input type="text" id="litter size" name="litter_size" value="<?php if (!empty($litter_size)) echo $litter_size; ?>" /><br />
<label for="picture">Picture:</label>
<input type="file" id="picture" name="picture" />
</fieldset>
<input type="submit" value="Save Profile" name="submit" />
</form>
</body>
</html>
答案 0 :(得分:0)
不是列出的问题,但我认为上传无效。这是因为该部分被注释掉了:
/*if (move_uploaded_file($_FILES['picture']['tmp_name'], $target))
$error = false;
else {
// The new picture file move failed, so delete the temporary file and set the error flag
@unlink($_FILES['picture']['tmp_name']);
$error = true;
echo '<p class="error">Sorry, there was a problem uploading your picture.</p>';
}
*/
从该块周围删除/ *和* /,您的上传应该能够正常运行
答案 1 :(得分:0)
发布错误会有所帮助,同时我看到你使用这个
"INSERT INTO dogs_db (0,'$breed_name','$page_title','$description','$history','$dimension','$health','$maintainance','$living_conditions','$life','$litter_size','$picture','$link')";
只有为表dogs_tb的所有列提供值时,才能使用此类型的SQl语句。考虑到您使用用户的事实,我认为每个用户都有自己的用户ID。基于正常的表设置,第一个字段将是ID字段,并且很难相信用户ID始终为0.尝试用户首先设置列名称的正常方法
"INSERT INTO dogs_tb (`field1_name`, `field2_name`, enz) VALUES ('field1_value', 'field2_vvalue')"
http://dev.mysql.com/doc/refman/5.1/en/insert.html
这确保您始终拥有正确的字段,即使您更改了表格。
除此之外,您还可以使用插入查询。如果您想更新记录,则需要UPDATE查询。