我想更新\ edit mysql表行数据,但它无法正常工作。
从表单元素中获取所有值并使用会话变量存储它:
if(isset($_POST['continue']))
{
// db connection
include('c-db.php');
// prevent mysql injection
foreach ($_POST as $key => $value) {
$_POST[$key] = mysql_real_escape_string(strip_tags($value));
}
// step one form element data
$_SESSION['pName'] = $_POST['pName'];
$_SESSION['position'] = $_POST['position'];
$_SESSION['departm'] = $_POST['departm'];
$_SESSION['birth'] = $_POST['birth'];
$_SESSION['blood'] = $_POST['blood'];
$_SESSION['gender'] = $_POST['gender'];
// step two form element data
$_SESSION['fName'] = $_POST['fName'];
$_SESSION['mName'] = $_POST['mName'];
$_SESSION['religion'] = $_POST['religion'];
$_SESSION['marital'] = $_POST['marital'];
$_SESSION['country'] = $_POST['country'];
$_SESSION['present'] = $_POST['present'];
// step three form element data
$_SESSION['email'] = $_POST['email'];
$_SESSION['mobile'] = $_POST['mobile'];
$_SESSION['office'] = $_POST['office'];
$_SESSION['home'] = $_POST['home'];
$_SESSION['fax'] = $_POST['fax'];
$_SESSION['lasting'] = $_POST['lasting'];
}
它是多步形式,其中包含文件包含表单元素:
<?php
if ( isset( $_POST["step"] ) and $_POST["step"] >= 1 and $_POST["step"]<= 5 )
{
call_user_func( "processStep" . (int)$_POST["step"] );
}
else
{
displayStep1();
}
function processStep1()
{
displayStep2();
}
function processStep2()
{
if ( isset( $_POST["continue"] ) and $_POST["continue"] =="< Back" )
{
displayStep1();
}
else
{
displayStep3();
}
}
function processStep3()
{
if ( isset( $_POST["continue"] ) and $_POST["continue"] =="< Back" )
{
displayStep2();
}
else
{
displayComplete();
}
}
function processStep4()
{
if ( isset( $_POST["complete"] ) and $_POST["complete"] =="< Back" )
{
displayStep3();
}
else
{
displaySuccess();
}
}
?>
<?php function displayStep1() { ?>
<?php include 'update_step1.php'; ?>
<?php } ?>
<?php function displayStep2() { ?>
<?php include 'update_step2.php'; ?>
<?php } ?>
<?php function displayStep3() { ?>
<?php include 'update_step3.php'; ?>
<?php } ?>
<?php function displayComplete() { ?>
<?php include 'update_step4.php'; ?>
<?php } ?>
<?php function displaySuccess() { ?>
<?php include 'update_step5.php'; ?>
<?php } ?>
update_step3:
<?php
// db connection
include('c-db.php');
// get id using username
$get_u_name = $_SESSION['SESS_name'];
$get_u_id = mysql_query('SELECT * FROM users WHERE name = "'. $get_u_name .'"');
$retrieve = mysql_fetch_array($get_u_id);
// using id fetching user data
$id = $retrieve['id'];
$get_u_data = mysql_query('SELECT * FROM users_info WHERE id = "'. $id .'"');
$extr = mysql_fetch_array($get_u_data);
?>
<form id="add_info" action="" method="post" enctype="multipart/form-data">
<!-- value devlivered from step to condition -->
<input type="hidden" name="step" value="3" />
<div class="wizard">
<h3 id="repost">Step 1</h3>
<h3>Step 2</h3>
<h3 class="active">Step 3</h3>
<h3>Step 4</h3>
</div>
<ul>
<li>
<small>•</small>
<label>E-mail</label>
<span>:</span>
<input type="text" name="email" data-bvalidator="email,required"
value="<?php echo $extr['email']; ?>" />
</li>
<li>
<small>•</small>
<label>Mobile</label>
<span>:</span>
<input type="text" name="mobile" data-bvalidator="digit,maxlength[30],required"
value="<?php echo $extr['mobile']; ?>" />
</li>
<li>
<small class="opt-field">•</small>
<label>Office Phone</label>
<span>:</span>
<input type="text" name="office"
value="<?php echo $extr['office']; ?>" />
</li>
<li>
<small class="opt-field">•</small>
<label>Home Phone</label>
<span>:</span>
<input type="text" name="home"
value="<?php echo $extr['home']; ?>" />
</li>
<li>
<small class="opt-field">•</small>
<label>Fax</label>
<span>:</span>
<input type="text" name="fax"
value="<?php echo $extr['fax']; ?>" />
</li>
<li>
<small>•</small>
<label>Lasting Address</label>
<span>:</span>
<textarea name="lasting" data-bvalidator="address,maxlength[150],required"><?php echo $extr['lasting']; ?></textarea>
</li>
<li class="end-list">
<button type="reset">Clear</button>
<button type="submit" name="continue" onClick="return validateOff();" value="< Back">Previous</button>
<button type="submit" name="continue" onclick="return validateOn();">Continue</button>
</li>
</ul>
</form>
行正确获取行数据,但在提交表单后,它会使用空白更新行字段,它会在前两个表单上发生,但第三个表单正确更新:
if(isset($_POST['complete']))
{
// db connection
include('c-db.php');
// insert data by get id
$id = $_GET['id'];
// insert data if id is empty
if(empty($id))
{
$get_u_name = $_SESSION['SESS_name'];
$get_u_id = mysql_query('SELECT * FROM users WHERE name = "'. $get_u_name .'"');
$retrieve = mysql_fetch_array($get_u_id);
$id = $retrieve['id'];
}
// variable to store data
$pName = $_SESSION['pName'];
$position = $_SESSION['position'];
$departm = $_SESSION['departm'];
$birth = $_SESSION['birth'];
$blood = $_SESSION['blood'];
$gender = $_SESSION['gender'];
$fName = $_SESSION['fName'];
$mName = $_SESSION['mName'];
$religion = $_SESSION['religion'];
$marital = $_SESSION['marital'];
$country = $_SESSION['country'];
$present = $_SESSION['present'];
$email = $_SESSION['email'];
$mobile = $_SESSION['mobile'];
$office = $_SESSION['office'];
$home = $_SESSION['home'];
$fax = $_SESSION['fax'];
$lasting = $_SESSION['lasting'];
// get username by get id
$get_uname = mysql_query('SELECT * FROM users WHERE id = "'. $id .'"');
$record = mysql_fetch_array($get_uname);
$u_name = $record['name'];
// store username to display
$uname = "_".$u_name;
$_SESSION['u_name'] = $u_name;
// renaming uploaded image by predefined username
$filename = $_FILES['photo']['name'];
// get extension of the image
$ext = strtolower(substr(strrchr($filename, '.'), 1));
$img_name = $uname . '.' . $ext;
// image upload directory
$target_path = "uploads\\";
$target_path = $target_path . $img_name;
if($_FILES['photo']['name']==='')
{
if($gender==='Male')
{
$photo = "male.png";
}
elseif($gender==='Female')
{
$photo = "female.png";
}
else
{
$photo = "confused.png";
}
}
elseif(move_uploaded_file($_FILES['photo']['tmp_name'], $target_path))
{
$photo = $img_name;
}
else{die(mysql_error());}
// store target place to variable
mysql_query("UPDATE users_info SET
pName='$pName', photo='$photo', position='$position', departm='$departm',
birth='$birth', blood='$blood', gender='$gender', fName='$fName', mName='$mName', religion='$religion',
marital='$marital', country='$country', present='$present', email='$email', mobile='$mobile',
office='$office', home='$home', fax='$fax', lasting='$lasting' WHERE id='$id'") or die(mysql_error());
}
请帮我找出问题所在,以及建议是什么。