如何使用PHP将值添加到mysql数据库 [英] How to add values to a mysql database with PHP
本文介绍了如何使用PHP将值添加到mysql数据库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我现在可以将值添加到数据库中,但只能添加字符串,因为当我尝试添加数字时,SQL会自动将值设置为0,我不知道为什么...
我尝试过的事情:
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...
What I have tried:
<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>
推荐答案
titulo_libro = null ;
titulo_libro = null;
autor_libro = 空;
autor_libro = null;
tema_libro = 空;
tema_libro = null;
这篇关于如何使用PHP将值添加到mysql数据库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文