Array does not store the full value of my DB if it has a space

0

Inside my DB I have the following information:

MariaDB [agenda]> select * from materia;
+-----------+--------------------------------------+
| idmateria | materia                              |
+-----------+--------------------------------------+
|         1 | Matemáticas                          |
|         2 | Física                               |
|         3 | Química                              |
|         4 | Ética y Valores                      |
|         5 | Introducción a las ciencias sociales |
|         6 | Taller de lectura y redacción        |
|         7 | Inglés                               |
|         8 | Informática                          |
+-----------+--------------------------------------+
8 rows in set (0.00 sec)
MariaDB [agenda]> SHOW CREATE TABLE materia;
+---------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| Table   | Create Table                                                                                                                                                                                                                                |
+---------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
| materia | CREATE TABLE 'materia' (
  'idmateria' int(11) NOT NULL AUTO_INCREMENT,
  'materia' varchar(45) COLLATE utf8_spanish_ci NOT NULL,
  PRIMARY KEY ('idmateria')
) ENGINE=InnoDB AUTO_INCREMENT=9 DEFAULT CHARSET=utf8 COLLATE=utf8_spanish_ci |
+---------+---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

I made a connection to my DB with a query to load the records. I have the file separated from my main code:

    <?php 

try {
    $conexion = new PDO('mysql:host=127.0.0.1;dbname=agenda;charset=utf8','root','');
    /*echo "Conexion OK"."<br />";*/
} catch (PDOException $e) {
    echo "Error: ". $e->getMessage();
}

$asignaturas = $conexion->query('SELECT * FROM materia');

?>

In this section of code where I bring the information of a DB to fill a select for after sending the information to another page.

<?php 

require_once('fn/conexion.php');

$semanas = ['1','2','3','4','5','6','7','8','9','10','11','12','13'];
$temas = ['1','2','3','4','5'];
$propositos = ['1','2','3','4','5'];
$recursos = ['1','2','3','4','5'];

 ?>

<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <title>Agenda</title>
</head>
<body>
    <form action = "Agenda_W3.php" method="post">
<hr>
        <p>Selecciona que materia quieres trabajar:
            <select name="materia" id="">
                <option selected disabled hidden style='display: none' value=''></option>
                <?php 
                    foreach ($asignaturas as $asignatura) {
                    echo "<option value=".$asignatura['idmateria'].">".$asignatura['materia']."</option>";
                    }
                 ?>
            </select>
        </p>

Adding var_dump

<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <title>Agenda</title>
</head>
<body>
    <form action = "Agenda_W3.php" method="post">
<hr>
        <p>Selecciona que materia quieres trabajar:
            <select name="materia" id="">
                <option selected disabled hidden style='display: none' value=''></option>
                object(PDOStatement)#2 (1) {
  ["queryString"]=>
  string(21) "SELECT * FROM materia"
}
<option value=1>Matemáticas</option><option value=2>Física</option><option value=3>Química</option><option value=4>Ética y Valores</option><option value=5>Introducción a las ciencias sociales</option><option value=6>Taller de lectura y redacción</option><option value=7>Inglés</option><option value=8>Informática</option>array(4) {
  ["idmateria"]=>
  string(1) "8"
  [0]=>
  string(1) "8"
  ["materia"]=>
  string(12) "Informática"
  [1]=>
  string(12) "Informática"
}
object(PDOStatement)#2 (1) {
  ["queryString"]=>
  string(21) "SELECT * FROM materia"
}
            </select>
        </p>

But when I print the array I realize that the full name of the record is not stored, if there is a space it only shows the first word.

<?php 
    require_once('fn/conexion.php');
    var_dump($_POST);
        echo '<hr>';

    $bloques = ['I','II','III','IV','V','VI','VII','VIII','IX','X','XI','XII','XIII'];

    /*Almacena el digito en una variable para usarse en un ciclo*/
    $asignatura = $_POST['materia'];
    $no_proposito = $_POST['no_proposito'];
    $no_tema1 = $_POST['no_tema1'];
    $no_recurso1 = $_POST['no_recursos1'];
    $no_tema2 = $_POST['no_tema2'];
    $no_recurso2 = $_POST['no_recursos2'];
 ?>

<!DOCTYPE html>
<html lang="es">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title>Agenda W3</title>
    <link rel="stylesheet" href="https://www.w3schools.com/w3css/4/w3.css">
</head>
<body>

    <div class="w3-container agenda">
        <p>Agenda para la materia: <?php echo $asignatura;?> </p>
        <form action="genera_html.php" method="post" target="_blank">
            <div class="w3-row w3-round bloque">
                <div class="w3-col w3-container ">

What causes this and how can I solve it? Maybe I'm doing it wrong, but I should store in the array the complete record, in this case the word "Ethics and Values"

    
asked by Marcos Rugerio 15.11.2018 в 01:17
source

1 answer

0

I found the solution to my problem, the detail is in the quotes " " of <option value> , I forgot to add the quotes of the tag <option value=""></option> and also escape the character so that the value is stored.

Original code:

<?php 
    foreach ($asignaturas as $asignatura) {
        echo "<option value=".$asignatura['materia'].">".$asignatura['materia']."</option>";
    }
 ?>

Code escaping quotation marks and escaping them

<?php 
    foreach ($asignaturas as $asignatura) {
        echo "<option value=\"".$asignatura['materia']."\">".$asignatura['materia']."</option>";
    }
 ?>

When the quotes escape, the value is saved in the array without problems.

array(8) {
  ["materia"]=>
  string(37) "Introducción a las ciencias sociales"
  ["semana"]=>
  string(1) "4"
  ["no_proposito"]=>
  string(1) "1"
  ["no_tema1"]=>
  string(1) "1"
  ["no_recursos1"]=>
  string(1) "1"
  ["no_tema2"]=>
  string(1) "1"
  ["no_recursos2"]=>
  string(1) "1"
  ["submit"]=>
  string(6) "Enviar"
}
    
answered by 15.11.2018 / 21:45
source