PHP sql 更新查询后数据库未更新,未定义索引?
PHP sql database not updating after update query, indexes are not defined?
我遇到无法更新数据库的问题。我认为错误出在更新查询本身,但我是 SQL 和 PHP 的新手,因此我不是 100% 确定。任何帮助将不胜感激。
我遇到了一个未定义的索引错误,我已修复该错误,但问题仍然存在:
$id = $_GET["id"];
$listdate = isset($_POST['list_date']) ? $_POST['list_date'] : '';
$listprice = isset($_POST['list_price']) ? $_POST['list_price'] : '';
$solddate = isset($_POST['sold_date']) ? $_POST['sold_date'] : '';
$soldprice = isset($_POST['sold_price']) ? $_POST['sold_price'] : '';
$shipdate = isset($_POST['ship_date']) ? $_POST['ship_date'] : '';
$shipcost = isset($_POST['ship_cost']) ? $_POST['ship_cost'] : '';
更改为上述代码(无效)后,我在更新数据库时仍然遇到问题。这是完整的当前代码:
<?
$id = $_GET["id"];
$listdate = $_POST["list_date"];
$listprice = $_POST["list_price"];
$solddate = $_POST["sold_date"];
$soldprice = $_POST["sold_price"];
$shipdate = $_POST["ship_date"];
$shipcost = $_POST["ship_cost"];
$servername = "localhost";
$username = "inventory";
$password = "*****";
$db = "products";
$conn = new mysqli($servername, $username, $password, $db);
if ($conn->connect_error){
die("Connection failed: ". $conn->connect_error);
}
$sql = "UPDATE inventory SET list_date = '$listdate', list_price = '$listprice', sold_date = '$solddate', sold_price = '$soldprice', ship_date = '$shipdate', ship_cost= '$shipcost' WHERE product_id = ' .$id. '";
if($conn->query($sql) === TRUE){
echo "Record Saved.";
} else {
echo "Error!";
}
$conn->close();
?>
即使我手动将 ID 更改为数据库分配的 ID,更新仍然不起作用,并且没有对数据库进行任何更改。我知道该表格看起来不漂亮,但我将其设置为进行测试。以下是我如何设置表单以捕获更改:
<form action="/SulleySells/scripts/updateitem.php?id=<?php echo $id;?>" method="post">
<script type="text/javascript">
function ShowHideDiv(listed) {
var updatel = document.getElementById("updatel");
updatel.style.display = listed.checked ? "block" : "none";
}
function ShowHideDiv2(sold) {
var updates = document.getElementById("updates");
updates.style.display = sold.checked ? "block" : "none";
}
</script>
<label for="listed">
<input type="checkbox" id="listed" onclick="ShowHideDiv(this)" />
Listed?
</label>
<label for="sold">
<input type="checkbox" id="sold" onclick="ShowHideDiv2(this)" />
Sold?
</label>
<hr>
<div id="updatel" style="display: none">
<h3>Update Listing Details:</h3>
<label for="listdate">Listed Date:</label>
<input type="date" id="updateltext" name="listdate" value=""/>
<br>
<label for="listprice">Listed Price:</label>
<input type="text" id="updateltext" name="listprice" value=""/>
<br>
<button>Update</button>
<hr>
</div>
<div id="updates" style="display: none">
<h3>Update Sale Details:</h3>
<label for="solddate">Sold Date:</label>
<input type="date" id="updatestext" name="solddate" value=""/>
<br>
<label for="soldprice">Sold Price:</label>
<input type="text" id="updatestext" name="soldprice" value=""/>
<br>
<label for="shipdate">Ship Date:</label>
<input type="date" id="updatestext" name="shipdate" value=""/>
<br>
<label for="shipcost">Ship Cost:</label>
<input type="text" id="updatestext" name="shipcost"value=""/>
<br>
<button>Update Sold Info</button>
<hr>
</div>
</form>
首先你要改变:
<input type="date" id="updateltext" name="listdate" value=""/>
至:
<input type="date" id="updateltext" name="list_date" value=""/>
匹配您在 php 代码中的内容并将其应用于其余 html 输入
然后更改:
WHERE product_id = ' .$id. '
至:
WHERE product_id = '$id'
通知
不要忘记在输入中使用 id="" 格式化 for="" in lable,以免给您不同的结果
我遇到无法更新数据库的问题。我认为错误出在更新查询本身,但我是 SQL 和 PHP 的新手,因此我不是 100% 确定。任何帮助将不胜感激。
我遇到了一个未定义的索引错误,我已修复该错误,但问题仍然存在:
$id = $_GET["id"];
$listdate = isset($_POST['list_date']) ? $_POST['list_date'] : '';
$listprice = isset($_POST['list_price']) ? $_POST['list_price'] : '';
$solddate = isset($_POST['sold_date']) ? $_POST['sold_date'] : '';
$soldprice = isset($_POST['sold_price']) ? $_POST['sold_price'] : '';
$shipdate = isset($_POST['ship_date']) ? $_POST['ship_date'] : '';
$shipcost = isset($_POST['ship_cost']) ? $_POST['ship_cost'] : '';
更改为上述代码(无效)后,我在更新数据库时仍然遇到问题。这是完整的当前代码:
<?
$id = $_GET["id"];
$listdate = $_POST["list_date"];
$listprice = $_POST["list_price"];
$solddate = $_POST["sold_date"];
$soldprice = $_POST["sold_price"];
$shipdate = $_POST["ship_date"];
$shipcost = $_POST["ship_cost"];
$servername = "localhost";
$username = "inventory";
$password = "*****";
$db = "products";
$conn = new mysqli($servername, $username, $password, $db);
if ($conn->connect_error){
die("Connection failed: ". $conn->connect_error);
}
$sql = "UPDATE inventory SET list_date = '$listdate', list_price = '$listprice', sold_date = '$solddate', sold_price = '$soldprice', ship_date = '$shipdate', ship_cost= '$shipcost' WHERE product_id = ' .$id. '";
if($conn->query($sql) === TRUE){
echo "Record Saved.";
} else {
echo "Error!";
}
$conn->close();
?>
即使我手动将 ID 更改为数据库分配的 ID,更新仍然不起作用,并且没有对数据库进行任何更改。我知道该表格看起来不漂亮,但我将其设置为进行测试。以下是我如何设置表单以捕获更改:
<form action="/SulleySells/scripts/updateitem.php?id=<?php echo $id;?>" method="post">
<script type="text/javascript">
function ShowHideDiv(listed) {
var updatel = document.getElementById("updatel");
updatel.style.display = listed.checked ? "block" : "none";
}
function ShowHideDiv2(sold) {
var updates = document.getElementById("updates");
updates.style.display = sold.checked ? "block" : "none";
}
</script>
<label for="listed">
<input type="checkbox" id="listed" onclick="ShowHideDiv(this)" />
Listed?
</label>
<label for="sold">
<input type="checkbox" id="sold" onclick="ShowHideDiv2(this)" />
Sold?
</label>
<hr>
<div id="updatel" style="display: none">
<h3>Update Listing Details:</h3>
<label for="listdate">Listed Date:</label>
<input type="date" id="updateltext" name="listdate" value=""/>
<br>
<label for="listprice">Listed Price:</label>
<input type="text" id="updateltext" name="listprice" value=""/>
<br>
<button>Update</button>
<hr>
</div>
<div id="updates" style="display: none">
<h3>Update Sale Details:</h3>
<label for="solddate">Sold Date:</label>
<input type="date" id="updatestext" name="solddate" value=""/>
<br>
<label for="soldprice">Sold Price:</label>
<input type="text" id="updatestext" name="soldprice" value=""/>
<br>
<label for="shipdate">Ship Date:</label>
<input type="date" id="updatestext" name="shipdate" value=""/>
<br>
<label for="shipcost">Ship Cost:</label>
<input type="text" id="updatestext" name="shipcost"value=""/>
<br>
<button>Update Sold Info</button>
<hr>
</div>
</form>
首先你要改变:
<input type="date" id="updateltext" name="listdate" value=""/>
至:
<input type="date" id="updateltext" name="list_date" value=""/>
匹配您在 php 代码中的内容并将其应用于其余 html 输入
然后更改:
WHERE product_id = ' .$id. '
至:
WHERE product_id = '$id'
通知 不要忘记在输入中使用 id="" 格式化 for="" in lable,以免给您不同的结果