2017-05-14 153 views
0

我試圖做到哪裏有FK在表中插入,但它不工作..PHP的MySQL插入有多個外鍵

我的數據庫名字bd_telefonos

我的數據庫看起來像這樣的:

表MARCAS

idmarcas --> PK AI

nombre

sistemas_operativos

idsistemas_operativos -->PK AI

nombre

telefonos

modelo

idtelefonos --> PK AI

resolucion_alto

resolucion_ancho

peso

bateria

marcas_idmarcas --> FK

sistemas_operativos_idsistemas_operativos -- > FK

我的PHP是這樣的:

<?php 

include_once('conexion/conexion.php'); 
$con = new conectar(); 

$modelo= $_POST['modelo']; 
$alto= $_POST['alto']; 
$ancho= $_POST['ancho']; 
$peso= $_POST['peso']; 
$bateria= $_POST['bateria']; 
$nombresis = $_POST['nombresis']; 
$nombremarca = $_POST['nombremarca']; 



$sSql = "INSERT INTO `bd_telefonos`.`telefonos` (`modelo`, `resolucion_alto`, `resolucion_ancho`, `peso`, `bateria`) VALUES ('$modelo', '$alto', '$ancho', '$peso', '$bateria')"; 

if(!mysqli_query($con->conectarse(), $sSql)){ 
     $resp = 'error '; 


    } 
    else{ 

     /*============================================================================= 
     =   select recently insert   = 
     =============================================================================*/ 

     $sSqlId = "SELECT MAX(id) as `id` FROM `bd_telefonos`.`telefonos`"; 

     if($respuesta = mysqli_query($con->conectarse(), $sSqlId)){ 
      foreach ($respuesta as $resp) { 
       $id = $resp['id']; 
      } 
     } 

     /*======================================================== 
     =   Insert sistemas_operativos   
     ========================================================*/ 

     $sSqlMas = "INSERT INTO `bd_telefonos`.`sistemas_operativos` (`idsistemas_operativos`, `nombre`) VALUES ('$id', '$nombresis')"; 

     if (mysqli_query($con->conectarse(), $sSqlMas)) { 
      $resp = 'success'; 
      echo $resp; 
     } else { 
      $resp = 'error'; 
      echo $resp; 
     } 


     /*======================================================== 
     =   Insert marcas    
     ========================================================*/ 


     $sSqlMass = "INSERT INTO `bd_telefonos`.`marcas` (`idmarcas`, `nombre`) VALUES ('$id', '$nombremarca')"; 
     if (mysqli_query($con->conectarse(), $sSqlMass)) { 
      $resp = 'success'; 
      $resp = 'error'; 

      echo $resp; 
     } 

    } 

    mysqli_close($con->conectarse()); 




?> 

它不工作,它只是沒有在我的表telefonos使插入,我做錯了什麼?

幫助請:(

+0

「它不工作」是一個相當不精確的是觀察到的行爲的描述。一些建議...使用'mysqli_error'從MySQL檢索錯誤消息,並顯示錯誤或記錄它。此外,用於檢索剛剛插入的行的id值的「select max(id)」模式將在測試中起作用,但它是併發會話的基本斷點模式。該代碼似乎也容易受到SQL注入的影響。 https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – spencer7593

回答

1

使用$ conn-> INSERT_ID而不是MAX(ID)。

$conn = new mysqli("hostname", "db_user_name", "db_password", "db_name"); 

// Check connection 
if ($conn->connect_error) { 
    die("Connection failed: " . $conn->connect_error); 
} 

$modelo= $_POST['modelo']; 
$alto= $_POST['alto']; 
$ancho= $_POST['ancho']; 
$peso= $_POST['peso']; 
$bateria= $_POST['bateria']; 
$nombresis = $_POST['nombresis']; 
$nombremarca = $_POST['nombremarca']; 



$sSql = "INSERT INTO `telefonos` (`modelo`, `resolucion_alto`, `resolucion_ancho`, `peso`, `bateria`) VALUES ('$modelo', '$alto', '$ancho', '$peso', '$bateria')"; 

if ($conn->query($sSql) === TRUE) { 
    $id = $conn->insert_id; 

    /*======================================================== 
    =   Insert sistemas_operativos 
    ========================================================*/ 

    $sSqlMas = "INSERT INTO `sistemas_operativos` (`idsistemas_operativos`, `nombre`) VALUES ('$id', '$nombresis')"; 

    if ($conn->query($sSqlMas) === TRUE) { 
     echo 'success'; 
     $sistemas_operativos_id = $conn->insert_id; 
    } else { 
     echo "Error: " . $sSqlMas . "<br>" . $conn->error; 
    } 

    /*======================================================== 
    =   Insert marcas 
    ========================================================*/ 

    $sSqlMass = "INSERT INTO `marcas` (`idmarcas`, `nombre`) VALUES ('$id', '$nombremarca')"; 

    if ($conn->query($sSqlMass) === TRUE) { 
     echo 'success'; 
     $marcas_id = $conn->insert_id; 
    } else { 
     echo "Error: " . $sSqlMass . "<br>" . $conn->error; 
    } 

    /************** Update FK ***************/ 
    $update_sql = "UPDATE telefonos SET marcas_idmarcas ='$marcas_id', sistemas_operativos_idsistemas_operativos ='$sistemas_operativos_id' WHERE idtelefonos = '$id'"; 
    if ($conn->query($update_sql) === TRUE) { 
     echo 'Successfully Update'; 
    } else { 
     echo "Error: " . $update_sql. "<br>" . $conn->error; 
    } 


} else { 
    echo "Error: " . $sSql . "<br>" . $conn->error; 
} 

$conn->close(); 
+0

我得到這個錯誤:'錯誤:INSERT INTO'telefonos'('modelo','resolucion_alto','resolucion_ancho' ('g','g','g','g','g') 無法添加或更新子行:外鍵約束失敗('bd_telefonos')。 ''telefonos',CONSTRAINT'fk_telefonos_marcas' FOREIGN KEY('marcas_idmarcas')參考'marcas'('idmarcas')ON DELETE NO ACTION ON UPDATE NO ACTION)' – User100696

+0

解決了!謝謝。 – User100696

0

是否要更新下面兩個列在表telefonos

marcas_idmarcas --> FK 
sistemas_operativos_idsistemas_operativos -- > FK 

你可以從兩個表中獲得最後一個插入值並在telefonos表中更新