MySQL不从PHP形式更新 [英] mysql not updating from php form
问题描述
我有一个非常简单的PHP表单,它显示一个复选框,并且会在数据库中进行检查或存储。这适用于初始插入,但不适用于更新。我测试了$ saleid等于$ pk的情况,并且它没有进入if分支来更新...为什么?
<< ;?php
error_reporting(E_ALL);
if(isset($ _ GET [cmd]))
$ cmd = $ _GET [cmd];
else
if(isset($ _ POST [cmd]))
$ cmd = $ _POST [cmd];
else die(无效的URL);
if(isset($ _ GET [pk])){$ pk = $ _GET [pk]; }
$ checkfield =;
$ checkboxes =(isset($ _ POST ['checkboxes'])?$ _POST ['checkboxes']:array());
if(in_array('field',$ checkboxes))$ checkfield ='checked';
$ con = mysqli_connect(localhost,user,,db);
if(!$ con){echo无法连接到MySQL服务器。错误代码:%s \ n。 mysqli_connect_error();出口; }
$ con> set_charset(utf8);
$ getformdata = $ con> query(select saleid,field from STATUS where saleid ='$ pk');
$ saleid =;
while($ row = mysqli_fetch_assoc($ getformdata)){
$ saleid = $ row ['saleid'];
$ checkfield = $ row ['field'];
}
if($ cmd ==submitinfo){
if($ saleid == null){
$ statusQuery =INSERT INTO STATUS VALUES( ?,?);
if($ statusInfo = $ con> prepare($ statusQuery)){
$ statusInfo-> bind_param(sssssssssssss,$ pk,$ checkfield);
$ statusInfo-> execute();
$ statusInfo-> close();
} else {
print_r($ con> error);
}
} else if($ saleid == $ pk){
$ blah =what;
$ statusQuery =UPDATE STATUS SET field =?WHERE saleid =?;
if($ statusInfo = $ con> prepare($ statusQuery)){
$ statusInfo-> bind_param(ss,$ checkfield,$ pk);
$ statusInfo-> execute();
$ statusInfo-> close();
} else {
print_r($ con> error);
$ b if($ cmd ==EditStatusData){
echo< form name = \statusForm \action = \test.php?pk =。$ pk。\method = \post \enctype = \multipart / form-data \>
< h1>拍卖编号信息:。$ pk。< / h1>
< input type = \checkbox \name = \复选框[] \value = \字段\。$ checkfield。/>
< label for = \field \>测试< / label>
< br>
< ; input type = \hidden \name = \cmd \value = \submitinfo \/>
;
}
?>
我创建了一个表并运行您的代码,对我来说工作很好
它不会像更新一样看起来的原因是因为您正在读取
$ saleid和$ checkfield然后构建一个更新语句,将相同的两个值放回到数据库中。
这行将$ checkfield设置为'checked',
if(in_array('field', $ checkboxes))$ checkfield ='checked';
然后您从数据库中设置$ checkfield(覆盖'checked'值)
while($ row = mysqli_fetch_assoc($ getformdata)){
$ saleid = $ row ['saleid'];
$ checkfield = $ row ['field'];
然后您将checkfield的原始值写回到数据库中
$ statusInfo-> bind_param(ss,$ checkfield,$ pk);
I have a very simple PHP form, which shows a checkbox, and will store if it is checked or not in a database. This works for the initial inserting, but not for updating. I have tested cases where $saleid equals $pk and it does not enter the if branch to update...why?
<?php
error_reporting(E_ALL);
if (isset($_GET["cmd"]))
$cmd = $_GET["cmd"];
else
if (isset($_POST["cmd"]))
$cmd = $_POST["cmd"];
else die("Invalid URL");
if (isset($_GET["pk"])) { $pk = $_GET["pk"]; }
$checkfield = "";
$checkboxes = (isset($_POST['checkboxes'])? $_POST['checkboxes'] : array());
if (in_array('field', $checkboxes)) $checkfield = 'checked';
$con = mysqli_connect("localhost","user","", "db");
if (!$con) { echo "Can't connect to MySQL Server. Errorcode: %s\n". mysqli_connect_error(); exit; }
$con->set_charset("utf8");
$getformdata = $con->query("select saleid, field from STATUS where saleid = '$pk'");
$saleid = "";
while ($row = mysqli_fetch_assoc($getformdata)) {
$saleid = $row['saleid'];
$checkfield = $row['field'];
}
if($cmd=="submitinfo") {
if ($saleid == null) {
$statusQuery = "INSERT INTO STATUS VALUES (?, ?)";
if ($statusInfo = $con->prepare($statusQuery)) {
$statusInfo->bind_param("sssssssssssss", $pk, $checkfield);
$statusInfo->execute();
$statusInfo->close();
} else {
print_r($con->error);
}
} else if ($saleid == $pk) {
$blah = "what";
$statusQuery = "UPDATE STATUS SET field = ? WHERE saleid = ?";
if ($statusInfo = $con->prepare($statusQuery)) {
$statusInfo->bind_param("ss", $checkfield, $pk);
$statusInfo->execute();
$statusInfo->close();
} else {
print_r($con->error);
}
}
}
if($cmd=="EditStatusData") {
echo "<form name=\"statusForm\" action=\"test.php?pk=".$pk."\" method=\"post\" enctype=\"multipart/form-data\">
<h1>Editing information for Auction No: ".$pk."</h1>
<input type=\"checkbox\" name=\"checkboxes[]\" value=\"field\" ".$checkfield." />
<label for=\"field\">Test</label>
<br />
<input type=\"hidden\" name=\"cmd\" value=\"submitinfo\" />
<input name=\"Submit\" type=\"submit\" value=\"submit\" />
</form>";
}
?>
well i created a table and ran your code and it works fine for me
the reason why it doesn't "look" like update is working, is because you are reading $saleid and $checkfield from the database then building an update statement that puts the same two values back into the database
which probably isn't what you are wanting to do
this line here sets $checkfield to 'checked',
if (in_array('field', $checkboxes)) $checkfield = 'checked';
then you set $checkfield from the database (overwriting the value 'checked' )
while ($row = mysqli_fetch_assoc($getformdata)) {
$saleid = $row['saleid'];
$checkfield = $row['field'];
then you write the original value of checkfield back to the database
$statusInfo->bind_param("ss", $checkfield, $pk);
这篇关于MySQL不从PHP形式更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!