我为两个表创建一个关系,一个包含项目,另一个包含项目的类别。我可以使用categories表中的值填充选项标记。现在我想在items表中将选项列表中选择的值发布到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>
答案 0 :(得分:0)
您的select
元素没有name
:
<select class ='form-control' >
因此该值根本没有发送到服务器。为了在表单post中包含该元素的值,表单元素需要name
(这是键/值对中的键)。
由于您正在查看$_POST['cat']
,因此名称将为“cat”:
<select class ='form-control' name="cat" >