简体   繁体   English

如何将绑定变量发送到另一个页面,然后更新数据库?

[英]How do I send bound variables to another page to then update the database?

I'm trying to update a database dependant on an whether the radio button has been clicked or not for a number of different rows which have all been generated from the database.我正在尝试根据是否已单击单选按钮来更新数据库,这些单选按钮是否已从数据库中生成了许多不同的行。 I've managed to select all of the data and store them into radio buttons fine.我已经设法 select 所有数据并将它们存储到单选按钮中。 However, I'm having trouble passing the variables onto the next page to run the update function.但是,我无法将变量传递到下一页以运行更新 function。 I haven't used the update function before and I'm struggling to find others who have had the same issue.我之前没有使用过更新 function 并且我正在努力寻找遇到同样问题的其他人。

Below is the code for the radio buttons and each variable value:下面是单选按钮和每个变量值的代码:

$name1 = "week1" . $ind;
            $name2 = "week2" . $ind;
            $name3 = "week3" . $ind;
            $name4 = "week4" . $ind;
            $name5 = "week5" . $ind;
            $name6 = "week6" . $ind;
            $name7 = "week7" . $ind;
            $name8 = "week8" . $ind;
            $name9 = "week9" . $ind;
            $name10 = "week10" . $ind;
            $SID2="SID" . $ind;
            $ClassID2 = "ClassID" . $ind;
            $StudentName2 = "StudentName" . $ind;

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

            //print a cell
            print ("<td> <input type ='hidden' name ='". $SID2."' value ='".$SID."'> $SID </td>");
            print ("<td> <input type ='hidden' name ='" . $ClassID2 . "' value ='" . $ClassID . "'> $ClassID </td>");
            print ("<td> <input type ='hidden' name ='" . $StudentName2 . "'value='".$StudentName."'> $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++;

And below is the next page receiving the data, which I believe is the main issue:下面是接收数据的下一页,我认为这是主要问题:



$SID =( isset ($_POST['".SID2 ."']));
$ClassID = (isset ($_POST['" . $ClassID2 . "']));
$StudentName = (isset ($_POST[ '" . $StudentName2 . "']));

$week1 = ( isset ($_POST ['". $name1 . "']) ? '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');

Then below is the SQL statement, which keeps cropping up with the HY093 error:然后下面是 SQL 语句,不断出现 HY093 错误:

$sqlQuery = "Update weekbyweek SET (:SID =?, :ClassID=?, :StudentName=?, :Week_1=?, :Week_2=?, :Week_3=?, :Week_4=?, :Week_5=?, :Week_6=?, :Week_7=?, :Week_8=?, :Week_9=?, :Week_10=?)";
$statement = $db->prepare($sqlQuery);
$statement->execute([':SID'=> $SID, ':ClassID'=> $ClassID, ':StudentName' => $StudentName, ':Week_1' => $week1, ':Week_2' => $week2, ':Week_3' => $week3, ':Week_4' => $week4,':Week_5' => $week5, ':Week_6' => $week6,':Week_7' => $week7, ':Week_8' => $week8,':Week_9' => $week9, ':Week_10' => $week10]);

I'm presuming I haven't bound the values properly on this next page.我假设我没有在下一页上正确绑定值。

Any help or advice would be greatly appreciated.任何帮助或建议将不胜感激。

You have basically 2 problems Fist你基本上有2个问题拳头

SET (:SID =?设置(:SID =?

both sides:SID and?双方:SID 和? are placeholders and when you decided to used named variables (:SID) you can only use:SID but this is only valif on the right side是占位符,当您决定使用命名变量(:SID)时,您只能使用:SID,但这只是右侧的valif

Like喜欢

SID =:SID SID =:SID

The left side is the columnname and the right side is the placeholder ´, to which you bind the $SID左侧是列名,右侧是占位符 ´,您将 $SID 绑定到该位置

The second problem are the parenthesis, mysql don't like them jn the way you use it,in your case get rid of them第二个问题是括号,mysql 不喜欢你使用它的方式,在你的情况下摆脱它们

So you get the next code, of course i don't know your database, so i can't guarantee that this will work.所以你得到下一个代码,当然我不知道你的数据库,所以我不能保证这会起作用。

$sqlQuery = "Update weekbyweek SET SID = :SID, ClassID = :ClassID, StudentName=:StudentName, Week_1= :Week_1, Week_2=:Week_2, Week_3=:Week_3, Week_4=:Week_4, Week_5= :Week_5, Week_6= :Week_6, Week_7= :Week_7, Week_8= :Week_8, Week_9= :Week_0, Week_10=:Week_10";
$statement = $db->prepare($sqlQuery);
$statement->execute([':SID'=> $SID, ':ClassID'=> $ClassID, ':StudentName' => $StudentName, ':Week_1' => $week1, ':Week_2' => $week2, ':Week_3' => $week3, ':Week_4' => $week4,':Week_5' => $week5, ':Week_6' => $week6,':Week_7' => $week7, ':Week_8' => $week8,':Week_9' => $week9, ':Week_10' => $week10]);

But when you have a error Hhadnling the problem should be eeasily soved,但是当你有一个错误时,问题应该很容易解决,

When i saw your code i thought, why didn't heuse arrays see Multiple radio button array for php form当我看到您的代码时,我想,为什么不使用 arrays 查看php 表单的多个单选按钮数组

暂无
暂无

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

相关问题 PHP:如何将变量发送到当前页面? - PHP: How do I send variables to the current page? 如何将变量传递到 php 中的另一个页面? - How do i pass variables to another page in php? 如何使用jQuery / Javascript将变量发布到另一页? - How do I POST variables using jQuery / Javascript to another page? 我如何将 ajax function 值从一个页面发送到另一个页面 - How do i send ajax function values from a page to another 我必须将两个变量 about 和 product 发送到 php 中的另一个页面 - I have to send two variables about and product to another page in php 如何在不刷新页面的情况下从数据库字段更新页面上的文本? - how do i update text on a page from a database field without refreshing page? 如何使用更新的mysql数据库信息更新javascript中的php变量? - How do I update my php variables in javascript with updated mysql database information? 如何使用数据库中正在更改的新值更新javascript变量? - How do I update javascript variables with new values which are changing in my database? 我如何生成将根据 ID 从数据库显示这些产品的链接到 php 中的另一个页面(产品页面) - How do i generate links that will display these products depending on the ID from the database to another page (product page) in php 在PHP中,我如何将表单数据发送到页面,但是当用户单击“提交”按钮时却将用户定向到另一个页面? - In PHP how do I send form data to a page but direct the user to another when he clicks the submit button?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM