gpt4 book ai didi

php - 我想插入到一个表中,从 php 中同一数据库中的另一个表获取的数据

转载 作者:行者123 更新时间:2023-11-29 18:32:51 29 4
gpt4 key购买 nike

我为两个表创建一个关系,一个包含项目,另一个包含项目的类别。我可以使用类别表中的值填充选项标签。现在我想将从选项列表中选择的值发布到项目表中的category_id(我已将其设置为int,就像类别表中的id一样),但我在添加其他信息时得到category_id空白。我怎样才能解决这个问题?代码如下。

<?
session_start();
$_SESSION['message'] = "";

$mysqli = new mysqli('localhost', 'root', '', 'auction');
if($_SERVER['REQUEST_METHOD'] == 'POST') {
$item_name = $mysqli->real_escape_string($_POST['item_name']);
$category_id = $mysqli->real_escape_string($_POST['cat']);
$item_description = $mysqli->real_escape_string($_POST['item_description']);
$item_image_path = $mysqli->real_escape_string('Images/item_img/' . $_FILES['item_image']['name']);

//make sure file is of image type
if(preg_match("!image!", $_FILES['item_image']['type'])) {
if(copy($_FILES['item_image']['tmp_name'], $item_image_path)) {
$_SESSION['item_name'] = $item_name;
$_SESSION['cat'] = $category_id;
$_SESSION['item_description'] = $item_description;
$_SESSION['item_image'] = $item_image_path;
//inserting into the database
$sql = "INSERT INTO items (item_name,category_id, item_image, item_description)VALUES('$item_name', '$category_id', '$item_image_path','$item_description')";
if($mysqli->query($sql) === true) {
$_SESSION['message'] = "Item Upload Successful!";
} else {
$_SESSION['message'] = "file upload failed";
}
} else {
$_SESSION['message'] = "file copying failed";
}
} else {
$_SESSION['message'] = "please upload gif, jpg, png";
}

}

$result = $mysqli->query("SELECT * FROM items ORDER BY rand() LIMIT 10") or die($mysqli->error);

?>

<html>

<head>
<title>Upload item</title>
<link rel="StyleSheet" href="Bootstrap/css/bootstrap.main.css">
<link rel="StyleSheet" href="Bootstrap/css/bootstrap.min.css">
<link rel="StyleSheet" href="style.css">
<!--for countdown timer-->
<script type="text/javascript">
setInterval(function() {
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "response.php", false);
xmlhttp.send(null);
document.getElementById("timer").innerHTML = xmlhttp.responseText;

}, 1000);
</script>
</head>

<body>
<div>
<!--to display records from database-->
<div class="row col-sm-12 c-head mar-pad">
<?php
$A = 0;
while($auction = $result->fetch_assoc()):
?>
<div class="grid ">
<h4><?= $auction['item_name'] ?></h4>
<img src='<?= $auction[' item_image '] ?>' class='img-responsive'>
<span id="timer" class="timer"></span>
<button class="c-button" name='bid'>Bid Now!</button>
</div>
<?php
if($A % 4 == 0)
echo "<br/>";
$A++;
endwhile;
?>
</div>

<!--for file upload form-->
<form class="form-horizontal" role="form" action="auction_upload.php" method="POST" enctype="multipart/form-data">
<h1><?=$_SESSION['message']?></h1>
<div class=" form-group">
<label class="control-label col-sm-2">Item Name:</label>
<div class="col-sm-8">
<INPUT type="text" class="form-control" name="item_name" required/>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2">Category:</label>
<div class="col-sm-8 ">

<select class='form-control'>
<?php
$mysqli = new mysqli('localhost','root','','auction');
$result1 = $mysqli->query("SELECT * FROM `categories`");

while ($row = mysqli_fetch_array($result1)):;?>
<option name="cat" value="<?=$row[0];?>">
<?=$row[1];?>
</option>

<?php endwhile;?>
</select>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2">Item Image:</label>
<div class="col-sm-8">
<INPUT type="file" class="form-control" name="item_image" accept="image/*" required/>
</div>
</div>
<div class="form-group">
<label class="control-label col-sm-2">Item Description:</label>
<div class="col-sm-8">
<textarea class="form-control" name="item_description" required>
</textarea>
</div>
</div>

<div class="form-group">
<div class="col-sm-offset-2 col-sm-8">
<button type="submit" class="btn btn-default" name="upload">Upload</button>
</div>
</div>
</form>
</div>
</body>
</html>

最佳答案

您的 select 元素没有 name:

<select class ='form-control' >

因此该值根本没有发送到服务器。为了在表单帖子中包含该元素的值,表单元素需要一个 name (这是键/值对中的键)。

由于您正在查找 $_POST['cat'] ,因此名称将为“cat”:

<select class ='form-control' name="cat" >

关于php - 我想插入到一个表中,从 php 中同一数据库中的另一个表获取的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45550532/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com