【问题标题】:Trying to grab data from form and insert to db试图从表单中获取数据并插入到数据库
【发布时间】:2011-08-09 22:54:27
【问题描述】:

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

    <?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>

【问题讨论】:

    标签: php mysql file-upload upload


    【解决方案1】:

    没有列出真正的问题,但我认为上传不起作用。这是因为该部分被注释掉了:

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

    从该块周围删除 /* 和 */,您的上传应该可以正常工作

    【讨论】:

    • 即使没有删除 cmets 符号,上传也可以正常工作,问题是它给出了错误查询
    【解决方案2】:

    发布错误会很有帮助,同时我看到你在使用这个

    "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 查询。

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

    【讨论】:

    • 我已经看到这已经在 php head 中完成了,因为他们已经给出了,如果 id 是自动递增的,则输入零,它将自动采用适当的值而不是零。
    猜你喜欢
    • 2021-12-16
    • 2017-05-16
    • 2017-01-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-10
    • 2018-02-19
    • 1970-01-01
    相关资源
    最近更新 更多