php - INSERT INTO 不会在数据库中写入任何内容
问题描述
基本上,我们正在尝试在数据库中添加一些值。我们使用 GET 命令来获取名为“valeur”的值并将其写入数据库。但是它不起作用,值不会添加到数据库中
<?php
try
{ // connection a la base de donnees
// connection to mySQL
$bdd = new
PDO('mysql:localhost;dbname=test1', 'root', '');
}
catch(Exception $e) //in case of error, display it and stop everything
{
die('Erreur : '.$e->getMessage());
}
if (isset($_GET['temp1'])) // test if the variable exists
{
$_GET['temp1'] = floatval($_GET['temp1']);
echo ('donnee ' .$_GET["temp1"]. ' en cours d\'ecriture</br>');
$bdd->exec('INSERT INTO temp (valeur) VALUES('.$_GET["temp1"].')');
echo ('donnee ' .$_GET['temp1']. ' ecrite!');
}
?>
如果我们在(在我们的例子中)http://localhost/test1/add.php?temp1=(thevalue)中输入一个值,那么它应该被插入到我们名为 temp 的表中的“valeur”列中。相反,它不写任何东西。
编辑:我们使用 PHP 5.6.19 和 MySQL 5.7.11 和 WAMPserver
EDIT2:我终于解决了这个问题,虽然我不知道如何。PHP看起来很有趣
解决方案
当您使用PDO
它时,利用它的一些优势是有意义的——主要是在这种情况下prepared statements
,bound parameters
并使 sql 对恶意用户更加安全。如果您将数据库连接与其余代码分开,您将拥有一个数据库连接,只需在运行时包含它即可快速轻松地在其他地方使用,因此下面的第一段代码可能是 db 连接文件。
(我看到你在发布这个之前已经自己解决了这个问题......)
<?php
/*******************
dbo-conn.php
*/
try{
$options=array(
PDO::ATTR_CURSOR => PDO::CURSOR_SCROLL,
PDO::ATTR_PERSISTENT => false,
PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true,
PDO::ATTR_EMULATE_PREPARES => true,
PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES \'utf8mb4\' COLLATE \'utf8mb4_general_ci\', @@sql_mode = STRICT_ALL_TABLES, @@foreign_key_checks = 1'
);
$dbuser='root';
$dbpwd='';
$bdd=new PDO( 'mysql:host=localhost;dbname=test1;port=3306;charset=UTF8', $dbuser, $dbpwd, $options );
}catch( PDOException $e ){
exit( $e->getMessage() );
}
?>
On the page that does the database inserts
<?php
try{
# test that the variable is set and available...
if( !empty( $_GET['temp1'] ) ){
# rudimentary check for number
if( !is_numeric( $_GET['temp1'] ) )throw new Exception( sprintf( 'Supplied parameter "%s" does not appear to be a number', $_GET['temp1'] ) );
$valeur = $_GET['temp1'];
# include the db connection
# the path used here depends where the file `dbo-conn.php` is saved
# - this assumes the same directory
require 'dbo-conn.php';
# generate sql & prepared statement
$sql='insert into `temp` ( `valeur` ) values ( :valeur )';
$stmt = $bdd->prepare( $sql );
# check the prepared statement was created ok before attempting to execute it
if( !$stmt ) throw new Exception( 'Failed to prepare sql "INSERT" query'
# bind the placeholder to the supplied user input
$stmt->bindParam( ':valeur', $valeur, PDO::PARAM_STR );
# commit the query
$result = $stmt->execute();
if( !$result )throw new Exception( 'oops! something went wrong' );
# display a message to the user
printf('donnee %s ecrite!', $valeur );
}
}catch( Exception $e ){
exit( sprintf( 'Erreur: %s', $e->getMessage() ) );
}
?>
推荐阅读
- python-3.x - 如何在图像上绘制它们之前合并两个文本
- javascript - 在模块配置中使用 clientScripts
- django - 如何在 django 中使用 xhtml2pdf 以 pdf 格式显示图像
- azure - 在 Azure 虚拟机中使用 iis 托管网站进行域配置
- python - 我怎么能用三重奏异步读取文件的特定行
- angular - Angular Reactiveforms 下拉菜单(多个)
- powershell - 在 PowerShell 的文件夹路径中使用变量
- sql - 获取特定或其他语言的页面数据
- java - 如果我在 IF 条件语句中使用 buffer.get(),是否需要使用 buffer.rewind()?
- c# - 无法在c#中发送带有excel附件的邮件