3 개의 테이블이 있습니다. B, 참고 문헌; C는 위치와 참조 사이의 관계를 수행합니다. 각 위치는 둘 이상의 참조를 가질 수 있지만 그 반대는 허용되지 않습니다. 오케이. 내 루틴 : 위치와 참조 사이의 관계를 저장하는 페이지가 양식에서 제출 buttom에 의해로드 될 때 : 1.이 위치가 존재하는지 확인 예이면 2로 이동하십시오. 그렇지 않으면 오류. 2.이 참조가 존재하는지 확인하십시오. 예인 경우이 참조와이 위치 사이의 관계를 추가하십시오. 아니요,이 참조를 추가 한 후이 참조와이 위치 사이의 관계를 추가하십시오.테이블에서 마지막으로 추가 한 항목을 다른 테이블에 삽입합니다.
이것은 제 기본 루틴입니다. 내 문제? 참조가 존재하지 않으면 스크립트는 새로운 참조를 추가 한 후 새 참조와 위치 사이의 관계를 추가합니다 ...하지만 잘 작동하지 않습니다. 처음에는 스크립트에서 참조 만 추가하지만 관계는 추가하지 않습니다 ... 페이지에서 다시 필요하고 다시 추가해야합니다.
내 코드 : 페이지 새로 고침없이, DB에 이후 새로운 참조를 추가 : 나는 올바르게 작동하는지이 작업을 수행하는 다른 방법을 모르는
<?php
#verify if this position exists at db
if(!empty($_POST['position'])) {
$query_position = "SELECT `PositionsId`, `PositionsIdentifier` from `positions` WHERE `PositionsId` = '".$_POST['position']."'";
$select_position = mysqli_query($connect, $query_ref);
#if yes, proceed to insert the new reference into this position...
if (mysqli_num_rows($select_position)) {
#verify if the reference exists at DB
if(!empty($_POST['ref'])) {
$query_ref = "SELECT `ReferencesId`, `ReferencesIdentifier` from `references` WHERE `ReferencesIdentifier` = '".$_POST['ref']."'";
$select_ref = mysqli_query($connect, $query_ref);
if (mysqli_num_rows($select_ref)) {
$list_ref = mysqli_fetch_array($select_ref, MYSQLI_ASSOC);
#verify if this reference exists to this position
$query_reference = "SELECT `PositionRefId`, `PositionsRef_PosId`, `PositionRef_RefId` FROM `positions_refs` WHERE `PositionsRef_PosId` = '".$id."' AND `PositionsRef_RefId` = '".$list_ref['ReferencesId']."'";
$select_reference = mysqli_query($connect, $query_reference);
if (mysqli_num_rows($select_reference)) {
echo "This reference exists to this position...";
}
else {
#insert this reference to this position
$query_insert_reference = "INSERT INTO `positions_refs` (`PositionsRef_PosId`, `PositionRef_RefId`) VALUES ('".$id."','".$list_ref['ReferencesId']."')";
if (mysqli_query($connect, $query_insert_reference)) {
echo "OK.";
}
else {
echo "Error.";
echo "<br /><br />";
echo mysqli_error($connect);
}
}
}
else {
#if don't exists at db, insert the new reference
if(!empty($_POST['ref'])) {
$query_insert_ref = "INSERT INTO `references`(`ReferencesIdentifier`) VALUES ('".$_POST['ref']."')";
if (mysqli_query($connect, $query_insert_ref)) {
#select the new reference ID
$query_new_ref = "SELECT `ReferencesId`, `ReferencesIdentifier` from `references` WHERE `ReferencesIdentifier` = '".$_POST['ref']." LIMIT 1'";
$select_new_ref = mysqli_query($connect, $query_new_ref);
if (mysqli_num_rows($select_new_ref)) {
$list_new_ref = mysqli_fetch_assoc($select_new_ref, MYSQLI_ASSOC);
#insert the new reference to this position
$query_insert_reference = "INSERT INTO `positions_ref`(`PositionsRef_PosId`, `PositionRef_RefId`) VALUES ('".$id."','".$list_new_ref['ReferencesId']."')";
if (mysqli_query($connect, $query_insert_reference)) {
echo "OK.";
}
else {
echo "Error.";
}
}
else {
}
}
else {
echo "Error.";
}
}
else {
echo "Error";
}
}
}
else {
echo "Error";
}
}
}
else {
echo "Error.";
}
?>
... 이 작업을 수행하려면 어떤 방법이 , 등,이 새로운 참조를 위치에 추가 하시겠습니까?
감사합니다,
귀하의 코드에 취약 [** SQL 주입 **] (HTTPS : //en.wikipedia.org/wiki/SQL_injection) 공격. [** mysqli **] (https://secure.php.net/manual/en/mysqli.prepare.php) 또는 [** PDO **] (https (https://secure.php.net/manual/en/mysqli.prepare.php)) 매개 변수를 통해 매개 변수가 바인딩 된 준비된 문을 사용해야합니다. : //secure.php.net/manual/en/pdo.prepared-statements.php) 드라이버. [**이 게시물 **] (https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php)에는 좋은 예가 있습니다. –
솔직하게 말하면 참조 및 직책에 대한 귀하의 설명은 저를 혼란스럽게합니다. 하지만 코드를 살펴보면 두 개의 insert 문이 같은 테이블에 삽입되지 않는다는 것을 알았습니다 ...? INSERT INTO'positions_refs' .... 다른 INSERT INTO'variantsref'는 ... 정상입니까? 최종 코멘트, 당신은 거래를 찾아 봐야합니다. 3 개의 쿼리 중 하나가 실패하면 참조 무결성을 깨뜨릴 수 있습니다 (또는 db 수준에서 처리 한 것입니까?). 그리고 준비된 진술! – Nic3500
안녕하세요, 이 문제는 해결되었지만 동일한 표가 있습니다. 쿼리가 표시되고 오류가 표시되지만이 스크립트를 실행하면 참조가 없으면 삽입되지만 원하는 위치에 새 참조가 삽입되지 않습니다. 다시 입력 해주세요. 스크립트는 이것을 삽입합니다 ... 마지막 참조에서이 참조와 위치 사이의 관계를 추가하기 위해 추가 된 ID를 얻는 데 문제가 있다고 생각하지만 문제인지 여부는 알 수 없습니다. – Rafael