使用PHP插入基于单选按钮选择的图像MYSQL
我有一个网站。在这个网站上,我可以上传图像,然后放入我的SQL数据库。从这里,我从数据库中选择图像,并将其作为缩略图显示在照片库中,当单击图像时,它会显示一个大版本,您可以在其中投票/喜欢和评论等 现在我要做的是制作3个分类页面,基本上是显示缩略图的照片库的3倍 我的数据库中有3个不同的表,在其中插入图像。 所以我复制了照片库3x和数据库3x中的原始上传表。 无论如何,我不想为每个photo gallery.php文件创建3个upload.php文件 我想做的是在我的上传页面上有3个单选按钮选择,在那里做出选择后,图像被上传到匹配的数据库表中(photo1、Photo2或Photo3) 我一直在尝试用函数等来实现这一点,但我就是无法让它工作,我可能正在做一些非常简单、非常愚蠢的事情 这是我为单选按钮和获取图像所用的代码:使用PHP插入基于单选按钮选择的图像MYSQL,php,mysql,image,radio-button,Php,Mysql,Image,Radio Button,我有一个网站。在这个网站上,我可以上传图像,然后放入我的SQL数据库。从这里,我从数据库中选择图像,并将其作为缩略图显示在照片库中,当单击图像时,它会显示一个大版本,您可以在其中投票/喜欢和评论等 现在我要做的是制作3个分类页面,基本上是显示缩略图的照片库的3倍 我的数据库中有3个不同的表,在其中插入图像。 所以我复制了照片库3x和数据库3x中的原始上传表。 无论如何,我不想为每个photo gallery.php文件创建3个upload.php文件 我想做的是在我的上传页面上有3个单选按钮选择
$titel = "Image";
$query = "SELECT * FROM `i268296_studie`.`fotos` ORDER BY foto_ID DESC";
$result = mysqli_query($conn, $query) or die("query error " . mysqli_error($conn) );
$fotos = array();
//create array from images in database
while($data = mysqli_fetch_assoc($result))
{
$fotos[] = array('src' => $data['src'], 'id' => $data['foto_ID']);
}
?>
<section id="upload">
<form method="post" action="upload.php" enctype="multipart/form-data">
<label for="bestand">Upload image:</label><br><br>
<input type="file" name="bestand" id="file"><br><br>
<label for="categorie"> Categorie: </label>
<input type="radio" name="cat" value="cat1">Portrait
<input type="radio" name="cat" value="cat2">Landscape
<input type="radio" name="cat" value="cat3">Other
<input type="submit" name="submit" value="Upload">
</form>
</section>
<?php
}
?>
<section class="images">
<?php
//show image thumbnails in photogallery
foreach($fotos as $foto)
{
?>
<a href="fotoinfo.php?foto_ID=<?php echo $foto['id'];?>"><img class="image" src="<?php echo 'upload/thumb/t_'.$foto['src'];?>"></a>
<?php
}
?>
</section>
当我不选择单选按钮,而是直接上传时,我只得到1个错误:
Undefined index: cat
然后上传到第三类
编辑2:
更改了包含全局$conn的函数
1 function category($cat, $titel, $fileName) {
2 global $conn;
3 $query = "INSERT INTO `i268296_studie`.`$cat` (`titel`, `src`) VALUES ('$titel', '$fileName')";
4 $result = mysqli_query($conn, $query) or die("query error " . mysqli_error($conn) );
5 }
1: you need these variables from the context calling the function
2: you need the global $conn variable in the function context to be able to run the query
3: use a function parameter for the table to upate call
4: it would be better to return results instead of breaking inside the function
调用代码如下所示:
if ($answer == "cat1") {
category("fotos", $titel, $fileName); // or "foto2", $titel, $fileName .....
}
请注意有关注入漏洞的评论。
也请阅读以下内容:您很容易受到攻击
addslashes()
在预防这些方面的作用与茶匙清除纽约暴风雪的作用一样。您是否考虑过将它们全部保存在同一个表中,并添加一个名为“category”的列,在其中您可以指定图像与哪个类别关联?@Marc B哈哈好的,这其实并不重要,因为网站不会向公众开放,它更像是一个学校的测试,我需要在那里实施某些事情。到目前为止,我已经做了所有的事情,但不知何故,我不能去工作,尽管我已经尝试了好几件事。我真的很喜欢用单选按钮来做选择,而不是每个按钮都有一个单独的上传文件gallery@VictorKilo事实上,我没有想太多,因为不知怎么的,这对我来说似乎是最容易的^^你有什么错误?最可能在类别()的上下文中category3()
函数您看不到$conn
变量,请立即尝试,谢谢您的帮助。我真的很感激!我没有得到任何错误时,选择一个类别,然后上传它。无论如何,它都不会显示图像或将图像上载到数据库中。我假设我需要声明$cat并将其链接到单选按钮?(很抱歉响应太晚,因为这里实在太晚了,所以我不得不快速淋浴^^^)它现在上载了一个图像,但实际上没有加载图像(我看到一个损坏的图像),并且在上载时出错:查询中的未定义变量:fileName。这不应该发生,因为它写得正确等等,我认为有;来源有问题。在我的原始数据库中,它填写了源代码,现在它是空的,无论如何,我要感谢您的帮助!如果你有一个源问题的解决方案,我将采取它!否则,我会尝试自己解决,并且仍然感谢您的帮助:)关于未定义变量:fileName
我的代码段(fileName fileName)中有一个打字错误,请参阅更新的代码(第1行);关于源代码中的其他问题,您当然可以创建更多问题。你需要对你必须得到更好答案的问题更具体一点。欢迎来到stackoverflow
1 function category($cat, $titel, $fileName) {
2 global $conn;
3 $query = "INSERT INTO `i268296_studie`.`$cat` (`titel`, `src`) VALUES ('$titel', '$fileName')";
4 $result = mysqli_query($conn, $query) or die("query error " . mysqli_error($conn) );
5 }
1: you need these variables from the context calling the function
2: you need the global $conn variable in the function context to be able to run the query
3: use a function parameter for the table to upate call
4: it would be better to return results instead of breaking inside the function
if ($answer == "cat1") {
category("fotos", $titel, $fileName); // or "foto2", $titel, $fileName .....
}