Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I can add values to my database now, but only strings, because when I try to add numbers, SQL automatically put that values to 0 and I don't know why...
Anyone can help ??

What I have tried:

HTML
<html>
   <head>
	<meta charset="utf-8">
	<link rel="shortcut icon" href="favicon.ico"/>
	<center>
	<title>Añadir Libro</title>
   </head>
   <body>
   <body background="backg.jpg">
	<p style="color:black">Desde aqui podremos <ins>añadir libros
	</ins> a la libreria, especificando todas sus caractericticas.
	</p>
      <?php
      $titulo_libro = null;
      $autor_libro = null;
      $tema_libro = null;
      $paginas_libro = null;
      $tema_libro = null;
      $paginas_libro = null;
      $formatoUno_libro = null;
      $formatoDos_libro = null;
      $formatoTres_libro = null;
      $estado_libro = null;
         if (!($link=mysqli_connect("localhost","root","")))  
         {  
         echo "Error conectando a la base de datos.";  
         }  
         if (!mysqli_select_db($link, "libreria1617"))
         {
         echo "Error seleccionando la base de datos.";
                  } else if(isset($_POST['titulo_libro'])){
                  $titulo_libro = $_POST['titulo_libro'] ?? '';
                  $autor_libro = $_POST['autor_libro'] ?? '';
         			$tema_libro = $_POST['tema_libro'] ?? '';
         			$paginas_libro = $_POST['numeroPaginas'] ?? '';
         			$formatoUno_libro = $_POST['cartone'] ?? '';
         			$formatoDos_libro = $_POST['rustica'] ?? '';
        			   $formatoTres_libro = $_POST['tapadura'] ?? '';
         			$estado_libro = $_POST['estado'] ?? '';

                  $sql=mysqli_query($link, "INSERT INTO `libro` (`Titulo`, `Autor`, `Tema`, `NumPaginas`, `FormatoUno`, `FormatoDos`, `FormatoTres`, `Estado`)
                  VALUES ('$titulo_libro', '$autor_libro', '$tema_libro', '$paginas_libro', '$formatoUno_libro', '$formatoDos_libro', '$formatoTres_libro', '$estado_libro')") 
                  or die('Error: ' . mysqli_error($link));
                  if(! $sql ) {
                     die('Error al añadir el libro: ' . mysqli_error($link));
                  }
                  echo "Libro añadido: ".'<br />';
                  mysqli_close($link);
         	} else {
            ?>     
               <form method="post" action="annadirlibro.php">
                  <table width = "400" border = "0" cellspacing = "1" 
                     cellpadding = "2">
                  
                  	
                     <tr>
                        <td width = "100">Titulo del libro</td>
                        <td><input name = "titulo_libro" type = "text" 
                           id = "titulo_libro"></td>
                     </tr>
                  
                     <tr>
                        <td width = "100">Autor del libro</td>
                        <td><input name = "autor_libro" type = "text" 
                           id = "autor_libro"></td>
                     </tr>
                  
                     <tr>
                        <td width = "100">Tema del libro</td>
                        <td><input name = "tema_libro" type = "text" 
                           id = "tema_libro"></td>
                     </tr>

                     <tr>
                        <td width = "100">Numero de paginas del libro</td>
                        <td><input name = "paginas_libro" type = "text" 
                           id = "paginas_libro"></td>
                     </tr>
                
                     <tr>
                        <td width = "100">Formato Cartone (1 SI, 0 NO)</td>
                        <td><input name = "formatoUno_libro" type = "text" 
                           id = "formatoUno_libro"></td>
                     </tr>
                     <tr>
                        <td width = "100">Formato Rustica (1 SI, 0 NO)</td>
                        <td><input name = "formatoDos_libro" type = "text" 
                           id = "formatoDos_libro"></td>
                     </tr>

                     <tr>
                        <td width = "100">Formato TapaDura (1 SI, 0 NO)</td>
                        <td><input name = "formatoTres_libro" type = "text" 
                           id = "formatoTres_libro"></td>
                     </tr>
                     <tr>
                        <td width = "100">Estado del libro (0 Normal, 1 Reedicion)</td>
                        <td><input name = "estado_libro" type = "text" 
                           id = "estado_libro"></td>
                     </tr>
                     <tr>
                        <td width = "100"> </td>
                        <td>
                           <input name = "button" type = "submit" id = "button" value = "Añadir libro">
                        </td>
                     </tr>
                  </table>
               </form>
                <?php } ?>
      </center>
   </body>
</html>
Posted
Updated 22-May-17 22:45pm

1 solution

Just as with your existing query that passes strings but without enclosing the numberic parameter variables by quotes:
PHP
$query = "INSERT INTO tablename (`strparam`, `numparam`) VALUES ('$strparam', $numparam)";
$result = mysqli($query);
See also PHP: Prepared Statements - Manual[^].

You may also prepare the statement and bind parameters. See PHP: mysqli::prepare - Manual[^] and PHP: mysqli_stmt::bind_param - Manual[^].

Using this is always the best choice because it avoids conversion problems by passing as value rather than strings (floating point values are not rounded, local settings for date, time, decimal point, and currency does not care).
 
Share this answer
 
Comments
Member 13208052 25-May-17 3:12am    
Sorry but I don't understand what you are trying to say...
Member 13208052 29-May-17 13:25pm    
???
Jochen Arndt 30-May-17 3:26am    
You don't have to quote the parameter value.

If you still get zero added, check the value of your numerical parameter (e.g. by printing the SQL command string).

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900