如何使用php更新操作获取数据?

时间:2015-06-30 09:18:38

标签: javascript php mysql crud insert-update

我编写了一个更新操作,但是我的代码遇到了问题。当我单击编辑按钮更新数据时,无法从数据库中获取数据,并且表单的字段为空。你能帮我修一下如何使用更新操作在表单字段中获取数据吗?

这是我的代码

<?php
    require 'database.php';
    $id = null;
    if ( !empty($_GET['id'])) {
        $id = $_REQUEST['id'];
    }

    if ( null==$id ) {
        header("Location: index.php");
    }

    if ( !empty($_POST)) {
        // keep track post values
        $file_name = $_POST['file_name'];
$ref_no = $_POST['ref_no'];
$to_name = $_POST['to_name'];
$confidential = $_POST['confidential'];
$designation = $_POST['designation'];
$date = $_POST['date'];
$solutation = $_POST['solutation'];
$entity = $_POST['entity'];
$add_1 = $_POST['add_1'];
$thank_you = $_POST['thank_you'];
$add_2 = $_POST['add_2'];
$yours_truly = $_POST['yours_truly'];
$add_3 = $_POST['add_3'];
$sign_name = $_POST['sign_name'];
$city = $_POST['city'];
$s_designation = $_POST['s_designation'];
$heading_line_1 = $_POST['heading_line_1'];
$encl_line_1 = $_POST['encl_line_1'];
$heading_line_2 = $_POST['heading_line_2'];
$encl_line_2 = $_POST['encl_line_2'];
$heading_line_3 = $_POST['heading_line_3'];
$encl_line_3 = $_POST['encl_line_3'];
//$text = mysql_real_escape_string( $_POST['text'] );


    // update data
    $valid = true;
        if ($valid) {
            $pdo = Database::connect();
            $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
            $sql = "UPDATE test  set file_name = ?, ref_no = ?, to_name =?
                                        confidential = ?, designation = ?, date =?
                                        solutation = ?, entity = ?, add_1 =?
                                        thank_you = ?, add_2 = ?, yours_truly =?
                                        add_3 = ?, sign_name = ?, city =?
                                        s_designation = ?, heading_line_1 = ?, encl_line_1 =?
                                        heading_line_2 = ?, encl_line_2 = ?, heading_line_3 =?
                                        encl_line_3 = ?, text = ?
                                                                          WHERE id = ?";
            $q = $pdo->prepare($sql);
            $q->execute(array($file_name,$ref_no,$to_name,$confidential,$designation,$date,$solutation,$entity,$add_1,
            $thank_you,$add_2,$yours_truly,$add_3,$sign_name,$city,$s_designation,$heading_line_1,$encl_line_1,$heading_line_2,
            $encl_line_2,$heading_line_3,$encl_line_3,$id));
            Database::disconnect();
            header("Location: index.php");
        }
     else {
       $pdo = Database::connect();
        $pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
        $sql = "SELECT * FROM test where id = ?";
        $q = $pdo->prepare($sql);
        $q->execute(array($id));
        $data = $q->fetch(PDO::FETCH_ASSOC);
        $file_name = $_POST['file_name'];
$ref_no = $_POST['ref_no'];
$to_name = $_POST['to_name'];
$confidential = $_POST['confidential'];
$designation = $_POST['designation'];
$date = $_POST['date'];
$solutation = $_POST['solutation'];
$entity = $_POST['entity'];
$add_1 = $_POST['add_1'];
$thank_you = $_POST['thank_you'];
$add_2 = $_POST['add_2'];
$yours_truly = $_POST['yours_truly'];
$add_3 = $_POST['add_3'];
$sign_name = $_POST['sign_name'];
$city = $_POST['city'];
$s_designation = $_POST['s_designation'];
$heading_line_1 = $_POST['heading_line_1'];
$encl_line_1 = $_POST['encl_line_1'];
$heading_line_2 = $_POST['heading_line_2'];
$encl_line_2 = $_POST['encl_line_2'];
$heading_line_3 = $_POST['heading_line_3'];
$encl_line_3 = $_POST['encl_line_3'];
//$text = mysql_real_escape_string( $_POST['text'] );

        Database::disconnect();
    }
    }
?>

<!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=utf-8" />
<title>Untitled Document</title>
<link rel="stylesheet" type="text/css" href="newstyles.css">
    <script src="js/bootstrap.min.js"></script>
</head>

<body>
<form action="edit.php?id=<?php echo $id?>" method="POST" > 
 <table border="0" class="DivTableBorder" width="840px">
      <tr>
        <td class="DivSubHeaderCellTop"  colspan="6">Letters</td>
</tr> <tr>  </td>  </tr>
  <tr>
   <td class="DivCellText" width="80px">File Name </td>
    <td class="DivCellText" width="480px" colspan="3"><input name="file_name" type="text" id="file_name" 
    value="<?php echo !empty($file_name)?$file_name:'';?>" class="inputRemarks"  />
    </td>
      <td class="DivCellText" width="80px">Referance #</td>
    <td class="DivCellText" width="200px"><input name="ref_no" type="text" id="ref_no" 
    value="<?php echo !empty($ref_no)?$ref_no:'';?>" class="inputRemarks"  />
    </td> </tr>

<tr ><td bgcolor="#999999" colspan="4"></td>
 <td class="DivCellText" width="80px"></td>
    <td class="DivCellText" width="200px"></td>
</tr>
  <tr>
   <td class="DivCellText" width="80px">To - Name</td>
    <td class="DivCellText" colspan="3"><input name="to_name" type="text" id="to_name" 
    value="<?php echo !empty($to_name)?$to_name:'';?>" class="inputRemarks"  />
    </td>
<td class="DivCellText" width="80px">Confidential</td>
    <td class="DivCellText" width="200px">
    <input name="confidential" type="checkbox" id="confidential" value="<?php echo !empty($confidential)?$confidential:'';?>"  />
    </td> </tr>
  <tr>
   <td class="DivCellText" width="80px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Designation</td>
    <td class="DivCellText" colspan="3"><input name="designation" type="text" id="designation" 
    value="<?php echo !empty($designation)?$designation:'';?>" class="inputRemarks"  />
    </td>
     <td class="DivCellText" width="80px">Date :</td>
    <td class="DivCellText" width="200px">
            <input name="date" type="date" id="date" maxlength="255" size="19" onclick="event.cancelBubble=true;this.select();lcs(this);" 
        value=18/06/2015 /> 
    </td> </tr>

  <tr>
   <td class="DivCellText" >&nbsp;</td>
    <td class="DivCellText" colspan="3">&nbsp;</td>
    <td class="DivCellText" width="80px">Solutation</td>
    <td class="DivCellText" width="200px" >

    <select name='solutation' id='solutation' size='1' STYLE='width: 95%' value="<?php echo !empty($solutation)?$solutation:'';?>" >
    <option value='Others' >[--Others--]</option>
    <option value='Dear Sir' >Dear Sir</option>
    <option value='Madam' >Madam</option>
    </select>
    </td> </tr>

  <tr>
   <td class="DivCellText" width="80px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Entity</td>
    <td class="DivCellText" colspan="3"><input name="entity" type="text" id="entity" 
    value="<?php echo !empty($entity)?$entity:'';?>" class="inputRemarks"  />
    </td>
    <td class="DivCellText" width="80px">&nbsp;</td>
    <td class="DivCellText" width="200px" >

    <input name="txtSolutation" type="text" id="txtSolutation" 
    value="" class="inputRemarks"  />
    </td> </tr>

  <tr>
   <td class="DivCellText" width="80px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Add-1</td>
    <td class="DivCellText" colspan="3"><input name="add_1" type="text" id="add_1" 
    value="<?php echo !empty($add_1)?$add_1:'';?>" class="inputRemarks"  />
    </td>
         <td class="DivCellText" width="80px">Thank You.</td>
    <td class="DivCellText" width="200px" ><input name="thank_you" type="text" id="thank_you" 
    value="<?php echo !empty($thank_you)?$thank_you:'';?>" class="inputRemarks"  />
    </td> 
  </tr>
  <tr>
   <td class="DivCellText" width="80px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Add-2</td>
    <td class="DivCellText" colspan="3"><input name="add_2" type="text" id="add_2" 
    value="<?php echo !empty($add_2)?$add_2:'';?>" class="inputRemarks"  />
    </td>
         <td class="DivCellText" width="80px">Yours truly</td>
         <td class="DivCellText" width="200px" >
    <select name='yours_truly' id='yours_truly' size='1' STYLE='width: 95%' value="<?php echo !empty($yours_truly)?$yours_truly:'';?>" >
    <option value='1' >Yours truly</option>
    <option value='2' >Regards</option>
    </select>

    </td> </tr>
  <tr>
   <td class="DivCellText" width="80px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Add-3</td>
    <td class="DivCellText" colspan="3"><input name="add_3" type="text" id="add_3" 
    value="<?php echo !empty($add_3)?$add_3:'';?>" class="inputRemarks"  />
    </td>
    <td class="DivCellText" width="80px">Signature-Name</td>
    <td class="DivCellText" width="200px" >

    <select name='sign_name'  id='sign_name' size='1' style='width:95%' value="<?php echo !empty($sign_name)?$sign_name:'';?>"> 
        <option value='1' >Adnan Afaq</option>
        <option value='2' >Muhammad Shahzad Saleem</option>
        <option value='3' >Adnan Dilawar</option>
        <option value='4' >Rana Muhammad Nadeem</option>
        <option value='5' >Jhangeer Hanif</option>
    </select>
    </td> </tr>
  <tr>
   <td class="DivCellText" width="80px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;City</td>
    <td class="DivCellText" colspan="3"><input name="city" type="text" id="city" 
    value="<?php echo !empty($city)?$city:'';?>" class="inputRemarks"  />
    </td>
    <td class="DivCellText" width="80px">S-Designation</td>
    <td class="DivCellText" width="200px">
    <select name='s_designation' id='s_designation' size='1' STYLE='width: 95%' value="<?php echo !empty($s_designation)?$s_designation:'';?>" >
    <option value='1' >Managing Director</option>
    <option value='2' >Chief Operating Officer</option>
    <option value='3' >Manager Ratings</option>
    <option value='4' >Unit Head Ratings</option>
    </select>
    </td>
  </tr>
  <tr>
   <td class="DivCellText" width="80px">Heading Line-1</td>
    <td class="DivCellText" width="480px" colspan="3"><input name="heading_line_1" type="text" id="heading_line_1" 
    value="<?php echo !empty($heading_line_1)?$heading_line_1:'';?>" class="inputRemarks"  maxlength="55"/>
    </td>
     <td class="DivCellText" width="80px">Encl: Line-1</td>
     <td class="DivCellText" width="200px" >
     <input name="encl_line_1" type="text" id="encl_line_1" value="<?php echo !empty($encl_line_1)?$encl_line_1:'';?>" class="inputRemarks"  />
    </td>
  </tr>  
  <tr>
   <td class="DivCellText" width="80px">Heading Line-2</td>
    <td class="DivCellText" width="480px" colspan="3"><input name="heading_line_2" type="text" id="heading_line_2" 
    value="<?php echo !empty($heading_line_2)?$heading_line_2:'';?>" class="inputRemarks"    maxlength="55" />
    </td>
     <td class="DivCellText" width="80px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Line-2</td>
     <td class="DivCellText" width="200px" >
     <input name="encl_line_2" type="text" id="encl_line_2" value="<?php echo !empty($encl_line_2)?$encl_line_2:'';?>" class="inputRemarks"  />
  </tr>  
  <tr>
   <td class="DivCellText" width="80px">Heading Line-3</td>
    <td class="DivCellText" width="480px" colspan="3"><input name="heading_line_3" type="text" id="heading_line_3" 
    value="<?php echo !empty($heading_line_3)?$heading_line_3:'';?>" class="inputRemarks"    maxlength="55" />
    </td>
     <td class="DivCellText" width="80px">&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Line-3</td>
    <td class="DivCellText" width="200px">
         <input name="encl_line_3" type="text" id="encl_line_3" value="<?php echo !empty($encl_line_3)?$encl_line_3:'';?>" class="inputRemarks"  />
    </td>
  </tr>
<tr ><td bgcolor="#999999" colspan="6"></td></tr>
<tr ><td colspan="6">
 <table  border="0" class="DivTableBorder" width="840px">
 <tr>
<td class="DivCellText" colspan="4">
<textarea rows="20" cols="100" id="text" name="text" style="font-size:14px;" value="<?php echo !empty($text)?$text:'';?>" ></textarea> </td>
 </tr>
<tr>
<td width="734">

</td>
<td  colspan="6" ><input type="reset" name="reset" value="Cancel"  /> </td>
<td width="44" colspan="6">

  <input type="submit" name="submit" value="Save"></input>
</td>
</tr>

</table>
<div class="form-actions">
                          <button type="submit" class="btn btn-success">Update</button>
                          <a class="btn" href="index.php">Back</a>
                        </div>

</form>
</body>
</html>

0 个答案:

没有答案