0

如何用图像更新文本表单

我正在尝试用图像更新文本,现在这段代码可以很好地处理文本,也可以完美地更新图像

但图像未在编辑中显示

示例编辑表单

  • 名称:演示
  • 父亲姓名:demo
  • 地址:演示

    照片 - 选择文件 - 我想要这里的图像名称,如 photo.jpg

图像也存在于数据库中,但未在此处以编辑形式显示

我该怎么做请告诉我解决这个问题谢谢

这是表单代码

<form action="" method="post" enctype="multipart/form-data">
     <input type="hidden" name="id" value="<?php echo $id; ?>"/>
     <div>
     <p><strong>ID:</strong> <?php echo $id; ?></p>
    <div>
       <p><span class="style9"><strong>Teacher No:</strong></span><strong>  *</strong>
         <input name="teacherno" type="text" value="<?php echo $teacherno; ?>" size="50" />
       </p>
       <p><span class="style9"><strong>Teacher Name:</strong></span><strong>  *</strong>
         <input name="name" type="text" value="<?php echo $name; ?>" size="50" />
       </p>
       <p><span class="style9"><strong>Education:</strong></span><strong>  *</strong>
         <input name="education" type="text" value="<?php echo $education; ?>" size="50" />
       </p>
       <p><span class="style9"><strong>Father Name:</strong></span><strong>  *</strong>
         <input name="fathername" type="text" value="<?php echo $fathername; ?>" size="50" />
       </p>
          <p><span class="style9"><strong>Salary:</strong></span><strong>  *</strong>
         <input name="salary" type="text" value="<?php echo $salary; ?>" size="50" />
        </p>
        <p><span class="style9"><strong>Date Of Birth :</strong></span><strong>  *</strong>
          <input name="age" type="text" value="<?php echo $age; ?>" size="50" />
       </p>
          <p><span class="style9"><strong>Teach Class :</strong></span><strong>  *</strong>
            <input name="classteacher" type="text" value="<?php echo $classteacher; ?>" size="50" />
       </p>
       <p><span class="style9"><strong>Phone No :</strong></span><strong>  *</strong>
         <input name="phone" type="text" value="<?php echo $phone; ?>"/>
       </p>
       <p><span class="style9"><strong>Date Of Join :</strong></span><strong>  *</strong>
         <input id="fullDate" name="dateofjoin" type="text" value="<?php echo $dateofjoin; ?>" size="50" />
       </p>
        <p><span class="style9"><strong>Home Address :</strong></span><strong>  *</strong>
           <textarea name="address" cols="50"><?php echo $address; ?></textarea>
       </p>
        <p><span class="style9"><strong>N.I.C No:</strong></span><strong>  *</strong>
          <input name="nic" type="text" value="<?php echo $nic; ?>" size="50" />
       </p>
        <span class="style9"><strong>Branch:</strong></span><strong> *</strong>
        <input name="branch" type="text" value="<?php echo $branch; ?>" size="50">
    <span class="style9"><strong>Photo:</strong></span><strong> *</strong>
                 <input type="hidden" name="size" value="<?php echo $photo; ?>" size="50">
                <input type="file" name="photo"> 
        <br/>
       <p class="style1">* required</p>
     <input type="submit" name="submit" value="Submit">
     </div>
     </form> 

这是php代码

<?php
 }

 //This is the directory where images will be saved
$target = "images/";
$target = $target . basename( $_FILES['photo']['name']);

//This gets all the other information from the form

$photo=($_FILES['photo']['name']);
//Writes the photo to the server
if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
{

//Tells you if its all ok
echo "The file ". basename( $_FILES['uploadedfile']['name']). " has been uploaded, and your information has been added to the directory";
}


 // connect to the database
 include('connect-db.php');

 // check if the form has been submitted. If it has, process the form and save it to the database
 if (isset($_POST['submit']))
 { 
 // confirm that the 'id' value is a valid integer before getting the form data
 if (is_numeric($_POST['id']))
 {
 // get form data, making sure it is valid
 $id = $_POST['id'];
 $teacherno = mysql_real_escape_string(htmlspecialchars($_POST['teacherno']));
 $name = mysql_real_escape_string(htmlspecialchars($_POST['name']));
 $education = mysql_real_escape_string(htmlspecialchars($_POST['education']));
 $salary = mysql_real_escape_string(htmlspecialchars($_POST['salary']));
 $classteacher = mysql_real_escape_string(htmlspecialchars($_POST['classteacher']));
 $dateofjoin = mysql_real_escape_string(htmlspecialchars($_POST['dateofjoin']));
 $nic = mysql_real_escape_string(htmlspecialchars($_POST['nic']));
 $address = mysql_real_escape_string(htmlspecialchars($_POST['address']));
 $age = mysql_real_escape_string(htmlspecialchars($_POST['age']));
 $fathername = mysql_real_escape_string(htmlspecialchars($_POST['fathername']));
 $phone = mysql_real_escape_string(htmlspecialchars($_POST['phone']));
 $branch = mysql_real_escape_string(htmlspecialchars($_POST['branch']));
 $photo = mysql_real_escape_string(htmlspecialchars($_FILES['photo']['name']));

 // check that firstname/lastname fields are both filled in
 if ($teacherno == '' || $name == '')
 {
 // generate error message
 $error = 'ERROR: Please fill in all required fields!';

 //error, display form
 renderForm($id, $teacherno, $name, $education, $salary, $classteacher, $dateofjoin, $nic, $address, $age, $fathername, $phone, $branch, $photo, $error);
 }
 else
 {
 // save the data to the database
 mysql_query("UPDATE teacher SET teacherno='$teacherno', name='$name', education='$education', salary='$salary', classteacher='$classteacher', dateofjoin='$dateofjoin', nic='$nic', address='$address', age='$age', phone='$phone', branch='$branch', photo='$photo' WHERE id='$id'")
 or die(mysql_error()); 



 // once saved, redirect back to the view page
 header("Location: view.php"); 
 }
 }
 else
 {
 // if the 'id' isn't valid, display an error
 echo 'Error!';
 }
 }
 else
 // if the form hasn't been submitted, get the data from the db and display the form
 {

 // get the 'id' value from the URL (if it exists), making sure that it is valid (checing that it is numeric/larger than 0)
 if (isset($_GET['id']) && is_numeric($_GET['id']) && $_GET['id'] > 0)
 {
 // query db
 $id = $_GET['id'];
 $result = mysql_query("SELECT * FROM teacher WHERE id=$id")
 or die(mysql_error()); 
 $row = mysql_fetch_array($result);

 // check that the 'id' matches up with a row in the databse
 if($row)
 {

 // get data from db
 $teacherno = $row['teacherno'];
 $name = $row['name']; 
 $education = $row['education']; 
 $salary = $row['salary']; 
 $classteacher = $row['classteacher']; 
 $dateofjoin = $row['dateofjoin']; 
 $nic = $row['nic']; 
 $address = $row['address']; 
 $age = $row['age']; 
 $fathername = $row['fathername']; 
 $phone = $row['phone']; 
 $branch = $row['branch']; 
 $photo = $row['photo']; 


 // show form
 renderForm($id, $teacherno, $name, $education, $salary, $classteacher, $dateofjoin, $nic, $address, $age, $fathername, $phone, $branch, $photo, '');
 }
 else
 // if no match, display result
 {
 echo "No results!";
 }
 }
 else
 // if the 'id' in the URL isn't valid, or if there is no 'id' value, display an error
 {
 echo 'Error!';
 }
 }
?>
4

2 回答 2

0

我不会调整你所有的代码。我认为,有了这些台词,您就足以理解它了!查看并告诉我们!:)

形式

<?
// You've to pass a parameter to get if is a form for edit teacher or for new teacher
$edit = $_POST['edit'];
?>
<form action="" method="post" enctype="multipart/form-data">
    <input type="hidden" name="id" value="<?php echo $id; ?>"/>
    <input type="hidden" name="edit" value="<? $edit ?>"/>

    <div>
        <p><strong>ID:</strong> <?php echo $id; ?></p>
    <div>
    <p>
        <span class="style9"><strong>Teacher No:</strong></span><strong>  *</strong>
        <input name="teacherno" type="text" value="<?php echo $teacherno; ?>" size="50" />
    </p>
    <p>
        <span class="style9"><strong>Teacher Name:</strong></span><strong>  *</strong>
        <input name="name" type="text" value="<?php echo $name; ?>" size="50" />
    </p>
    <p>
        <span class="style9"><strong>Education:</strong></span><strong>  *</strong>
        <input name="education" type="text" value="<?php echo $education; ?>" size="50" />
    </p>
    <p>
        <span class="style9"><strong>Father Name:</strong></span><strong>  *</strong>
        <input name="fathername" type="text" value="<?php echo $fathername; ?>" size="50" />
    </p>
    <p>
        <span class="style9"><strong>Salary:</strong></span><strong>  *</strong>
        <input name="salary" type="text" value="<?php echo $salary; ?>" size="50" />
    </p>
    <p>
        <span class="style9"><strong>Date Of Birth :</strong></span><strong>  *</strong>
        <input name="age" type="text" value="<?php echo $age; ?>" size="50" />
    </p>
    <p>
        <span class="style9"><strong>Teach Class :</strong></span><strong>  *</strong>
        <input name="classteacher" type="text" value="<?php echo $classteacher; ?>" size="50" />
    </p>
    <p>
        <span class="style9"><strong>Phone No :</strong></span><strong>  *</strong>
        <input name="phone" type="text" value="<?php echo $phone; ?>"/>
    </p>
    <p>
        <span class="style9"><strong>Date Of Join :</strong></span><strong>  *</strong>
        <input id="fullDate" name="dateofjoin" type="text" value="<?php echo $dateofjoin; ?>" size="50" />
    </p>
    <p>
        <span class="style9"><strong>Home Address :</strong></span><strong>  *</strong>
        <textarea name="address" cols="50"><?php echo $address; ?></textarea>
    </p>
    <p>
        <span class="style9"><strong>N.I.C No:</strong></span><strong>  *</strong>
        <input name="nic" type="text" value="<?php echo $nic; ?>" size="50" />
    </p>
    <span class="style9"><strong>Branch:</strong></span><strong> *</strong>
    <input name="branch" type="text" value="<?php echo $branch; ?>" size="50">

    <span class="style9"><strong>Photo:</strong></span><strong> *</strong>
    <?
    if ($edit) {
        // This will show a image that we have on the ddbb
        ?><img src="$path_images.$img" /><?
    }
    ?>
    <input type="file" name="photo" /> 

    <br/>
    <p class="style1">* required</p>

    <input type="submit" name="submit" value="Submit">
    </div>
</form> 

PHP

// connect to the database
include('connect-db.php');

//This is the directory where images will be saved
$target = "images/";


// check if the form has been submitted. If it has, process the form and save it to the database
if (isset($_POST['submit']))
{ 
    // confirm that the 'id' value is a valid integer before getting the form data
    if (is_numeric($_POST['id']))
    {
        // get form data, making sure it is valid
        $id = $_POST['id'];
        $teacherno = mysql_real_escape_string(htmlspecialchars($_POST['teacherno']));
        $name = mysql_real_escape_string(htmlspecialchars($_POST['name']));
        $education = mysql_real_escape_string(htmlspecialchars($_POST['education']));
        $salary = mysql_real_escape_string(htmlspecialchars($_POST['salary']));
        $classteacher = mysql_real_escape_string(htmlspecialchars($_POST['classteacher']));
        $dateofjoin = mysql_real_escape_string(htmlspecialchars($_POST['dateofjoin']));
        $nic = mysql_real_escape_string(htmlspecialchars($_POST['nic']));
        $address = mysql_real_escape_string(htmlspecialchars($_POST['address']));
        $age = mysql_real_escape_string(htmlspecialchars($_POST['age']));
        $fathername = mysql_real_escape_string(htmlspecialchars($_POST['fathername']));
        $phone = mysql_real_escape_string(htmlspecialchars($_POST['phone']));
        $branch = mysql_real_escape_string(htmlspecialchars($_POST['branch']));
        $photo = mysql_real_escape_string(htmlspecialchars($_FILES['photo']['name']));

        // check that firstname/lastname fields are both filled in
        if ($teacherno == '' || $name == '')
        {
        // generate error message
        $error = 'ERROR: Please fill in all required fields!';

        //error, display form
        renderForm($id, $teacherno, $name, $education, $salary, $classteacher, $dateofjoin, $nic, $address, $age, $fathername, $phone, $branch, $photo, $error);
    }else{
         // save the data to the database
         mysql_query("UPDATE teacher SET teacherno='$teacherno', name='$name', education='$education', salary='$salary', classteacher='$classteacher', dateofjoin='$dateofjoin', nic='$nic', address='$address', age='$age', phone='$phone', branch='$branch', photo='$photo' WHERE id='$id'")
         or die(mysql_error()); 
         // once saved, redirect back to the view page
         header("Location: view.php"); 
     }
}else{
    // if the 'id' isn't valid, display an error
    echo 'Error!';
}


if ($_POST['edit'])
{
    if (($_FILES["img"]["error"] != 0) or if($_FILES["img"]["error"] != 4))
    {
        // we update everyting without the image!!
        mysql_query("UPDATE teacher SET teacherno='$teacherno', name='$name', education='$education', salary='$salary', classteacher='$classteacher', dateofjoin='$dateofjoin', nic='$nic', address='$address', age='$age', phone='$phone', branch='$branch' WHERE id='$id'") or die(mysql_error()); 

    }else{
        // we update everyting without the image!!
        $target = $target . basename( $_FILES['photo']['name']);
        $photo = ($_FILES['photo']['name']);
        if(move_uploaded_file($_FILES['photo']['tmp_name'], $target))
        {   
             mysql_query("UPDATE teacher SET teacherno='$teacherno', name='$name', education='$education', salary='$salary', classteacher='$classteacher', dateofjoin='$dateofjoin', nic='$nic', address='$address', age='$age', phone='$phone', branch='$branch', photo='$photo' WHERE id='$id'")
            or die(mysql_error()); 
        }
    }
}

?>
于 2013-04-16T07:17:58.407 回答
0

只需尝试以下操作:

<p>
   <span class="style9"><strong>Photo:</strong></span><strong> *</strong>
   <input type="hidden" name="size" value="<?php echo $photo; ?>" size="50">
   <input type="file" name="photo"><img src="users_photo/<?php ($photo ? $photo : 'default.jpg');?>" border="0"> 
</p>   

我认为这可以帮助您解决问题。

于 2013-04-16T07:24:09.510 回答