dousha4804 2013-04-16 06:47
浏览 23

如何使用图像更新文本表单

how to update text form with image

im trying to update text with image now this code working fine with text and also image update perfectly

but image is not showing in edit from

example edit form

  • Name:demo
  • Father Name:demo
  • address: demo

    Photo - chose file- i want here image name like photo.jpg

image is also there in database but not showing here in edit form

how can i do this please tell me to fix this issue thanks

this is form code

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

and this is php code

<?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!';
 }
 }
?>
  • 写回答

2条回答 默认 最新

  • douduoyan5943 2013-04-16 07:17
    关注

    I'm not goint to adapt all your code. I think that with these lines you've enought to underestand it! Check it out and tell to us! :)

    FORM

    <?
    // 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()); 
            }
        }
    }
    
    ?>
    
    评论

报告相同问题?

悬赏问题

  • ¥15 基于单片机的靶位控制系统
  • ¥15 AT89C51控制8位八段数码管显示时钟。
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 下图接收小电路,谁知道原理
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错