2016-09-25 3 views
-1

동적으로 mysqli 테이블을 업데이트하면 조건이 저에게 효과적이지 않습니다. 도와주세요.동적으로 mysqli 테이블을 업데이트하는 방법 oopp의 조건

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(); 
} 

그러나 정적으로 내가이 작업을 수행 할 때 내가 모르는 작동하는 이유 :

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(); 
} 

어떻게 updateProfile 함수를 호출 :

<?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); 
} ?> 

updateMyProfile.php에서 내 html 양식

<!doctype html> 

<html lang="en"> 

    <body> 
<form name="uploadForm" method="post" action="updateMyprofile.php" >  
<label>profilep</label> <input type="text"name="profile_picture" ><br><br> 
<label>Uname</label> <input type="text" name="username" ><br><br> 
<label>BName</label><input type="text" name="businessname"><br><br> 
<label>Town </label> <input type="text" name="town" ><br><br> 
    <input type="submit" name="submit" value="Submit" /> 
    </form> 
    </body> 
</html> 
+0

내가 그 오류를 피하기 위해 할 일은 그 기능을 다음과 같습니다 또한 오류가 사라진 경우에도 작동하지 않습니다. '$ response = array(); if (isset ($ _ POST [ 'profile_picture']) && isset ($ _ POST [ 'username']) && isset ($ _ POST [ 'businessname']) && isset ($ _ POST [ 'town']) && isset ($ _POST [ 'profile_id'])! = '') { $ profile_picture = $ _POST [ 'profile_picture']; $ username = $ _POST [ 'username']; $ businessname = $ _POST [ 'businessname']; $ town = $ _POST [ 'town']; $ response = $ db-> createProfile ($ profile_picture, $ username, $ businessname, $ town, $ profile_id); }' – user3518835

+1

@scaisEdge가 게시 한 답변은 'profile_id'를 매개 변수로 함수에 전달하지 않았거나 함수 내의 전역 변수로 사용할 수 없게되었습니다. '4'의 값을 하드 코딩하면 변수가 필요한 이유가 무효화됩니다. 만약 당신이'profile_id'를 POST하면, 매개 변수로 전달하거나 함수 내에서 POST 배열로부터 변수를 생성해야합니다. – RamRaider

+0

함수 내에서 POST 배열로부터 어떻게 변수를 생성 할 수 있는지 모르겠습니다. 도움이 필요해. 지금 위의 코드는 위의 오류가 있지만 내 테이블에 업데이 트가 언제든지 해당 함수를 호출 – user3518835

답변

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 당신이해야 할 whay update 문 반대로 실제로 insert 문을 사용하는 것이 전적으로 가능 - 당신은 당신이 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 오류를 확인한 후 실제 문제가 무엇인지 설명하기 위해 열심히 노력하지 않는 경우 로그.

+0

죄송합니다. 아무런 변화가 없습니다. – user3518835

+0

이 함수를'createProfile'이라고하는데, 이것은'update' 문 대신에'insert' 문을 사용해야한다는 것을 의미합니다. 프로필이 아직 존재하지 않으며 실제로 프로필을 만들려고합니다. – RamRaider

+0

그것은 함수의 이름을 짓는 문제였습니다. 프로파일이 이미 존재하기 때문에 업데이 트하는 것입니다. 함수 이름을 변경하고 html 폼을 추가하여 질문을 업데이트했습니다. – user3518835

관련 문제