我想从我的数据库中检索数据以供用户编辑,但我不知道我哪里出错...
下面的代码应该以json格式从数据库中返回信息并将它们插入到现有的文本框中以供用户编辑...
我的问题是,当点击更新按钮时,没有任何效果......浏览器没有响应......
请帮助......我对所有这些
都是新手从数据库中检索数据的代码(fetch.php):
<?php
$connect= mysqli_connect("localhost", "Eben", "Pass1234", "stageupgh");
if (isset($_POST['item_id'])) {
$output= array();
$procedure= "
CREATE PROCEDURE whereItem(IN item_id int(11))
BEGIN
SELECT * FROM items WHERE item_id= item_id;
END;
";
if (mysqli_query($connect, "DROP PROCEDURE IF EXISTS whereItem")) {
if (mysqli_query($connect, $procedure)) {
$query= "CALL whereItem(".$_POST['item_id'].")";
$result= mysqli_query($connect, $query);
while ($row= mysqli_fetch_array($result)) {
$output["itemName"]= $row["name"];
$output['itemDescription']= $row["description"];
$output['stock']= $row["in_stock"];
$output['price']= $row["price"];
}
echo json_encode($output);
}
}
}
&GT;
itemAction.php:
<?php
if (isset($_POST["action"])) {
$output= '';
$connect= mysqli_connect("localhost", "Eben", "Pass1234", "stageupgh");
if ($_POST["action"]=="Add") {
$itemName= mysqli_real_escape_string($connect, $_POST["itemName"]);
$itemDescription= mysqli_real_escape_string($connect, $_POST["itemDescription"]);
$stock= mysqli_real_escape_string($connect, $_POST["stock"]);
$price= mysqli_real_escape_string($connect, $_POST["price"]);
$procedure= "
CREATE PROCEDURE insertItem(IN name varchar(100), description varchar(250), in_stock int(11), price decimal(6,2))
BEGIN
INSERT INTO items(name, description, in_stock, price) VALUES (name, description, in_stock, price);
END
";
if (mysqli_query($connect, "DROP PROCEDURE IF EXISTS insertItem")) {
if(mysqli_query($connect, $procedure)){
$query= "CALL insertItem('".$itemName."', '".$itemDescription."','".$stock."','".$price."')";
mysqli_query($connect, $query);
echo "Data Inserted";
}
}
}
if (isset($_POST['action'])=="Edit") {
$itemName= mysqli_real_escape_string($connect, $_POST["itemName"]);
$itemDescription= mysqli_real_escape_string($connect, $_POST["itemDescription"]);
$stock= mysqli_real_escape_string($connect, $_POST["stock"]);
$price= mysqli_real_escape_string($connect, $_POST["price"]);
$procedure= "
CREATE PROCEDURE updateItem(IN item_id int(11), name varchar(100), description varchar(250), in_stock int(11), price decimal(6,2))
BEGIN
UPDATE items SET name= itemName, description= itemDescription, in_stock= stock, price= price WHERE item_id= item_id;
END;
";
if (mysqli_query($connect, "DROP PROCEDURE IF EXISTS updateUser")) {
if (mysqli_query($connect, $procedure)) {
$query= "CALL updateUser('".$_POST['item_id']."', '".$itemName."', '".$itemDescription."',, '".$stock."', '".$price."')";
mysqli_query($connect, $query);
echo "Data Updated";
}
}
}
}
&GT;
主体:
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label class="sr-only">Item Name:</label>
<input type="text" class="form-control" placeholder="Name" id="itemName" name="itemName" required data-validation-required-message="Please enter the item's." autofocus="true">
<p class="text-danger help-block"></p>
</div>
</div>
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label class="sr-only">Description</label>
<input type="text" class="form-control" placeholder="Item Description" id="itemDescription" name="itemDescription" required data-validation-required-message="Please enter the item's description.">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label class="sr-only">In Stock</label>
<input type="text" class="form-control" placeholder="In Stock" id="stock" name="stock" required data-validation-required-message="How many items are available">
<p class="help-block text-danger"></p>
</div>
</div>
<div class="row control-group">
<div class="form-group col-xs-12 floating-label-form-group controls">
<label class="sr-only">Price (Gh ₵:)</label>
<input type="text" class="form-control" placeholder="Unit Price (Gh ₵:)" id="price" name="price" required data-validation-required-message="How many items are available">
<p class="help-block text-danger"></p>
</div>
</div>
<br>
<div id="success"></div>
<div class="row">
<div class="form-group col-xs-12">
<input type="hidden" name="item_id" id="item_id">
<button type="button" name="action" id="action" class="btn btn-success btn-lg" style="float: right; margin-right: 20px;">Add</button>
</div>
</div>
脚本:
<script type="text/javascript">
$(document).ready(function(){
fetchItems();
function fetchItems(){
var action= "select";
$.ajax({
url: "itemSelect.php",
method: "POST",
data:{action:action},
success: function(data){
$('#itemName').val('');
$('#itemDescription').val('');
$('#stock').val('');
$('#price').val('');
$('#action').val("Add");
$('#result').html(data);
}
})
}
$('#action').click(function(){
var itemName= $('#itemName').val();
var itemDescription= $('#itemDescription').val();
var stock= $('#stock').val();
var price= $('#price').val();
var item_id= $('#itemId').val();
var action= $('#action').text();
if (itemName!='' && itemDescription!='' && stock!='' && price!='') {
$.ajax({
url: "itemAction.php",
method: "POST",
data:{itemName:itemName, itemDescription:itemDescription, stock:stock, price:price, item_id:item_id, action:action},
success:function(data){
alert(data);
fetchItems();
}
});
}else{
alert("All fields are required");
}
});
$(document).on('click', '.update', function(){
var item_id= $(this).attr("#item_id");
$.ajax({
url: "fetch.php",
method: "POST",
data:{item_id:item_id},
dataType:"json",
success:function(data){
$action('#action').text("Edit");
$('#itemId').val(item_id);
$('#itemName').val(data.itemName);
$('#itemDescription').val(data.itemDescription);
$('#stock').val(data.stock);
$('#price').val(data.price);
}
});
});
});
由于
答案 0 :(得分:0)
首先确保您成功连接到数据库。编写一些代码,当您按下提交按钮时,它将连接到数据库,查询并回显到屏幕。如果这样可以,那么可以从列表中检查一个调试项目。