Skip to content
Advertisement

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. However, I’m having trouble passing the variables onto the next page to run the update function. I haven’t used the update function before and I’m struggling to find others who have had the same issue.

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:

$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.

Advertisement

Answer

You have basically 2 problems Fist

SET (:SID =?

both sides :SID and ? 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

Like

SID = :SID

The left side is the columnname and the right side is the placeholder ยด, to which you bind the $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

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

User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement