开发者

Trying to grab data from form and insert to db

Hi this is my code where i m trying to grab data from form including uploading a file and move it to images folder and if sucessful it should show the message .

    <?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开发者_JS百科, 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>


Not really a question listed, but I assume that the upload isn't working. It is because that section is commented out:

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

Remove the /* and */ from around that block and your upload should function correctly


It would be helpfull to post the error, in the meantime I see your using this

"INSERT INTO dogs_db (0,'$breed_name','$page_title','$description','$history','$dimension','$health','$maintainance','$living_conditions','$life','$litter_size','$picture','$link')";

This type of SQl statement can only be used if you give a value for all the columns of the table dogs_tb. Looking to the fact that you uses users I suppose every user has a own user ID. Based on a normal tablesetup the first field would be the ID field, and it's hard to believe that the user ID is always 0. Try to user the normal way of setting the column names first like this

"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

This makes sure you've always got the right field, even if you change youre table.

Besides this all, you use a insert query. If you would like to update a record, you will need a UPDATE query.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜