简体   繁体   English

PHP PDO - 如何使用已包含当前数据库值的单选按钮值更新数据库?

[英]PHP PDO - How do I update the Database with Radio button values which already contain the current database value?

Updated to be more relevant.更新为更相关。

I'm fairly new to PHP as shown below.我对 PHP 相当陌生,如下所示。 But I'm currently generating the database and storing the values in Radio buttons, I then want to if changed to be sending new values and updating the database, however I'm struggling to find any material on updating with PHP.但是我目前正在生成数据库并将值存储在单选按钮中,然后我希望更改为发送新值并更新数据库,但是我正在努力寻找有关使用 PHP 进行更新的任何材料。 There is more to the code as this is just a snippet of the relevant bits.代码还有更多内容,因为这只是相关位的一个片段。 All opening brackets do have the corresponding closes too.所有开括号也有相应的闭括号。

try {
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
     $stmt = $conn->prepare("Select SID, ClassID,StudentName, Week_1, Week_2, Week_3, Week_4, Week_5, Week_6, Week_7, Week_8, Week_9, Week_10 FROM weekbyweek");

$stmt->bindColumn('SID', $SID);
$stmt->bindColumn('ClassID', $ClassID);
$stmt->bindColumn('StudentName', $StudentName);    
$stmt->bindColumn('Week_1', $week_1);
$stmt->bindColumn('Week_2', $week_2);
$stmt->bindColumn('Week_3', $week_3);
$stmt->bindColumn('Week_4', $week_4); 
$stmt->bindColumn('Week_5', $week_5);
$stmt->bindColumn('Week_6', $week_6);
$stmt->bindColumn('Week_7', $week_7);
$stmt->bindColumn('Week_8', $week_8);
$stmt->bindColumn('Week_9', $week_9);
$stmt->bindColumn('Week_10', $week_10);

   $stmt->execute();

 print ("<form action='Table/Update2.php' method='post'><tr>");

            //print a cell
            print ("<td> $SID </td>");
            print ("<td> $ClassID </td>");
            print ("<td> $StudentName </td>");
    // print the radio buttons
            print ("<td> $week_1 <input type='radio' value='1' name=" . $name1 ." " . (($week_1 == '1')? 'checked=\"checked\"' :"") .  "><input type='radio' value='0' name=" . $name1 ." " . (($week_1 == '0')? 'checked=\"checked\"' :"") .  "></td> ");
            print ("<td> $week_2 <input type='radio' name=" . $name2 ." " . (($week_2 == '1')? 'checked=\"checked\"' :"") .  "><input type='radio' name=" . $name2 ." " . (($week_2 == '0')? 'checked=\"checked\"' :"") .  "></td> ");
            print ("<td> $week_3 <input type='radio' name=" . $name3 ." " . (($week_3 == '1')? 'checked=\"checked\"' :"") .  "><input type='radio' name=" . $name3 ." " . (($week_3 == '0')? 'checked=\"checked\"' :"") .  "></td> ");
            print ("<td> $week_4 <input type='radio' name=" . $name4 ." " . (($week_4 == '1')? 'checked=\"checked\"' :"") .  "><input type='radio' name=" . $name4 ." " . (($week_4 == '0')? 'checked=\"checked\"' :"") .  "></td> ");
            print ("<td> $week_5 <input type='radio' name=" . $name5 ." " . (($week_5 == '1')? 'checked=\"checked\"' :"") .  "><input type='radio' name=" . $name5 ." " . (($week_5 == '0')? 'checked=\"checked\"' :"") .  "></td> ");
            print ("<td> $week_6 <input type='radio' name=" . $name6 ." " . (($week_6 == '1')? 'checked=\"checked\"' :"") .  "><input type='radio' name=" . $name6 ." " . (($week_6 == '0')? 'checked=\"checked\"' :"") .  "></td> ");
            print ("<td> $week_7 <input type='radio' name=" . $name7 ." " . (($week_7 == '1')? 'checked=\"checked\"' :"") .  "><input type='radio' name=" . $name7 ." " . (($week_7 == '0')? 'checked=\"checked\"' :"") .  "></td> ");
            print ("<td> $week_8 <input type='radio' name=" . $name8 ." " . (($week_8 == '1')? 'checked=\"checked\"' :"") .  "><input type='radio' name=" . $name8 ." " . (($week_8 == '0')? 'checked=\"checked\"' :"") .  "></td> ");
            print ("<td> $week_9 <input type='radio' name=" . $name9 ." " . (($week_9 == '1')? 'checked=\"checked\"' :"") .  "><input type='radio' name=" . $name9 ." " . (($week_9 == '0')? 'checked=\"checked\"' :"") .  "></td> ");
            print ("<td> $week_10 <input type='radio' name=" . $name10 ." " . (($week_10 == '1')? 'checked=\"checked\"' :"") .  "><input type='radio' name=" . $name10 ." " . (($week_10 == '0')? 'checked=\"checked\"' :"") .  "></td> ");

            //close row
            print ("</tr>");
            $ind++;  print ("</table>");
        print ("<input type='submit' name='submit' value='update' onclick='updateDB'/></form>");

The above successfully prints out the database values as 1's and 0's.以上成功将数据库值打印为 1 和 0。 However I'm struggling to update the new values to the database, as shown below.但是,我正在努力将新值更新到数据库中,如下所示。

function updateDB ($db) {

$week1 = ( isset($_POST['Week_1']) ? '1':'0' );
$week2 = ( isset ($_POST [ '". $name2 . "']) ? '1' : '0');
$week3 = ( isset ($_POST [ '". $name3 . "']) ? '1' : '0');
$week4 = ( isset ($_POST [ '". $name4 . "']) ? '1' : '0');
$week5 = ( isset ($_POST [ '". $name5 . "']) ? '1' : '0');
$week6 = ( isset ($_POST [ '". $name6 . "']) ? '1' : '0');
$week7 = ( isset ($_POST [ '". $name7 . "']) ? '1' : '0');
$week8 = ( isset ($_POST [ '". $name8 . "']) ? '1' : '0');
$week9 = ( isset ($_POST [ '". $name9 . "']) ? '1' : '0');
$week10 = ( isset ($_POST [ '". $name10 . "']) ? '1' : '0');

try { 
    $conn = new PDO("mysql:host=$servername;dbname=$dbname", $username, $password);
    $conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);

$sqlQuery = "Update weekbyweek SET  Week_1=?, Week_2=?, Week_3=?, Week_4=?, Week_5=?, Week_6=?, Week_7=?, Week_8=?, Week_9=?, Week_10=?";
$statement->prepare($sqlQuery)->execute(['?','?','?', $name1, $name2, $name3,$name4,$name5,$name6, $name7, $name8,$name9,$name10]);



$statement = $conn->prepare($sqlQuery);
$statement->execute();
}
  }
        UpdateDB();

I don't know how to update values 100%, however I've managed to get to this... There seems to be limited reading on updating multiple values so any help or advice would be top.我不知道如何 100% 更新值,但是我设法做到了这一点……关于更新多个值的阅读似乎有限,因此任何帮助或建议都是最重要的。

Any help would be greatly appreciated.任何帮助将不胜感激。 Thanks, Ollie谢谢,奥利

you can use您可以使用

isset伊塞特

to check whether the radio button is checked/uncheck, because it was unchecked, the form wouldn't send any form data for the particular name.检查单选按钮是否被选中/取消选中,因为它未被选中,表单不会发送特定名称的任何表单数据。

For example to get the value of the radio button:-例如获取单选按钮的值:-

$week1_value = ( isset($_POST['Week_1']) ? '1':'0' );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM