2015-02-11 178 views
1

我是新来的PHP。我已经创建了一个更新表单,如下所示。我需要更新以下字段。更新后提交事务

分类,简短描述,详细描述。 这部分发生,即如果我仅更新类别字段,则其余字段将变为空白。那么该怎么做呢?任何帮助,将不胜感激。 第1步。在view.php当用户点击编辑按钮,它会去updateview.php

步骤2:在updateview.php当用户更改任何字段值,然后按更新按钮它会去update.php

步骤3.从update.php它会返回到带有更新值的view.php

感谢

View.php

<table id="example" class="row-border" cellspacing="0" width="100%"> 
     <thead> 
      <tr> 
       <th>SRN</th> 
       <th>Client</th> 
       <th>Category</th> 
       <th>Short Description</th> 
       <th>Full Description</th> 
       <th>Action</th> 
      </tr> 
     </thead> 
     <tbody> 
     <?php while($row = mysql_fetch_array($selectQ)){ ?> 
     <tr> 
      <td><?php echo $row['srn'];?></td> 
      <td><?php echo $row['client'];?></td> 
      <td><?php echo $row['category'];?></td> 
      <td><?php echo $row['sd'];?></td> 
      <td><?php echo $row['fd'];?></td> 
      <td><a href="updateview.php?srn=<?php echo $row['srn']; ?>" target="_blank">Edit</a></td> 
    </tr> 
     <?php } ?> 
     </tbody> 
    </table> 

dbconn.php

<?php 
$username = "root"; 
$password = "root"; 
$hostname = "localhost"; 
$dbhandle = mysql_connect($hostname, $username, $password) 
    or die("Unable to connect to MySQL"); 
$selected = mysql_select_db("eservice",$dbhandle) 
    or die("Could not select database"); 
?> 

updateview.php

<!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> 
<div id="main-content"> 
<fieldset> 
<?php 
if(isset($_SESSION['example'])) 
{ 
echo $_SESSION['example']; 
} 
else 
{ 
echo "Session destroyed.."; 
} 
?> 
</div> 
<?php 
include_once('dbconn.php'); 
$srn = $_GET['srn']; 
$selQ = "Select * from main where srn = '".$srn."'"; 
$selectQ = mysql_query($selQ); 
?> 
<?php 
     while($row = mysql_fetch_array($selectQ)){ ?> 
<form action="update.php" method="post" enctype="multipart/form-data" novalidate> 
<div class="item"> 
    <label> <span>SRN</span> 
<input name="srn" type="text" id="srn" size="15" readonly="readonly" maxlength="40" value="<?php echo $row['srn']; ?>"/> 
    </label> 
    </div> 
    <div class="item"> 
    <label> <span>Client</span> 
    <select class="required" name="client" value="<?php echo $row['client']; ?>" disabled="disabled"/> 
          <?php include_once('dbconn.php'); ?> 
        <option value=""><?php echo $row['client']; ?></option> 
      <?php 
mysql_connect ("localhost","root",""); 
        mysql_select_db ("eservice"); 
        $select="eservice"; 
        if (isset ($select)&&$select!="") 
{ 
         $select=$_POST ['NEW']; 
} 
?> 
<?php 
        $list=mysql_query("select * from client"); 
        while($row_list=mysql_fetch_assoc($list)) 
{ 
?> 
      <?php $ct = $row_list['cname'];?> 
      <option value="<?php echo $ct; ?>"<?php if($ct==$select){ echo "selected"; } ?> > <?php echo $ct; ?></option> 
      <?php } ?> 
     </select> 
    <input type="hidden" name="client" value = "<?php echo $row['client']; ?>" /> 
    </label> 
</div> 
    <div class="item"> 
    <label> <span>Category</span> 
     <select class="required" name="category" value="<?php echo $row['category']; ?>"/> 
          <?php include_once('dbconn.php'); ?> 
        <option value=""><?php echo $row['category']; ?></option> 
      <?php 
mysql_connect ("localhost","root",""); 
        mysql_select_db ("eservice"); 
        $select="eservice"; 
        if (isset ($select)&&$select!="") 
{ 
         $select=$_POST ['NEW']; 
} 
?> 
      <?php 
        $list=mysql_query("select * from category"); 
        while($row_list=mysql_fetch_assoc($list)) 
} 
?> 
      <?php $ct = $row_list['name'];?> 
      <option value="<?php echo $ct; ?>"<?php if($ct==$select){ echo "selected"; } ?> > <?php echo $ct; ?></option> 
      <?php } ?> 
    </select> 
    </label> 
</div> 
<div class="item"> 
<label> <span>Short Description</span> 
    <textarea required="required" name='sd'><?php echo $row['sd']; ?></textarea> 
</div> 
<div class="item"> 
<label> <span>Full Description</span> 
    <textarea required="required" name='fd'><?php echo $row['fd']; ?></textarea> 
</div> 
<div class="item"> 
<button id='cancel' type='cancel'>Cancel</button> 
<button id='send' type='submit'>Update</button> 
</div> 
</form> 
<?php } ?> 

update.php

<?php 
include_once('dbconn.php'); 
$srn   = $_POST['srn']; 
$client  = $_POST['client']; //required 
$cate   = $_POST['category']; 
$sd   = $_POST['sd']; //required 
$fd   = $_POST['fd']; //required 

$updQry = "Update main Set client = '".$client."',category = '".$cate."',sd= '".$sd."',fd= '".$fd."' where srn = '".$srn."'"; 
$updateQ = mysql_query($updQry); 
header("Location: view.php?res=U"); 
?> 
+0

不要使用'mysql_ *'功能,这些API已被弃用。你还应该使用准备好的语句。 – Jens 2015-02-11 06:42:24

+0

如何使用准备好的语句? – Kiran 2015-02-11 06:48:47

+0

阅读[手册](http://php.net/manual/de/mysqli.quickstart.prepared-statements.php) – Jens 2015-02-11 06:59:06

回答

0

注:

确保您srn柱是独一无二的。 您再次调用updateview.php中的数据库连接,include及其所有代码。

include_once('dbconn.php'); 

mysql_connect ("localhost","root",""); 
        mysql_select_db ("eservice"); 

其中,您dbconn.php用户名和密码既是根,但你的updateview.php内,用户名为root,但没有密码指示。你可以在while循环中调用它。

我认为,您的更新查询没有问题,它只是使用旧的弃用mysql_*函数,它很容易SQL injections。您应该使用mysqli_* prepared statementPDO

建议:

我再做你的代码更值得推荐mysqli_* prepared statement。耐心理解,但很容易。

你dbconn.php:

<?php 

$mysqli = new mysqli("localhost", "root", "root", "eservice"); 

/* ESTABLISH CONNECTION */ 
if (mysqli_connect_errno()) { 
    printf("Connect failed: %s\n", mysqli_connect_error()); 
    exit(); 
} 
?> 

你的UpdateView。PHP

<form action="update.php" method="post" enctype="multipart/form-data" novalidate> 

<?php 
include_once('dbconn.php'); 
$srn = $_GET['srn']; 

    if($stmt = $mysqli->prepare("SELECT srn, client, category, sd, fd FROM main WHERE srn=?")){ 

    $stmt->bind_param("s",$_GET["srn"]); 
    $stmt->execute(); 
    $stmt->bind_result($srn,$client,$category,$sd,$fd); 
    $stmt->fetch(); 
    $stmt->close(); 

    } 

    ?> 

<div class="item"> 
    <label> <span>SRN</span> 
    <input name="srn" type="text" id="srn" size="15" readonly="readonly" maxlength="40" value="<?php echo $srn; ?>"/> 
    </label> 
</div> 

<div class="item"> 
    <label> <span>Client</span>  
    <select class="required" name="client"/> 
    <?php 

    if($stmt = $mysqli->prepare("SELECT cname FROM client")){ 

     $stmt->execute(); 
     $stmt->bind_result($cname); 

     while($stmt->fetch()){ 

     ?> 
      <option value="<?php echo $cname; ?>" <?php if($cname==$client){ echo "selected"; } ?>> <?php echo $cname; ?> </option> 
     <?php 

     } /* END OF WHILE LOOP */ 

     $stmt->close(); 

    } /* END OF PREPARED STATEMENT OF CLIENT */ 

    ?> 
    </select> 
    </label> 
</div> 

<div class="item"> 
    <label> <span>Category</span>  
    <select class="required" name="category"/> 
    <?php 

    if($stmt = $mysqli->prepare("SELECT name FROM category")){ 

     $stmt->execute(); 
     $stmt->bind_result($name); 

     while($stmt->fetch()){ 

     ?> 
      <option value="<?php echo $name; ?>" <?php if($name==$category){ echo "selected"; } ?>> <?php echo $name; ?> </option> 
     <?php 

     } /* END OF WHILE LOOP */ 

     $stmt->close(); 

    } /* END OF PREPARED STATEMENT OF CATEGORY */ 

    ?> 
    </select> 
    </label> 
</div> 

<div class="item"> 
<label> <span>Short Description</span> 
    <textarea required="required" name='sd'><?php echo $sd; ?></textarea> 
</div> 
<div class="item"> 
<label> <span>Full Description</span> 
    <textarea required="required" name='fd'><?php echo $fd; ?></textarea> 
</div> 
<div class="item"> 
<button id='cancel' type='cancel'>Cancel</button> 
<button id='send' type='submit'>Update</button> 
</div> 
</form> 

update.php

<?php 

    include('dbconn.php'); 

    $stmt = $mysqli->prepare("UPDATE main SET client=?, category=?, sd=?, fd=? WHERE srn=?"); 

    $stmt->bind_param('sssss', $_POST["client"], $_POST["category"], $_POST["sd"], $_POST["fd"], $_POST["srn"]); 

    $stmt->execute(); 

?> 
+0

但它不显示数据,即 – Kiran 2015-02-12 05:51:15

+0

@Kiran - 甚至是错误? – 2015-02-13 00:20:34

+0

现在我正在处理它。一旦我收到错误,我会回复你。 – Kiran 2015-02-13 07:41:33