2011-08-09 141 views
-1

嗨,这是我的代码,我试图从表单抓取数据,包括上传文件并将其移动到图像文件夹,如果成功,它应显示消息。尝试从表单中获取数据并插入到数据库

<?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>'; 
     } 
    */ 

删除/ *和* /从周围的块和上载应正常运行了

+0

上传工作,即使没有删除评论标志,问题是,它是给错误查询 – ashish

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。基于正常tablesetup第一场将是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

的正常方式

这可以确保您始终拥有正确的领域,即使您改变了您的表格。

除此之外,您使用插入查询。如果您想更新记录,则需要更新查询。

http://dev.mysql.com/doc/refman/5.1/en/update.html

+0

我已经看到这已经完成在PHP头,因为他们已经给出,通过将零如果ID是自动增量它会自动采取适当值而不是零。 – ashish

相关问题