-2

有代码:

$id = $_POST['id'];
$name = $_POST['name'];
$surname = $_POST['surname'];
$mail = $_POST['mail'];

$sql = "UPDATE emps SET";
$moresql = '';

if(isset($name) && !empty($name)) {
    $moresql .= " name = '$name'";  
}

if(isset($surname) && !empty($surname)) {
    if ($moresql) $moresql .= ',';
    $moresql .= " surname = '$surname'";    
}

if(isset($mail) && !empty($mail)) {
    if ($moresql) $moresql .= ',';
    $moresql .= " mail = '$mail'";  
}

$sql .= $moresql;
$sql .= " WHERE Id = '$id'";

在此代码中,我可以更新例如:姓名并保留姓氏和电子邮件中的现有值或更新姓氏并留下姓名并通过电子邮件发送现有值

但我也想一次,例如:更新姓名和姓氏并留下电子邮件现有值或更新姓氏和电子邮件并留下姓名现有值或更新所有字段。如何?

4

2 回答 2

3
<form action="settings.php" method="POST">
       <input type="text" name="full name" />
       <input type="text" name="email" />
       ........ (and more)
</form>

<?php
  if($_POST)
  {
   if(isset($_POST['full name'])) { //Update full name of user and redirect to the settings page on success. }
   else { //Redirect and show errors! }
   if(isset($_POST['email'])) { //Update email of user and redirect to the settings page on success. } 
   else { //Redirect and show errors! }      
  }
?>

或者您可以使用 PHP 的数组函数在其中设置 MySql 查询,如 ---

<?php
  mysql_query("
    UPDATE table name SET 
    //Loading different values from the array using foreach() php function.
  ");
?>

相似且有用:

if(isset($_POST['foo']) && !empty($_POST['foo'])){/*do stuff*/}
于 2013-02-27T18:24:08.513 回答
1
SET ... name=name, email=email

这样表将保留以前的值(col_name=col_colname)

在 PHP 中

if(isset($mail) && !empty($mail)) 
{
    if ($moresql) $moresql .= ',';
    $moresql .= " mail = '$mail'";  
}
else
{
 if ($moresql) $moresql .= ',';
    $moresql .= " mail = mail"; 
}
于 2013-02-27T18:17:19.180 回答