두 개의 쿼리가 수행되는 함수를 생성했습니다. 이 두 쿼리는 모두 사용자 등록과 관련된 두 개의 별도 테이블에 데이터를 삽입합니다. 사용자 이름, 암호와 같은 하나의 테이블 일 개최되는 등 주소, 전화와 같은 다른 테이블 물건에 ... 여기 는 기능입니다 : 사실2 개의 mysql 쿼리를 수행 할 때 오류 처리
function register_biz_user($post,$connection)
{
$name=$connection-> real_escape_string($_POST['name']);
$lastname= $connection->real_escape_string($_POST['lastname']);
$pass_hashed = password::hash($_POST['password']);
$passwd= $connection->real_escape_string($pass_hashed);
$buztype= $connection->real_escape_string($_POST['buztype']);
$usertype= $connection->real_escape_string($_POST['usertype']);
$address= $connection->real_escape_string($_POST['address']);
$city= $connection->real_escape_string($_POST['city']);
$municipality= $connection->real_escape_string($_POST['municipality']);
$url= $connection->real_escape_string($_POST['wwwaddress']);
$email= $connection->real_escape_string($_POST['e-mail']);
$phone= $connection->real_escape_string($_POST['phone']);
$hash =$connection->real_escape_string(md5(rand(0,1000))) ;
$connection->set_charset("utf8");
$result1 = $connection->query("insert into users values
(NULL,'" .$name. "','" .$lastname . "','".$email."','". $passwd."','".
$hash."','". $usertype."')");
if (!$result1) {
throw new Exception('error');
return false;
}
else{$result2=$connection->query("insert into business_users values
('".$connection->insert_id."','" .$address."','".$url ."','".$phone.
"','".$city. "','".$municipality. "','".$buztype. "')");
}
if(!$result2)
{ throw new Exception('error');
return false;}
반환; 당신이 코드를 보면 당신이 첫번째 쿼리가 문제없이 실행하고 두 번째는 예외 또는 그 verca을 던져 문제가 있음을 알 수 있습니다 경우 : }
그리고 여기 내 문제입니다.
저의 요점은 db가 등록 된 사용자의 부분 데이터 만 가질 위험이 있다는 것입니다. 목표는 두 쿼리가 성공적으로 실행되거나 실행되지 않는 것입니다.
위의 코드를 작성해야 어떻게하면 위의 내용을 얻을 수 있습니까?
내가 충분히 명확했으면 좋겠다.
transasctions를 사용하십시오. –
@AndyLester : "와이드 오픈"? 템플릿 주석 메시지를 복사하는 것이 항상 좋은 생각은 아닙니다. 이 경우에는 어떤 위협도 볼 수 없습니다. –
모든 것을 피하는 대신에 준비된 명령문으로 변경하는 것이 좋습니다. 준비된 문장은 잘라 내기 및 붙여 넣기 반복 코드의 양을 줄여 일부 입력을 피하기 위해 잊어 버린 바보 같은 실수를 저지를 가능성을 줄입니다. –