Display an alert when inserting a duplicate product in the database

2

I have a question, I want to add a product to a database and when this product exists there is an alert that says "this product is already in the database", a question that I have achieved quite well except that when this warning comes out, is redirected to the part of "botones.php (another file of the program), how do I make it so that when I accept the alert, I stay where it is so I can change the name?

"add prod" code:

<!DOCTYPE html>
  <html>
  <head>
<title>Agregar Nuevo Producto</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta charset="utf-8">
<link rel="stylesheet" type="text/css" href="style.css">
</head>
  <body>

  <h3 align="center" style="color: #0B3861">Agrega un nuevo producto!</h3>

 <form method="POST" action="botones.php">

<div class="input-group">
<label>Nombre del Producto:</label>
<input type="text" name="nombreProd" required>
</div>

<div class="input-group">
<label>Stock:</label>
<input type="number" name="stockProd" required>
</div>

 <div class="input-group">
<input type="submit" name="agregar" value="Agregar" class="btn">
</div>

 </form>

<div class="input-group" align="center">
    <a href="./adminStock.php">
    <input style="width: 100px" type="button" name="volver" value="Volver" 
  class="btn" >
    </a>
   </div>


 </body>
 </html>

"button" code:

  require('functions.php');
  Botones();

"functions" code:

  function Botones(){ 

    $action="";

    switch(isset($_POST))
  {
case isset($_POST['new']):
  $action=$_POST['new'];
break;

case isset($_POST['agregar']):
  $action=$_POST['agregar'];
break;

case isset($_POST['pp']):
 $action=$_POST['pp'];
 break;

case isset($_POST['nv']):
 $action=$_POST['nv'];
 break;

case isset($_POST['adminStock']):
 $action=$_POST['adminStock'];
 break;

case isset($_POST['editar']):
 $action=$_POST['editar'];
 break;



  }
   switch ($action) {
       case 'new':

    header("location: ./agregarProd.php");

    break;

case 'Agregar':

    //Vuelvo a conectar con la base de datos, es el mismo codigo que en 
    conexion.php
        session_start();    
        $usuario = "root";
        $servidor = "localhost";
        $basededatos = "stock_manager";
        $conexion = mysqli_connect("$servidor", "$usuario", "", 
     "$basededatos");

        if (!$conexion) {
            echo "Error: No se pudo conectar a MySQL." . PHP_EOL;
            echo "errno de depuración: " . mysqli_connect_errno() . PHP_EOL;
            echo "error de depuración: " . mysqli_connect_error() . PHP_EOL;
            exit;
        }




        if (isset($_POST['nombreProd']) && isset($_POST['stockProd'])) {

            $nombreProd= mb_strtolower($_POST['nombreProd']);


            $stockProd=$_POST['stockProd'];

            $_SESSION['msg']="Se agrego el producto correctamente";



        }

        $db = mysqli_select_db( $conexion, $basededatos ) or die ( "Upps! no 
         se ha podido conectar a la base de datos");

        $qp="SELECT nombreProd FROM productos WHERE nombreProd 
      ='$nombreProd'; ";
        require("conexion.php");
        $con=conectar();
        $stmt= $con-> prepare($qp);
        $result = $stmt->execute();
        $cuenta= $stmt->rowCount();
            if ($cuenta==0) {



            $query="INSERT INTO productos ('nombreProd','stockProd') VALUES 
           ('$nombreProd','$stockProd');";
            $resultado = mysqli_query( $conexion, $query ) or die ( "Algo ha 
            ido mal en la consulta a la base de datos");

            header("location: ./adminStock.php");

        }else { 


            ?>

            <script language="JavaScript" type="text/javascript"> setTimeout 
        ( alert("este texto es el que modificas"),0); </script>




         <?php
     }
    
asked by mariano1424 04.04.2018 в 23:00
source

1 answer

2

I have reviewed the code, so I see questions if the product exists after sending the form, the correct thing is to ask before sending it but well, in this case you can do the following; Once I sent you the "Repeated Product" alert you can put a line below to go back to the previous page where you had your data to modify them:

window.history.back();

This line of javascript sends you to the previous page. Place it below the alert when the product is repeated.

<script language="JavaScript" type="text/javascript"> 
    setTimeout( alert("este texto es el que modificas"),0); 
    window.history.back();
</script>

I hope you find it helpful, greetings.

    
answered by 04.04.2018 / 23:29
source