php 是否有更有效的方法来更新数据库中的值

y3bcpkx1  于 2023-06-21  发布在  PHP
关注(0)|答案(3)|浏览(130)

我所做的是检查每个Input标签是否为空,如果不是空的,就调用函数,我只是想知道是否有更好的方法来做到这一点

$firstname = $this->request->getPost('firstname');
        $lastname = $this->request->getPost('lastname');
        $middlename = $this->request->getPost('middlename');
        $address = $this->request->getPost('address1') . $this->request->getPost('address2');
        $idNum = $this->request->getPost('idNum');
        $department = $this->request->getPost('department');
        $driverNumber = $this->session->get('driverNumber');

        if (!empty($firstname)) {
            $r = $this->db->editInfo('firstname', $firstname, $driverNumber);
        }
        if (!empty($lastname)) {
            $r = $this->db->editInfo('lastname', $lastname, $driverNumber);
        }
        if (!empty($middlename)) {
            $r = $this->db->editInfo('middlename', $middlename, $driverNumber);
        }
        if (!empty($this->request->getPost('address1')) || !empty($this->request->getPost('address2'))) {
            $r = $this->db->editInfo('address', $address, $driverNumber);
        }
        if (!empty($department)) {
            $r = $this->db->editInfo('department', $department, $driverNumber);
        }
        if (!empty($idNum)) {
            $r = $this->db->editInfo('idNum', $idNum, $driverNumber);
        }

我试过开关的情况下,但从我的理解开关的情况下需要一个变量等于一个字符串

$favcolor = "red";

switch ($favcolor) {
  case "red":
    echo "Your favorite color is red!";
    break;
  case "blue":
    echo "Your favorite color is blue!";
    break;
  case "green":
    echo "Your favorite color is green!";
    break;
  default:
    echo "Your favorite color is neither red, blue, nor green!";
}
chhkpiq4

chhkpiq41#

你可以将每个变量添加到一个数组中,循环数组以检查是否有任何变量为空,如果所有变量都存在,执行if语句,否则,错误输出。

$fields = array($firstname, $lastname, $middlename, $address, $idNum, 
$department, $driverNum);

foreach ($fields as $field) {
if ($field) {
// db calls
} else {echo "empty field"}`;
}

您也可以通过关联的方式来指示哪个键是空的。

$fields = array("firstname" => $firstname, "lastname" => $lastname, 
"middlename" => $middlename, "address" => $address, "id" => $idNum, 
"department" => $department, "driverNo" => $driverNum);

foreach ($fields as $key => $value) {
if ($value)
//db calls
} else {echo  $key . "is empty"}

希望这能帮上忙。

w7t8yxp5

w7t8yxp52#

创建模型**/app/Models/Model_name.php**&定义更新函数:

namespace App\Models;

use CodeIgniter\Model;
use CodeIgniter\Database\ConnectionInterface;
class Model_name extends Model {

    protected $db;
    public function __construct(ConnectionInterface &$db) {
        $this->db =& $db;
    }

    public function update($id, $data) {
        return $this->db
                        ->table('user')
                        ->where(["driverNumber" => $id])
                        ->set($data)
                        ->update();
    }
}

在您的控制器中:
1.在控制器中调用模型
1.初始化构造函数
1.使用模型更新数据

namespace App\Controllers;

use App\Models\Model_name; //Call a Model in Controller

class User extends BaseController {

  public function __construct() {    
      $db = db_connect();
      $this->Model_name= new Model_name($db);  //Update your model name
  }

  public function update() {
    $firstname = $this->request->getPost('firstname');
    $lastname = $this->request->getPost('lastname');
    $driverNumber = $this->session->get('driverNumber');

    $data = [
      'firstname'    => $firstname,
      'lastname'     => $lastname,
    ];

    $result = $this->Model_name->update($driverNumber, $data);
  }
}
vxf3dgd4

vxf3dgd43#

在您的例子中,您可以通过使用字段数组并循环检查它们是否为空来简化代码。
这样,您就可以避免为每个字段重复相同的条件语句。。您的更新代码如下

$fields = array(
        'firstname' => $this->request->getPost('firstname'),
        'lastname' => $this->request->getPost('lastname'),
        'middlename' => $this->request->getPost('middlename'),
        'address' => $this->request->getPost('address1') . $this->request->getPost('address2'),
        'idNum' => $this->request->getPost('idNum'),
        'department' => $this->request->getPost('department')
    );

$driverNumber = $this->session->get('driverNumber');

foreach ($fields as $field => $value) {
    if (!empty($value)) {
        $r = $this->db->editInfo($field, $value, $driverNumber);
    }
}

相关问题