简体   繁体   中英

How to save data coming from a different MYSQL table?

Good day, I have a form to add new registers to a table called "viajes", below, both tables:

A picture of my tables https://i.stack.imgur.com/nUHgJ.png

agregar-viaje.php

<!doctype html>
<?php

include('libreria/motor.php');
require ('connection.php');
require ('libreria/verificar-usuario.php');
$est=viajes::mostrar();
$viajes=new viajes();
if($_POST){
$viajes->NROVIAJE=$_POST['NROVIAJE'];
$viajes->IMO=$_POST['IMO'];

$querybuqueimo="select NOMBRE from buques as elbuque where IMO='".$_POST['IMO']."'";
$relabuqueimo=mysql_query($querybuqueimo);
$buquedeimox=mysql_fetch_assoc($relabuqueimo);
$buquedeimo=$buquedeimox["elbuque"];

$viajes->BUQUE=$buquedeimo;
$viajes->PTOPROCEDENCIA=$_POST['PTOPROCEDENCIA'];
$viajes->PAISPROCEDENCIA=$_POST['PAISPROCEDENCIA'];
$viajes->PTODESTINO=$_POST['PTODESTINO'];
$viajes->PAISDESTINO=$_POST['PAISDESTINO'];
$viajes->OPERACION=$_POST['OPERACION'];
$viajes->CARGA=$_POST['CARGA'];
$viajes->CANTIDAD=$_POST['CANTIDAD'];
$viajes->CHARTER=$_POST['CHARTER'];
$viajes->guardar();
    }
$result = mysql_query("SELECT NOMBRE, IMO FROM buques ORDER BY NOMBRE ASC");
$opcion=' ';
while($row=mysql_fetch_assoc($result)){
    $opcion .= '<option value = "'.$row['IMO'].'">'.$row['NOMBRE'].' - IMO: '.$row['IMO'].'</option>';
}
$resultch = mysql_query("SELECT NOMBRECOMPANIA FROM clientes ORDER BY NOMBRECOMPANIA ASC");
$opcionch=' ';
while($row=mysql_fetch_assoc($resultch)){
    $opcionch .= '<option value = "'.$row['NOMBRECOMPANIA'].'">'.$row['NOMBRECOMPANIA'].'</option>';
}
$maxnroviaje=mysql_query("SELECT MAX(NROVIAJE) as maximo from viajes");
$nroviajenew=mysql_fetch_assoc($maxnroviaje);
$nroviajex=$nroviajenew["maximo"];
$nroviajex++;

?>
<html>
<head>
<meta charset="utf-8">
<title>Agregar Viaje</title>
<link href="bootstrap/css/bootstrap.min.css" rel="stylesheet" type="text/css" media="screen">
</head>

<body>
<div class="form-group">
<form id="form1" name="form1" method="post">
  <p>
    <label for="NROVIAJE">Número de Viaje:</label>
    <input name="NROVIAJE" type="number" id="NROVIAJE" autocomplete="off" class="form-control" readonly value="<?php echo $nroviajex; ?>">
  </p>
  <p>
    <label for="IMO">Buque:</label>
    <select name="IMO" required id="IMO" class="form-control">
    <option value=" "> Seleccione el buque </option>
    <?php echo $opcion; ?>
    </select>
  </p>
  <p>
    <label for="PTOPROCEDENCIA">Puerto de Procedencia:</label>
    <select name="PTOPROCEDENCIA" id="PTOPROCEDENCIA" class="form-control">
    <option value=" "> Seleccione un puerto de procedencia </option>
        <option value=" Aachen  "> Aachen </option>
        <option value=" Aalesund    "> Aalesund </option>
        <option value=" Aarhus  "> Aarhus </option>
        bla bla bla
    </select>
  </p>
  <p>
    <label for="PAISPROCEDENCIA">País de Procedencia:</label>     
    <select required name="PAISPROCEDENCIA" class="form-control">
      <option value="   ">Seleccione un país de procedencia </option>
        <option value=" Afganistán  "> Afganistán </option>
        <option value=" Akrotiri    "> Akrotiri </option>
        <option value=" Albania "> Albania </option>
        bla bla bla
      </select>
  </p>
  <p>
    <label for="PTODESTINO">Puerto de Destino:</label>
    <select name="PTODESTINO" required class="form-control" id="PTODESTINO">
    <option value=" "> Seleccione un puerto de destino </option>
        <option value=" Aachen  "> Aachen </option>
        <option value=" Aalesund    "> Aalesund </option>
        <option value=" Aarhus  "> Aarhus </option>
        bla bla bla
    </select>
  </p>
  <p>
    <label for="PAISDESTINO">País de Destino:</label>      
    <select required name="PAISDESTINO" class="form-control">
      <option value="   ">Seleccione un país de destino</option>
        <option value=" Afganistán  "> Afganistán </option>
        <option value=" Akrotiri    "> Akrotiri </option>
        <option value=" Albania "> Albania </option>
        bla bla bla
      </select>
  </p>
  <p>
    <label>Tipo de Operación:</label>
    <br>
    <label class="radio-inline">
      <input type="radio" name="OPERACION" value="CARGA" id="OPERACION_0" required>
      Carga</label>
    <label class="radio-inline">
      <input type="radio" name="OPERACION" value="DESCARGA" id="OPERACION_1">
      Descarga</label>
  </p>
  <p>
    <label for="CARGA">Cargamento:</label>
    <select name="CARGA" required id="CARGA" class="form-control">
    <option value=" ">  Seleccione el cargamento     </option>
    <option value=" 150N    ">  150N     </option>
<option value=" 150N/CHEVRONTEXACO  ">  150N/CHEVRONTEXACO   </option>
<option value=" 150N/SHELL  ">  150N/SHELL   </option>
bla bla bla

    </select>
  </p>
  <p>
    <label for="CANTIDAD">Cantidad (BBLS):</label>
    <input name="CANTIDAD" type="number" required id="CANTIDAD" min="1" class="form-control">
  </p>
  <p>
    <label for="CHARTER">Charter (Cliente):</label>
    <select name="CHARTER" required id="CHARTER" class="form-control">
    <option value=" "> Seleccione el charter (cliente) </option>
    <?php echo $opcionch; ?>
    </select>
  </p>
  <p>
    <input name="Enviar" id="Enviar" value="Enviar" type="submit" class="btn btn-default">
  </p>
</form>
</div>
</body>
</html>

This is the code of my form and I have managed to shoe a list of every vessel and her IMO in "Select IMO", however, I'm trying to find a way to, when pressing the submit button, both IMO and its related buque get saved in the viajes table, without having to add "buque" in any way in the form.

The way I'm doing things, a new register is being created in the "viajes" table with the rest of the data, but "buque" field remains blank and I get the following PHP error: Notice: Undefined index: elbuque in C:\\xampp\\htdocs\\agente-nav-online\\agregar-viaje.php on line 16

Many thanks beforehand four your help.

Based on the following error_log line:

PHP Notice: Undefined index: elbuque in C:\\xampp\\htdocs\\agente-nav-online\\agregar-viaje.php on line 16

And this part of the code:

/* 13 */ $querybuqueimo = "select NOMBRE from buques as elbuque where IMO='".$_POST['IMO']."'";
/* 14 */ $relabuqueimo = mysql_query($querybuqueimo);
/* 15 */ $buquedeimox = mysql_fetch_assoc($relabuqueimo);
/* 16 */ $buquedeimo = $buquedeimox["elbuque"];

The notice occurs on line 16, indicating that the index elbuque is nonexistent in the $buquedeimox array.

As we can see on line 13, the SQL syntax is wrong, where the following:

select NOMBRE from buques as elbuque where IMO='".$_POST['IMO']."'

Should actually be:

select `NOMBRE` as `elbuque` from `buques` where `IMO`='".$_POST['IMO']."'

Further comments

Do not use mysqli_* functions since these are deprecated and insecure. Use MySQLi or PDO with parameterized statements instead. This will also prevent SQL injections.

Stick to object-oriented style or procedural style, but don't mix them unless entirely necessary. I personally suggest object-oriented style, this paradigm improves your code's readability, efficiency, efficacy, modularity, and ultimately makes your application more maintainable.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM