1

I'm new on this part of develop on PHP

and I do a insert on MYSQL used JS and PHP

but I don't know what not work

I have the indes with the form but when I click on the button it does not insert me, it really does nothing

this is my code-> ---form---

<?php
include_once('class/productos.php');
$p = new Producto('','',0,'');
$p->Obtener();
$c = 3;
?>
<body>
    <h1 align="center">AGREGAR ITEM</h1>
 <table width="70%" border="1px" align="center">
<form>
<center>
 <br>
  <th><label>ID:<input type="text" id="id" name="id" value="<?php echo $p->id;?>"></label></th>
 <th><label>Nombre:<input type="text" id="nom" name="nom" value="<?php echo $p->nom;?>"><label></th>
 <th><label>Cantidad:<input type="text" id="cant" name="cant" value="<?php echo $p->cant;?>"><label></th>
 <th><label>Item:<input type="text" id="Item" name="Item" value="<?php echo $p->Item;?>"><label></th>
 <th><label><input type="button" name="op" id="op_<?php echo $c;?>" onclick="agregar(<?php echo $c;?>);" value="Agregar"><label></th>
 </br>
</center>
</form>
</table>
</body>

------function JS------

var x = $(document).ready(Ini);

function Ini(){
 $("#load").load("listados.php");
}

function Llamar(n){
 $.get("modificar.php",{id:n},function(data){
  $("#datos").html(data);
 });
}

function Agregar(n){
 $.get("agregar.php",{op:btn,id:idi,nom:nomb,cant:canti,Item:Item},function(data){
  $("#agregar").html(data);
 });
}

function Agregar(n){
 var btn = $("#op_"+n).val();
 var idi = $("#id").val();
 var nomb = $("#nom").val();
 var canti = $("#cant").val();
 var Item = $("#Item").val();
 $("#agregar").html('<img src="loading.gif">');
 $("#agregar").fadeTo("slow",0.1);
 $.get("operaciones.php",{op:btn,id:idi,nom:nomb,cant:canti,Item:Item});
}

-----index----

<!DOCTYPE html>
<html>
<head>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="function.js"></script>
</head>
<body>
<div id="encabezado">
<!--casa moa-->
</div>
<!--Div con el contenido-->
<div id="wrapper">
<center>
<h1>CASA MOA</h1></center>
<p>Aquí va algún texto y más abajo irían imágenes</p>
</div>
<div style='height: 220px; width: 220px; border: 3px'>

<!--Menú-->
<ul>
<li>Inicio</li>
<li>Clientes</li>
<li>Inventario</li>
<li>Nuevo Item</li>
<li>Vendedores</li>
<li>Venta</li>
<li>CuentasXPagar</li>
</ul>
</div>
<div style='height: 220px; width: 1500px; border: 3px'>
<div id="agregar"></div>

</div>
</body>
</html>

--------select the operacion------

<?php
include_once('class/productos.php');
$p = new Producto($_REQUEST['id'],$_REQUEST['nom'],$_REQUEST['cant'],$_REQUEST['Item']);

$op = $_REQUEST['op'];

switch($op){
 case 'Modificar':
 $p->Modificar();
 break;
 
 case 'Eliminar':
 $p->Eliminar();
 break;
 
 case 'Agregar':
 $p->Agregar();
 break;
  
}
sleep(1);
?>

--------conexion whit DB-------

<?php
include_once('conexion.php');

class Producto{

 var $id;
 var $nom;
 var $cant;
 var $Item;

 function Producto($id=0,$nom='',$cant=0,$Item=''){
  $this->id=$id;
  $this->nom=$nom;
  $this->cant=$cant;
  $this->Item=$Item;
 }
   function Agregar(){
    $con = new ConexionBD();
    $sql = "INSERT INTO productos (id, nom, cant, Item) VALUES ('".$this->id."', '".$this->nom."', '".$this->cant."', '".$this->Item."')";

  return $con->ejecutarsentencia($sql);
    }
}
?>
  • your php code is **vulnerable to sqk injection** please take a look at https://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php and somehow i am missing the ajax part – nbk Jun 14 '20 at 20:47
  • You have two functions with the same name in your js script. Also keep in mind javascript is a case sensitive language. – RST Jun 14 '20 at 22:51

1 Answers1

0

[try to see in your browser what is wrong ..If you have firefox use debug type, CTRL+i ...and view in tab rede , and after in response.

enter image description here