Help with loading SELECT table and INSERT data to display in a PHP MYSQL table!

1

I am new in these areas I found your page on the WEB and I want a little hand for this system that I am doing, since my expertise with programming and web development is really basic and I am still in the process of perfecting myself and learning more.

I'm going to the point, the problem I have is that through a registration form I have only textbox to add data. The thing is that they asked me to add a dynamic select and I forgot how to install it inside my maintainer. However create the fields and a table that is related to another to be able to make references and show the data that comes out there, the drama is not to show the select and make it look in a list. I will add the forms and the database.

Database

use master;

create database intranet;

use intranet;

create table cargo(
cod_cargo int (5)  PRIMARY KEY,
nom_cargo varchar(30),
foreign key (cod_cargo) REFERENCES usuarios(cod_cargo)
);

insert into cargo values ('1','Secretaria Alcaldia');
insert into cargo values ('2','Jefe de Gabinete');
insert into cargo values ('3','Secretaria de Administracion');
insert into cargo values ('4','Encargado de Informatica');
insert into cargo values ('5','Oficina de Partes');
insert into cargo values ('6','Encargada de OIRS');

drop table cargo;

create table usuarios(
usuario varchar(45) PRIMARY KEY,
clave varchar(45) NOT NULL,
cod_cargo int (5) NOT NULL,
cargo varchar (45) NOT NULL,
admin boolean NOT NULL,
foreign key (cod_cargo) references cargo (cod_cargo)
);

insert into usuarios values
('Admin','123456','1','Administrador',1);

drop table usuarios;

Registration form with HTML code , here is where I enter the data, supposedly I have to add a SELECT below the key Textbox and replace the one that is there.

<?php 
require '../scripts/funciones.php';
if(! haIniciadoSesion() || ! esAdmin() )
{
header('Location: index.html');
}

conectar();
$usuarios = getUsuario();
desconectar();
?>

<!DOCTYPE html>

<html lang="es">

  <head>

    <meta charset="utf-8">

    <meta name="viewport" content="width=device-width, initial-scale=1">

    <title>Panel de Administración</title>

    <!-- Bootstrap core CSS -->

    <link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/css/bootstrap.min.css">

    <!-- Custom styles for this template -->

    <link href="../css/dashboard.css" rel="stylesheet">

    <!-- Validaciones en JavaScript -->

    <script src="validar-usuario.js" type="text/javascript"></script>

    <!-- CSS ME -->

    <link rel="stylesheet" type="text/css" href="../css/estilos.css">

  </head>

  <body>
    <?php include './menu-superior.php'; ?>
    <div class="container-fluid">
      <div class="row">

        <?php include './sidebar.php'; ?>

        <!-- <?php include './menu-lateral.php'; ?> -->

        <div class="col-sm-9 col-sm-offset-3 col-md-10 col-md-offset-2 main" >

        <h1 class="page-header">Agregar Nuevo Usuario:</h1>

        <form action="registro.php" method="post" onsubmit="return validar();">

        <div class="form-group">
            <label for="email">Usuario:</label>
<input style="width: 20%;" type="text" class="form-control" id="txtUsuario" name="txtUsuario" placeholder="Ingrese Usuario" required="required"  >
         </div>
        <div class="form-group">
            <label for="pwd">Clave:</label>
<input style="width: 20%;" type="password" class="form-control" id="txtClave" name="txtClave" placeholder="Ingrese Clave"required="required"   >
        </div>

    <label for="pwd">Cargo:</label>
    <br>
    <select style="width: 20%;" name="cargo" size="0">
      <?php
       while ($arreglo = mysql_fetch_array($query)) {   ?>
        <option value="<?php echo $arreglo ['cod_cargo']?>"><?php echo $arreglo['nom_cargo']?></option>
      <?php } ?>
    </select>
</div>

      <div class="form-group">
      <label for="pwd">N° de Permiso (1 para admin y 0 para usuario):</label>
      <br>
     <input style="width: 20%;" type="number" class="form-control" id="txtAdmin" name="txtAdmin" placeholder="Ingrese N° de Permiso"required="required"> 

<hr>

<button type="submit" id="btnguardar" name="btnguardar" class="btn btn-primary">Guardar</button>

<input type="reset" name="btn-reset" value="Limpiar Campos" class="btn btn-primary">

        </form>

       </div>

       <?php
       if(isset($_POST['btnguardar'])){
        require("registro.php");

        }
       ?>

    </div>
  </div>

    <!-- Bootstrap core JavaScript
    ================================================== -->
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.2/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.2/js/bootstrap.min.js"></script>
  </body>
</html>      

PHP Registry , page where I insert the data.

<?php


$conexion;

require '../scripts/funciones.php';

conectar();

// CREACION DE VARIABLES $USUARIO, $CLAVE, $CARGO Y $PERMISO PARA ALMACENAR LOS DATOS

$usuario = $_POST['txtUsuario'];
$clave = $_POST['txtClave'];
$cargo = $_POST['txtCargo'];
$permiso = $_POST['txtAdmin'];

// CONSULTA PARA INSERTAR LOS DATOS RECOGIDOS EN LAS VARIABLES DENTRO DEL FORMULARIO

$insertar = mysqli_query($conexion, "INSERT INTO usuarios VALUES('".$usuario."', '".$clave."', '".$cargo."', '".$permiso."')");

// EJECUCION DE CONSULTA

$resultado = mysqli_query($conexion, $insertar); 

// IF PARA VERIFICAR SI EL USUARIO SE REGISTRO O NO, DE LO CONTRARIO INGRESE NUEVAMENTE LOS DATOS

if(!$resultado){
echo'<script>alert("Usuario registrado satisfactoriamente!")</script>';
echo'<script>alert("Ahora elija los permisos para el usuario.")</script>';
echo '<script>location.href ="permisos.php"</script>';
}else
{
echo '<script>alert("Error, Usuario no Registrado.")</script>';
}

mysqli_close($conexion);

?>

It would be very helpful if you can help me, I would greatly appreciate a lot of greetings to all from Chile.

    
asked by Manticore 19.06.2018 в 23:15
source

3 answers

0

There is a problem in the part where you build the select , since you mix the API mysql_ (obsolete) with the API mysqli_ (that you use, and thus it should be, in the rest of the code, less here).

You should therefore use mysqli also there:

<select style="width: 20%;" name="cargo" size="0">
  <?php while ($arreglo = mysqli_fetch_array($query)) { ?>
    <option value="<?php echo $arreglo ['cod_cargo']?>"><?php echo $arreglo['nom_cargo']?></option>
  <?php } ?>
</select>

NOTE.OF SECURITY:

Your query INSERT is highly vulnerable to SQL injection attacks. You should apply prepared queries to correct that security hole in the code.

In a later edition of this answer you could indicate the way to do it, if you are interested.

    
answered by 21.06.2018 / 20:07
source
0

in the insert apparently missing a data

in the user table

usuario varchar(45) PRIMARY KEY,
clave varchar(45) NOT NULL,
cod_cargo int (5) NOT NULL,
cargo varchar (45) NOT NULL,
admin boolean NOT NULL,

are 5 records and in the select only you put 4

$insertar = mysqli_query($conexion, "INSERT INTO usuarios VALUES('".$usuario."', '".$clave."', '".$cargo."', '".$permiso."')");

If you want to put only those 4 you must specify it in the INSERT

$insertar = mysqli_query($conexion, "INSERT INTO usuarios (usuario,clave,cod_cargo,admin) VALUES('".$usuario."', '".$clave."', '".$cargo."', '".$permiso."')");
    
answered by 19.06.2018 в 23:24
0

I understand your point perfectly, what I want to do is the complete SELECT sequence. I do not know if you noticed but the charge table has a name field charge that same I want to translate it into my table to show the user records next to that field. That's exactly where I'm running: /

    
answered by 20.06.2018 в 02:03