如何使用oophp中的where where动态更新mysqli表

时间:2016-09-25 20:50:12

标签: php mysql mysqli

使用where条件对我不起作用动态更新mysqli表,请帮帮我。

   $query = $em->createQuery('SELECT u FROM My\UserBundle\Entity\User u WHERE u.id = :userid');//getting once again user entity but it did not change
    $query->setParameter('userid', $user->getId());
    $user = $query->getResult();

    // Force refresh of the object:
    $em->refresh($user);

    $tariff_upd = $user[0]->getTariff();//tariff1 here but I need tariff2! 

但是,当我这样做时,我会不知道原因:

public function updateProfile($profile_picture, $username,$businessname, $town) {

    $stmt = $this->conn->prepare("UPDATE profile_information set profile_picture = ?, username= ?, businessname= ?, town= ? where profile_id= ? ");

    $stmt->bind_param("ssssi",$profile_picture, $username,$businessname, $town, $profile_id);

    $stmt->execute();
    $stmt->close();
}

我如何调用updateProfile函数:在updateMyProfile.php中

public function updateProfile($profile_picture, $username, $businessname, $town) {

    $stmt = $this->conn->prepare("UPDATE profile_information set profile_picture = ?, username= ?, businessname= ?, town= ? where profile_id= 4 ");

    $stmt->bind_param("ssss",$profile_picture, $username,$businessname, $town);

    $stmt->execute();
    $stmt->close();
}

我的HTML格式

<?php 
include './DbHandler.php';
$db = new DbHandler();
$response = array(); 
if (  isset($_POST['profile_picture']) && isset($_POST['username']) &&      isset($_POST['businessname']) && isset($_POST['town']) != '') {

    $profile_picture = $_POST['profile_picture'];
  $username = $_POST['username'];
 $businessname = $_POST['businessname'];
 $town = $_POST['town'];

  $response = $db->updateProfile( $profile_picture, $username,     $businessname, $town);
}  ?>

1 个答案:

答案 0 :(得分:0)

如上所述,您需要将profile_id传递给您的函数或在函数内生成 - 无论哪种方式都需要可用,所以这样的事情可能会有所帮助。

$response = array();
if( isset( $_POST['profile_picture'],$_POST['username'],$_POST['businessname'],$_POST['town'],$_POST['profile_id'] ) ) { 

    $profile_picture = $_POST['profile_picture']; 
    $username = $_POST['username']; 
    $businessname = $_POST['businessname']; 
    $town = $_POST['town'];
    $profile_id=intval( $_POST['profile_id'] );

    $response = $db->createProfile( $profile_picture, $username, $businessname, $town, $profile_id );
}



public function createProfile( $profile_picture, $username, $businessname, $town, $profile_id ) {

    $stmt = $this->conn->prepare("update `profile_information` set `profile_picture`=?, `username`=?, `businessname`=?, `town`=? where `profile_id`=?;");
    if( $stmt ){
        $stmt->bind_param( "ssssi", $profile_picture, $username, $businessname, $town, $profile_id );
        $stmt->execute();
        $stmt->close();
    }
}

仅通过函数的名称来判断createProfile你完全可能需要做的是使用insert语句而不是update语句 - 你暗示你没有profile_id,因此支持您需要生成配置文件而不是更新的概念。在这种情况下,可能还有以下几点:

$response = array();
if( isset( $_POST['profile_picture'], $_POST['username'], $_POST['businessname'], $_POST['town'] ) ) { 

    $profile_picture = $_POST['profile_picture']; 
    $username = $_POST['username']; 
    $businessname = $_POST['businessname']; 
    $town = $_POST['town'];

    $response = $db->createProfile( $profile_picture, $username, $businessname, $town );
} else {
    echo "One or more required variables are NOT in the POST array!"
}


public function createProfile( $profile_picture, $username, $businessname, $town ) {
    $stmt = $this->conn->prepare("insert into `profile_information` set `profile_picture`=?, `username`=?, `businessname`=?, `town`=?;");
    if( $stmt ){
        $stmt->bind_param( "ssss", $profile_picture, $username, $businessname, $town );
        $stmt->execute();
        $stmt->close();
    }
}

如果这没有帮助,那么请更新您的问题以包含所有相关代码(包括表单)或更加努力地解释在检查php错误日志后实际问题是什么。