我修复了我的代码中的先前错误,但是我遇到了一个新问题,我创建的删除和更新函数正在影响我数据库中的所有其他行... 我怎么阻止它?这是我的代码......
主体:
<h2 align="center">Add to Inventory</h2>
<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>
</div>
</div>
<div id="result" class="table">
</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('');
$('#item_id').val('');
$('#action').text("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= $('#item_id').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("id");
$.ajax({
url: "fetch.php",
method: "POST",
data:{item_id:item_id},
dataType:"json",
success:function(data){
$('#action').text("Edit");
$('#item_id').val(item_id);
$('#itemName').val(data.itemName);
$('#itemDescription').val(data.itemDescription);
$('#stock').val(data.stock);
$('#price').val(data.price);
$('#item_id').val(data.item_id);
}
});
});
$(document).on('click', '.delete', function(){
var item_id= $(this).attr("id");
if (confirm("Are you sure you want to remove this data?")) {
var action= "Delete";
$.ajax({
url: "itemAction.php",
method: "POST",
data:{item_id:item_id, action:action},
success:function(data){
fetchItems();
alert(data);
}
});
}else{
return false;
}
});
});
itemSelect.php:
<?php
$output= '';
$connect= mysqli_connect("localhost", "Eben", "Pass1234", "stageupgh");
if (isset($_POST["action"])) {
$procedure= "
CREATE PROCEDURE selectItem()
BEGIN
SELECT * FROM items ORDER BY item_id DESC;
END;
";
if (mysqli_query($connect, "DROP PROCEDURE IF EXISTS selectItem")) {
if (mysqli_query($connect, $procedure)) {
$query= "CALL selectItem()";
$result= mysqli_query($connect, $query);
$output.= '
<table class="table" style="background-color: inherit;">
<tr>
<th>Item Name</th>
<th>Description</th>
<th>In Stock</th>
<th>Unit price</th>
<th colspan="2" align="center">Action</th>
</tr>
';
if (mysqli_num_rows($result)>0) {
while ($row= mysqli_fetch_array($result)) {
$output.= '
<tr>
<td>'.$row["name"].'</td>
<td>'.$row["description"].'</td>
<td>'.$row["in_stock"].'</td>
<td>Gh ₵ '.$row["price"].'</td>
<td hidden="true">'.$row["item_id"].'</td>
<td>
<button type="button" name="update" id="'.$row["item_id"].'" class="update btn btn-warning">Update</button>
</td>
<td>
<button type="button" name="delete" id="'.$row["item_id"].'" class="delete btn btn-danger">Delete</button>
</td>
</tr>
';
}
}else{
$output.= '
<tr align="center">
<td colspan="6">There are no items available</td>
</tr>
';
}
$output.= '</table>';
echo $output;
}
}
}
&GT;
fetch.php:
<?php
$connect= mysqli_connect("localhost", "Eben", "Pass1234", "stageupgh");
if ($connect) {
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"];
$output['item_id']= $row['item_id'];
}
echo json_encode($output);
}
}
}
}else{
die(mysql_error());
}
&GT;
和itemAction.php:
<?php
if (isset($_POST["action"])) {
$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 ($_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"]);
$item_id= mysqli_real_escape_string($connect, $_POST["item_id"]);
$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= name, description= description, in_stock= in_stock, price= price
WHERE item_id= item_id;
END;
";
if (mysqli_query($connect, "DROP PROCEDURE IF EXISTS updateItem")) {
if (mysqli_query($connect, $procedure)) {
$query= "CALL updateItem('".$_POST["item_id"]."', '".$itemName."', '".$itemDescription."', '".$stock."', '".$price."')";
if (mysqli_query($connect, $query)) {
echo "Data Updated";
}else{
echo "Something went wrong...</br>See Developer.</br></br>";
die (mysqli_error($connect));
}
}
}
}
if($_POST["action"]=="Delete"){
$procedure="
CREATE PROCEDURE deleteItem(IN item_id int(11))
BEGIN
DELETE FROM items WHERE item_id= item_id;
END;
";
if (mysqli_query($connect, "DROP PROCEDURE IF EXISTS deleteItem")) {
if (mysqli_query($connect, $procedure)) {
$query= "CALL deleteItem('".$_POST["item_id"]."')";
if (mysqli_query($connect, $query)) {
echo "Data Deleted";
}else{
echo "Something went wrong...</br></br>";
die (mysqli_error($connect));
}
}
}
}
}
&GT;