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

9
  • 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. Commented Jun 19, 2017 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? Commented Jun 19, 2017 at 16:08
  • Well, that's a different question and I have no idea what line 22 is. Did you put the print_r()? Commented Jun 19, 2017 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. Commented Jun 19, 2017 at 16:19
  • What is line 22? Commented Jun 19, 2017 at 16:21

2 Answers 2

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!

Sign up to request clarification or add additional context in comments.

1 Comment

Yes, it is true, I realized only the error of writing, thank you for responding.
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!

3 Comments

Why would you unaccept my answer when you credit me for the solution?
Sorry for this, this page do not let me accept your solution :c
Not true, because you just unaccepted mine and accepted yours. Any how, c'est la vie.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.