I can not insert foreign key data, help!

0

I need your help, I have a PHP form, which in an HTML select I get data from a table, the data I get is PK from another table (FK), then enter them into the database, the problem is that when I click the enter button, this data is not entered into the database and I do not even get an error, I do not know what I'm doing wrong, or maybe I can not add a data that is already added in the bd ?, I'll leave the code PHP and HTML, please your comments:

 <?php
include "../conexionBD.php";

if ($op == "add"){

    //asigna el post a las variables
    $Numero     = $_POST['Numero'];
    $Sector     = $_POST['ListaSector'];
    $Capacidad  = $_POST['Capacidad'];
    $Listagarzon            = $_POST['Listagarzon'];


    //prepara la query
    $stmt = $bd->prepare("
    INSERT INTO mesa (Numero, Sector, Capacidad, Rut_garzon) 
    VALUES (?, ?, ?, ?)");

    //asigna las variables a la query
    $stmt->bind_param('isis', $Numero, $Sector, $Capacidad, $Listagarzon);

    //ejecuta la query
    $stmt->execute();       
}

header('Location: modmesas.php');
?>


<section>
    <div class="container">
     <h4 align="center">Ingresar mesa</h4>
    <form action="controladorMesas.php?op=add" method="POST" enctype="multipart/form-data">

    <div class="form-group">
        <label for="Numero">Número:</label>
        <input type="number" step="1" class="form-control" id="Numero" name="Numero" min="1" max="20" placeholder="Ingresar número de mesa">
    </div>

    <div class="form-group">
        <label for="Sector">Sector:</label>
        <select class="form-control" id="sel2" name="ListaSector">
            <option value="Primer piso">Primer piso</option>
            <option value="Segundo piso">Segundo piso</option>
        </select>
    </div>

    <div class="form-group">
        <label for="Capacidad">Capacidad:</label>
        <input type="number" step="1" class="form-control" id="Capacidad" name="Capacidad" min="2" max="6" placeholder="Ingresar capacidad de personas">
    </div>

    <div class="form-group">
        <label for="Garzon">Garzon:</label>         
               <?php echo '<select class="form-control" id="sel3" name="Listagarzon">
        <option>Seleccione:</option>'?>

        <?php
        include "../conexionBD.php"; // este esta de más, ya estas llamando la conexion arriba

$query = 'SELECT * FROM garzon';

$result = $bd->query($query);
         while ( $row = $result->fetch_array() )    
   {
        ?>
        <option value=" <?php echo $row['Rut']  //el echo no esta cerrado ?> " >
         <?php echo $row['Rut']; ?>
    </option>
     <?php
  }    
  ?>   
        </select>
        <?php
  // para que esta este PHP?
 ?>         
    </div>

    <div class="btn-lg">
    <button type="submit" class="btn btn-primary" onclick="agregar()">Agregar</button>
    </div>

    </form>
    
asked by Pedro 29.10.2018 в 00:00
source

1 answer

0

It should be like this:

<?php
try {
$conn = new PDO('mysql:host=localhost;dbname=db', 'root', '');
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
}
catch(PDOException $e){
    echo "ERROR: " . $e->getMessage();
}


if(isset($_POST['add'])){
if(!empty($_POST['Numero']) && !empty($_POST['ListaSector']) && !empty($_POST['Capacidad']) && !empty($_POST['Listagarzon'])){
    $add = $conn->prepare("INSERT INTO mesa (Numero, Sector, Capacidad, Rut_garzoncha) VALUES (:Numero, :Sector, :Capacidad, :Rut_garzon)");
    $add->bindValue('Numero, Sector, Capacidad, Rut_garzon', ($_POST['nombre']));
    $add->bindValue(':Sector', ($_POST['Numero']));
    $add->bindValue(':Capacidad', ($_POST['Capacidad']));
    $add->bindValue(':Rut_garzoncha', ($_POST['Listagarzon']));
    $add->execute();
    echo "El nuevo cliente fue agregado <a href='../'>volver</a>";
}
else{
    echo "Ingresar datos <a href='../'>volver</a>";
}
}
?>


<section>
<div class="container">
<h4 align="center">Ingresar mesa</h4>
<form action="" method="post" enctype="multipart/form-data">
<div class="form-group">
<label for="Numero">Número:</label>
<input type="number" step="1" class="form-control" id="Numero" name="Numero" min="1" max="20" placeholder="Ingresar número de mesa">
</div>

<div class="form-group">
<label for="Sector">Sector:</label>
<select class="form-control" id="sel2" name="ListaSector">
<option value="Primer piso">Primer piso</option>
<option value="Segundo piso">Segundo piso</option>
</select>
</div>

<div class="form-group">
<label for="Capacidad">Capacidad:</label>
<input type="number" step="1" class="form-control" id="Capacidad" name="Capacidad" min="2" max="6" placeholder="Ingresar capacidad de personas">
</div>

<div class="form-group">
<label for="Garzon">Garzon:</label>         
<select class="form-control" id="sel3" name="Listagarzon">
<option>Seleccione:</option>
<?php
$consulta = $conn->query("SELECT * FROM garzon");
while ($row = $consulta->fetch()){
?>
<option value=" <?=$row['Rut'];?>"><?=$row['Rut'];?></option>
<?php
}    
?>   
</select>       
</div>

<div class="btn-lg">
<button type="submit" class="btn btn-primary" onclick="agregar()" name="add">Agregar</button>
</div>
</form>

ps: I can not comment, I do not reach 50 points.

    
answered by 29.10.2018 в 01:41