2009-05-24 37 views
0

我有一個非常簡單的PHP表單,它顯示一個複選框,並將存儲如果它被檢查或不在數據庫中。這適用於初始插入,但不適用於更新。我測試了$ saleid等於$ pk的情況,並且它沒有輸入if分支來更新...爲什麼?mysql不從php格式更新

<?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>"; 
} 
?> 

回答

0

以及我創建了一個表,跑了你的代碼,它工作正常,我

之所以不「看」之類的更新工作,是因爲你正在閱讀 $ saleid和$從校驗場該數據庫,然後構建一個使兩個相同的值返回到數據庫中的更新語句

這可能不是你想要做什麼

這裏該行設置$校驗場,以「檢查」,

if (in_array('field', $checkboxes)) $checkfield = 'checked'; 

然後從數據庫中設置$校驗場(覆蓋值「檢查」)

while ($row = mysqli_fetch_assoc($getformdata)) { 
    $saleid = $row['saleid']; 
    $checkfield = $row['field']; 

那麼你就寫校驗場的原始值回數據庫

$statusInfo->bind_param("ss", $checkfield, $pk); 
0

不知道你是否可以混合使用GET和POST請求類型

也許改變這種做法,PK傳回的隱藏字段?

echo "<form name=\"statusForm\" action=\"test.php?pk=".$pk."\" method=\"post\" enctype=\"multipart/form-data\"> 

例如,有點像這個

echo "<form name=\"statusForm\" action=\"test.php\" method=\"post\" enctype=\"multipart/form-data\"> 
<input type=\"hidden\" name=\"pk\" value=\"".$pk."\"> 
+0

也記住,如果複選框不打勾,它不會在php – bumperbox 2009-05-25 00:13:16

+0

的複選框[]數組中返回一個值混合工作正常插入...只是不更新​​... – Josh20002 2009-05-25 01:28:32

0

這是你的HTML應該是什麼樣子:

<form id="aform" action="thisform.php" method="post"> 
     <input type="checkbox" name="agree" value="yes" /> 
     <input type="hidden" name="secret" value="shhh" /> 
     <input type="submit" value="do it" /> 
    </form> 

通過以上如果你這樣做:

print_r($_POST); 

你會得到一個數組,其中有[同意] =>'是'或否的東西,取決於他們是否檢查框,所以不需要放置數組括號,除非你有很多盒子。

至於SQL部分,我建議將該列設置爲一個整數類型,其中它可以有0或1. 0表示未選中,1表示選中。對於插入,你會做這樣的事情:

$check_value = ($_POST['agree'] == 'yes') ? 1 : 0; 
    $secret_stuff = $_POST['secret']; 
    mysqli_query("Insert INTO sales_table (secret_column, agree_column) 
       VALUES ('$secret_stuff', '$check_value')"); 

這將讓你的複選框到表中。要得到它,你應該去:

$results = mysqli_query("SELECT * from sales_table where secret_column = $secret_stuff") 

    while($row = mysqli_fetch_assoc($results)) { 
    $checked = ($row['agree_column'] == 1) ? "checked=\"checked\"" : ""; 
    $secret_stuff = $row['secret_column]; 
    } 

    ?> 
    <form action=blah method=post id=blah> 
    <input type="checkbox" name="agree" value="yes" <?php echo $checked;?> /> 
    </form> 

對不起,失去了最後的蒸汽。但是這涵蓋了前端和後端。使用1/0開關,只需設置一些像$ check這樣的變量到「checked ='勾選'」,如果它是1.

+0

我現在這樣做的方式工作正常,與我的HTML ...唯一的問題是更新聲明沒有得到ca lled – Josh20002 2009-05-25 01:29:31

0

您沒有設置$ pk變量,除非isset($_GET["pk"]),但您仍然稍後在查詢中使用它。這不是一個好主意,因爲根據其他情況,這可能會導致錯誤。你希望你的邏輯是什麼樣子是這樣的:

if pk is not set in form 
    insert new record 
    deal with error if insert failed 
else 
    update existing record 
    check update count and deal with error if 0 records were updated 
     (perhaps by doing an insert of the missing record) 
end 
+0

該窗體始終使用pk作爲ajax應用程序的參數調用,因此這是設計的行爲。目前唯一的問題是沒有輸入更新分支。 – Josh20002 2009-05-25 01:59:08

0

正如一個側面說明,它看起來像MySQL的REPLACE功能會派上用場你。

此外,當複選框未選中時,該值可能是一件棘手的事情。我寫了將該值設置爲一個函數,如果公佈值設置,並且零,如果不是......

function checkbox_value($name) { 
    return (isset($_POST[$name]) ? 1 : 0); 
} 

可以throught該查詢運行發佈複選框值和總能得到一個或零。