2011-08-09 8 views
-1

こんにちは、これは私のファイルで、ファイルをアップロードしてイメージフォルダに移動するなど、フォームからデータを取得しようとしています。フォームからデータを取得してdbに挿入しようとしています

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

その間に、エラーを投稿する参考になっだろうIあなたは、あなたがテーブルdogs_tbのすべての列の値を与える場合は、SQL文のこのタイプにのみ使用することができ、この

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

を使用して参照してください。あなたがユーザーを使用しているという事実を見ると、すべてのユーザーが自分のユーザーIDを持っていると思います。通常tablesetupに基づいて、最初のフィールドには、IDフィールドとなり、そしてそれは、ユーザIDは、常に最初にこの

"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

などのカラム名を設定する通常の方法ユーザーを0にしてみていることを信じることは難しいです

これは、youreテーブルを変更した場合でも、常に正しいフィールドを持っていることを確認します。

これ以外にも、挿入クエリを使用します。レコードを更新する場合は、UPDATEクエリが必要です。

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

+0

私はこれがphp headで最初に行われているのを見ています。ゼロの代わりに値。 – ashish

関連する問題