0

こんにちはみんな私は学生の詳細を更新するstudent_edit.phpファイルがあるプロジェクトに取り組んでいますすべてのデータは正常に更新されましたが、更新時に1つの問題があり、すべてではなく2つのフィールドしか言わず、画像が空白になり、フィールドが空白になります更新成功。

私がやっていることは、学生の写真を見せていることです。それに加えて、画像を閲覧して画像を更新するための別の入力フィールドがあります。私が欲しいのは、代わりに画像を更新していない場合、他のフィールドの画像を更新しても、空白にならないようにすることです。

必要なコード スニペットは次のようになります。

<?php

    $file_name = $_FILES['picture']['name'];
    $tmp_name  = $_FILES['picture']['tmp_name'];

    if (copy($tmp_name, "images/" . $file_name)) {
        $picture = "images/" . $file_name;
    }

    if (!isset($_POST['picture'])) {
        $res = mysql_query("UPDATE student SET `id`='$id',`branch_id`='$branch_id',`class_id`='$class_id',`section_id`='$section_id',`roll_number`='$roll_number',`student_name`='$student_name',`father_name`='$father_name',`dob`='$dob',`student_address`='$student_address',`gender`='$gender',`status`='$status',updated=now() WHERE id='$id'") or die(mysql_error());
    }
    else {
        $res = mysql_query("UPDATE student SET `id`='$id',`branch_id`='$branch_id',`class_id`='$class_id',`section_id`='$section_id',`roll_number`='$roll_number',`student_name`='$student_name',`father_name`='$father_name',`dob`='$dob',`student_address`='$student_address',`gender`='$gender',`status`='$status',`picture`='$picture',updated=now() WHERE id='$id'") or die(mysql_error());
    }

?>

そして、それらは私がpicsと呼ぶ画像領域です

<p>
    <label for="picture"><strong>Picture:</strong> </label>
    <a href="#"><img src="<?php echo $rec['picture'];?>" width="100" height="100"/></a>
    <input name="picture" type="file" value="">

</p>

そして、それらは私がpicsと呼ぶ画像領域です

<p>
    <label for="picture"><strong>Picture:</strong> </label>
    <a href="#"><img src="<?php echo $rec['picture'];?>" width="100" height="100"/></a>
    <input name="picture" type="file" value="">

</p>
4

1 に答える 1

0

コードを見て簡単に推測する$pictureと、データベースに配置したときに の値をエスケープしていないため、データベースの画像の値が更新されていない可能性があります。

また、使用しないif(!isset($_POST['picture']))で、試してみてくださいif (!isset($_FILES['picture']) || (isset($_FILES['picture']) && $_FILES['picture']['name'] == "")。ファイルはスーパー グローバル経由で送信されるため、$_POSTスーパー グローバルは機能しません$_FILES

すべてを一緒に入れて:

$file_name = $_FILES['picture']['name'];
$tmp_name = $_FILES['picture']['tmp_name'];

if (!isset($_FILES['picture']) || (isset($_FILES['picture']) && $_FILES['picture']['name'] == "")) {
  $res = mysql_query("UPDATE student SET `id`='$id',`branch_id`='$branch_id',`class_id`='$class_id',`section_id`='$section_id',`roll_number`='$roll_number',`student_name`='$student_name',`father_name`='$father_name',`dob`='$dob',`student_address`='$student_address',`gender`='$gender',`status`='$status',`picture`='$picture',updated=now() WHERE id='$id'") or die(mysql_error());
} else {
  copy($tmp_name, "images/" . $file_name)

  $picture = mysql_real_escape_string("images/".$file_name);
  $res = mysql_query("UPDATE student SET `id`='$id',`branch_id`='$branch_id',`class_id`='$class_id',`section_id`='$section_id',`roll_number`='$roll_number',`student_name`='$student_name',`father_name`='$father_name',`dob`='$dob',`student_address`='$student_address',`gender`='$gender',`status`='$status',updated=now() WHERE id='$id'") or die(mysql_error());
}

また、MySQLi 拡張機能を使用することを強くお勧めします: http://php.net/manual/en/book.mysqli.php

上記のコードは完全にテストされていませんが、動作するはずです。

于 2012-07-21T20:41:51.470 に答える