0

(Excuse me if I use a translator so that I can understand this problem).

I have been practising for almost a week for the checkbox case, using Procedural MySqli, which I can not understand if one selects a type of coffee as seen in these codes, choosing one or many and before proceeding, I investigated if i have the same problem as mine, I can not find (two id), the idea is that I must insert the database using $ _POST, I have been trying several methods and none of them works.

pruebacafe2.php

<h1>Cafe</h1>
<form method="POST" action="anadircafe.php">
<p><b>Selección de Cafe</b></p>
Cafe:<br/>
<input type="checkbox" name="id_cafe[]" value="1"/> Nescafe <br/>
<input type="checkbox" name="id_cafe[]" value="2"/> Americano <br/>
<input type="checkbox" name="id_cafe[]" value="3"/> Macciato <br/>
<input type="checkbox" name="id_cafe[]" value="4"/> Doble <br/>
<input type="checkbox" name="id_cafe[]" value="5"/> Cortado <br/>
<input type="checkbox" name="id_cafe[]" value="6"/> Capuccino <br/>
<input type="checkbox" name="id_cafe[]" value="7"/> Irlandes <br/>
<input type="checkbox" name="id_cafe[]" value="8"/> Cafe Solo<br/>
Usuario: <br/>
<input type="checkbox" name="id_usuario[]" value="1"/> Fslynx <br/>
<input type="checkbox" name="id_usuario[]" value="2"/> Guts <br/>
<input type="checkbox" name="id_usuario[]" value="3"/> Otter <br/>
<input type="checkbox" name="id_usuario[]" value="4"/> Sebastian <br/>
<input type="checkbox" name="id_usuario[]" value="5"/> Julian <br/>
<input type="checkbox" name="id_usuario[]" value="6"/> Claire<br/> 
Precio: <input type="text" name="precio"><br>
IVA: <input type="text" name="iva"><br>
Total: <input type="text" name="total"><br>
<input type="submit" name="prueba" value="enviar">
</form>

anadirimplode.php

<?php
$msg = $id_cafe = $id_usuario = $precio = $iva = $total = NULL;

if(isset($_POST['enviar'])){
 $id_cafe = $_POST['id_cafe'];
 $id_usuario = $_POST['id_usuario'];
 $precio = $_POST['precio'];
 $iva = $_POST['iva'];
 $total = $_POST['total'];
 $caf = implode("", $id_cafe);
 $usu = implode("", $id_usuario);

 if($id_cafe && $id_usuario && $precio && $iva && $total){
 
  $link = mysqli_connect("localhost", "root", "", "cafe");

  if (mysqli_connect_errno()) {
   printf("Fallo conexion: %s\n", mysqli_connect_error());
   exit();
  } 

  $query = mysqli_query($link, "INSERT INTO compra_cafe (id_cafe, id_usuario, precio, iva, total) VALUES ('$id_cafe', '$id_usuario', '$precio', '$iva', '$total')");

  if(!$query) {
   printf("Error: %s\n", mysqli_error($link));
  } else {
   $msg = "Datos insertados";
  }
 }
}
echo $msg;
?>

Table compra_cafe in phpmyadmin

CREATE TABLE `compra_cafe` (
 `id_cafe` int(11) NOT NULL,
 `id_usuario` int(11) NOT NULL,
 `Precio` varchar(30) NOT NULL,
 `IVA` varchar(30) NOT NULL,
 `Total` varchar(30) NOT NULL,
 UNIQUE KEY `id_cafe` (`id_cafe`),
 UNIQUE KEY `id_usuario` (`id_usuario`),
 CONSTRAINT `compra_cafe_ibfk_1` FOREIGN KEY (`id_cafe`) REFERENCES `tipo_cafe` (`id_cafe`) ON DELETE CASCADE ON UPDATE CASCADE,
 CONSTRAINT `compra_cafe_ibfk_2` FOREIGN KEY (`id_usuario`) REFERENCES `usuario` (`Id_usuario`) ON DELETE CASCADE ON UPDATE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=latin1
halfer
  • 19,824
  • 17
  • 99
  • 186
Baku84
  • 1
  • 4
  • 1
    Do yourself a favor, simply place a `print_r($_POST)` or `var_dump($_POST)` in the PHP page which receives the form submission. Fill out your form, submit and look closely at the data printed to the screen. Familiarize yourself with how form data is posted to scripts, including what gets passed and what doesn't. – Jay Blanchard Jun 19 '17 at 15:56
  • I have filled out the form and this error appears: Notice: Array to string conversion in C:\xampp\htdocs\anadirimplode.php on line 22 Notice: Array to string conversion in C:\xampp\htdocs\anadirimplode.php on line 22 Error: Cannot add or update a child row: a foreign key constraint fails (`cafe`.`compra_cafe`, CONSTRAINT `compra_cafe_ibfk_1` FOREIGN KEY (`id_cafe`) REFERENCES `tipo_cafe` (`id_cafe`) ON DELETE CASCADE ON UPDATE CASCADE), I have done something wrong in this query? – Baku84 Jun 19 '17 at 16:08
  • Well, that's a different question and I have no idea what line 22 is. Did you put the `print_r()`? – Jay Blanchard Jun 19 '17 at 16:11
  • I did it in `anadirimplode.php` and it even repeats the same error of line 22, if it is the $query problem that is seen in the code I showed.I do not quite understand this. – Baku84 Jun 19 '17 at 16:19
  • What is line 22? – Jay Blanchard Jun 19 '17 at 16:21
  • The number of the code line that shows the error or the error is in: `$query = mysqli_query($link, "INSERT INTO compra_cafe (id_cafe, id_usuario, precio, iva, total) VALUES ('$id_cafe', '$id_usuario', '$precio', '$iva', '$total')");` – Baku84 Jun 19 '17 at 16:28
  • So - one of the arrays is giving you a problem. Did you do a `print_r()`? – Jay Blanchard Jun 19 '17 at 16:32
  • i did again one more time, using `print_r($_POST)` and it shows to me the array: Array ( [id_cafe] => Array ( [0] => 3 ) [id_usuario] => Array ( [0] => 1 ) [precio] => 60 [iva] => 10 [total] => 70 [prueba] => enviar ) What does this problem refer to?. – Baku84 Jun 19 '17 at 16:43
  • Change `$id_cafe = $_POST['id_cafe']; $id_usuario = $_POST['id_usuario'];` to `$id_cafe = $_POST['id_cafe'][0]; $id_usuario = $_POST['id_usuario'][0];` which gives you the element from the array. – Jay Blanchard Jun 19 '17 at 16:48
  • If an answer solved your problem, consider accepting the answer. Here's how http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work then return here and do the same with the tick/checkmark till it turns green. This informs the community, a solution was found. Otherwise, others may think the question is still open and may want to post (more) answers. You'll earn points and others will be encouraged to help you. *Welcome to Stack!* – Jay Blanchard Jun 19 '17 at 16:49

2 Answers2

1

In order for your submission to work you have to use the name as the identifier in the post array. For example...

if(isset($_POST['enviar'])){

should be

if(isset($_POST['prueba'])){

Because 'prueba' is the name of the submit button:

<input type="submit" name="prueba" value="enviar">

Warning!

Little Bobby says your script is at risk for SQL Injection Attacks. Learn about prepared statements for MySQLi. Even escaping the string is not safe!

Jay Blanchard
  • 34,243
  • 16
  • 77
  • 119
0

The answer was solved thanks to @Jay Blanchard in the comments, for the element from the array, here is the code:

<?php
$msg = $id_cafe = $id_usuario = $precio = $iva = $total = NULL;

if(isset($_POST['prueba'])){
 $id_cafe = $_POST['id_cafe'][0];
 $id_usuario = $_POST['id_usuario'][0];
 $precio = $_POST['precio'];
 $iva = $_POST['iva'];
 $total = $_POST['total'];
 //$caf = implode("", $id_cafe);
 //$usu = implode("", $id_usuario);

 //$caf = array();
 //$usu = array();

 if($id_cafe && $id_usuario && $precio && $iva && $total){
 
  $link = mysqli_connect("localhost", "root", "", "cafe");

  if (mysqli_connect_errno()) {
   printf("Fallo conexion: %s\n", mysqli_connect_error());
   exit();
  } 

  $query = mysqli_query($link, "INSERT INTO compra_cafe (id_cafe, id_usuario, precio, iva, total) VALUES ('$id_cafe', '$id_usuario', '$precio', '$iva', '$total')");

  if(!$query) {
   printf("Error: %s\n", mysqli_error($link));
  } else {
   $msg = "Datos insertados";
  }
 }
}
echo $msg;
?>

Hope it helps with the same problem for both arrays and i hope this site grows more for the community Many thanks!

Baku84
  • 1
  • 4