我是新来的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");
?>
不要使用'mysql_ *'功能,这些API已被弃用。你还应该使用准备好的语句。 – Jens 2015-02-11 06:42:24
如何使用准备好的语句? – Kiran 2015-02-11 06:48:47
阅读[手册](http://php.net/manual/de/mysqli.quickstart.prepared-statements.php) – Jens 2015-02-11 06:59:06