I am new on the php but I am trying to build a page that produces after having read something from the database the inherent checkboxes, but when these are printed with echo the formatting it is not the desired one producing therefore of the errors for the carriage return. Here is the php
<?php
$conn = mysqli_connect("localhost", "username", "password", "database_name");
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$query="SELECT * FROM ingrediente";
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
$name = $row["id_ingrediente"];
$value = $row["immagine"];
$etichetta = $row["nome"];
$prezzo = $row["costo_unitario"];
echo "\n";
echo "<input type='checkbox' name=$name value='$value' onchange=\"add_ing2($name,'$value','$etichetta',$prezzo,this);\">$etichetta" ;
echo "\n";
echo "<input type='number' name='$etichetta' style='border:0px; text-align:right;background:transparent; height:auto;' value=$prezzo readonly> €";
echo "\n";
echo "<br><br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
Here is the produced html
<input type='checkbox' name=1 value='/images/ingredienti/pomodoro.png' onchange="add_ing2(1,'/images/ingredienti/pomodoro.png','Pomodoro',0.3,this);">Pomodoro
<input type='number' name='Pomodoro' style='border:0px; text-align:right;background:transparent; height:auto;' value=0.3 readonly> €
<br><br>
<input type='checkbox' name=2 value='/images/ingredienti/formaggio.png
' onchange="add_ing2(2,'/images/ingredienti/formaggio.png
','Formaggio',0.2,this);">Formaggio
<input type='number' name='Formaggio' style='border:0px; text-align:right;background:transparent; height:auto;' value=0.2 readonly> €
<br><br>
<input type='checkbox' name=3 value='/images/ingredienti/hamburger.png
' onchange="add_ing2(3,'/images/ingredienti/hamburger.png
','Hamburger',1,this);">Hamburger
<input type='number' name='Hamburger' style='border:0px; text-align:right;background:transparent; height:auto;' value=1 readonly> €
<br><br>
As you can see the correct way to print has been applied only to the first element (Pomodoro (line 1:2 in html code)). Since this causes me more than few problems when then use the checkboxes, as can I resolve?
Thank you so much
Nothing wrong with PHP - the data you're pulling out of the database is just bad. Some of your image paths have a new-line character at the end.
You should ultimately fix the data (validating user input, adjusting scripts that pull it in automatically, etc.), but you can apply trim()
to the values to strip off the extra character(s).