使用PHP更新mysqli数据库中的产品
尝试使用php一次更新一行 我想让用户能够更新他们已经添加到数据库中的产品,我有一个带有相关字段的简单表单:使用PHP更新mysqli数据库中的产品,php,ajax,mysqli,Php,Ajax,Mysqli,尝试使用php一次更新一行 我想让用户能够更新他们已经添加到数据库中的产品,我有一个带有相关字段的简单表单: <fieldset><legend><span> Update a product in the database! </span></legend> <form id ="productsform" method="post" onsubmit="return false;" enctype="mu
<fieldset><legend><span> Update a product in the database! </span></legend>
<form id ="productsform" method="post" onsubmit="return false;" enctype="multipart/form-data">
<label> Product name: <input type="text" id="name" name="name"/> </label>
<label> Product quantity: <input type="number" id="quantity" name="quantity"/> </label>
<label> Product description: <input type="text" id="description" name="description"/> </label>
<label> Product price: <input type="text" id="price" name="price"/> </label>
</br>
<input id="submit" name="submit" type="button" class="reg" value="Update Product">
<div id="update"></div>
</form>
更新数据库中的产品!
产品名称:
产品数量:
产品说明:
产品价格:
我使用的是ajax,它在控制台上正常工作,但我在php方面难以更新行:
<?php
include("dbase/config_database.php");
$id = $_POST["id"];
$name = $_POST['name'];
$quantity = $_POST['quantity'];
$description = $_POST['description'];
$price = $_POST['price'];
$query = "UPDATE products SET name = '{$name}', quantity = '{$quantity}', description = '{$description}', price = '{$price}'
WHERE id = {$id}";
mysqli_query($mysqli, $query);
?>
您没有从表单中获得$\u POST[“id”]
,因为没有名为id
的输入元素
从表中获取所有数据后,将id
作为隐藏字段放入表单中
像
您是否收到任何错误?这是从哪里来的<代码>$\u POST[“id”]代码>我在updateSQL.php的第5行获得了未定义的index:id
<?php
include("dbase/config_database.php");
//Stores all information passed through AJAX into the query
$name = $_POST['name'];
$quantity = $_POST['quantity'];
$description = $_POST['description'];
$price = $_POST['price'];
//Adds information to database
$query = "INSERT INTO products (name, quantity, description, price) VALUES ('$name','$quantity','$description','$price')";
//Runs the query
$result = $mysqli->query($query) OR die("Failed query $query");
echo $mysqli->error."<p>";
echo "Product Added";
$querynew = ("SELECT id as 'collectid' from products WHERE name = '$name'and quantity = '$quantity'and description ='$description'and price = '$price'");
$resultnew = $mysqli->query($querynew) OR die("Failed query $querynew");
while($info = mysqli_fetch_array( $resultnew)){
$productid = $info['collectid'];
}
$image = $_FILES['file1']['name'];
$type = $_FILES['file1']['type'];
$size = $_FILES['file1']['size'];
$tmp_name = $_FILES['file1']['tmp_name'];
$imgpath = "images/".$productid.".jpg";
// Run the move_uploaded_file() function here
$moveResult = move_uploaded_file($tmp_name, $imgpath);
// Evaluate the value returned from the function if needed
$querytwo = ("SELECT * FROM products WHERE name = '$name' and quantity = '$quantity' and description = '$description' and price = '$price'");
$resulttwo = $mysqli ->query($querytwo) OR die ("Failed query $querynew");
$info = array();
while($row = mysqli_fetch_assoc($resulttwo)){
$product = array("id" => $row ['id'],
"name" => $row ['name'],
"quantity" => $row ['quantity'],
"description" => $row ['description'],
"price" => $row ['price'],
);
array_push($info,$product);
}
$json_output = json_encode($info);
echo $json_output;
?>
<input type="hidden" name="id" value="<?=$row['id']?>">