Jump to content

PHP Option Select to MySQL


StuHannah
Go to solution Solved by Psycho,

Recommended Posts

Morning,

 

I am looking for some help. What I am trying to do is for each week of the year assign a letter to it, either an A or B week for rotas.

 

I am able to print on screen the weeks for 52 weeks and this part works fine and lists the weeks, and what I want to do from there is use a combobox next to each date so we can allocate a week to it selecting either A or B. The code I have been using and working on I have put on the bottom.

 

Just come a bit stuck and need a little help please.

 

This bit gets me all of the weeks for the next 52 weeks.

for($i=1; $i<=52; $i++){
$num = date("Y-m-d", strtotime('Last Monday +'.$i.' week')); 
echo $num

I am now trying to get the below working and can't figure it out...

// This is the part to read the submission
<?php
if(isset($_POST['formSubmit']) )
{
  $varWeekNow = $_POST['{$num}'];
  // SQL Bit
  
  $sql = "INSERT INTO weeks (Date, Week) VALUES ('$num', '$varWeekNow')";
if (mysqli_query($link, $sql)) {
    echo "New record created successfully";
} else {
    echo "Error: " . $sql . "<br>" . mysqli_error($link);
}}
  ?>

// this is the part to print the weeks and a dropbown box.
<?php 
for($i=1; $i<=52; $i++){
$num = date("Y-m-d", strtotime('Last Monday +'.$i.' week')); 
echo "<form action=\"index.php\" method=\"post\">
What is this week? " . $num . "
<select name=\"{$num}\">
  <option value=\"\">Select...</option>
  <option value=\"A\">A Week</option>
  <option value=\"B\">B Week</option>
</select>
<input name=\"formSubmit\" type=\"hidden\" value=\1\" />
<input name=\"submit\" type=\"submit\" />
</p></form>";
}
?>

Kind Regards,

 

Stuart

Link to comment
Share on other sites

In the top part of your code, you are using $num, but it isn't defined yet, '{$num}' is just a string, and $varWeekNow is thus NULL.

 

Don't use $num as your name of your select menu, but make it the same name for all forms, and put another hidden input in each form with name "num", and value $num.

 

PS.  Take steps to prevent SQL injection.

 

PSS.  Use PDO.

Link to comment
Share on other sites

your design has some user experience UX problems.

 

by using a select/option menu, the user must first click the select button to see the two choice to pick from and since each week is its own form, only one selection can be submitted at a time. if someone was setting up all 52 weeks, it would take 156 clicks and 52 form submissions and re-displays of the page.

 

if you instead use radio buttons and a single form, the user can go through and select or change any of the week values by simply clicking on the choices and hitting one submit button. that would take only 53 clicks and one form submission to set up all 52 weeks.

 

p.s. you are missing a " in your html markup, probably lost due to the escaping \" going on. you should use single-quotes ' inside of a double-quoted string so that the code is not cluttered up with escape characters.

Edited by mac_gyver
Link to comment
Share on other sites

  • Solution

Using several of the recommendations above and some of my ideas on improving

 

<?php
 
$responseMessage = '';
if(isset($_POST['date']) )
{
    //Get list of ALL submitted dates with valid value
    $insertValues = array();
    foreach($_POST['date'] as $date => $value)
    {
        if($value=='A' or $value=='B')
        {
            $insertValues[] = "('{$date}', '{$value}')";
        }
    }
 
    //Run a a single query to inset all submitted values
    // *** The query should be a prepared statement to guard against SQL injection
    $sql = "INSERT INTO weeks (Date, Week) VALUES " . implode(',', $insertValues);
    if (mysqli_query($link, $sql)) {
        $responseMessage = "New record created successfully";
    } else {
        // *** For debugging only. Should not display actual errors to user
        $responseMessage = "SQL: " . $sql . "<br>Error: " . mysqli_error($link);
    }
}
 
// *** Should run query to get already saved values from the database
// *** and use those values in the loop below to set any already saved values
 
//Create the form fields for each date
$formFields = '';
$weeksOut = 52;
for($weekNo=1; $weekNo<=$weeksOut; $weekNo++)
{
    $date = date("Y-m-d", strtotime('Last Monday +'.$weekNo.' week')); 
    $formFields .= "<p>What is this week?:  {$date}\n";
    $formFields .= "<input type='radio' name='date[{$date}]' value='A'>A</input>\n";
    $formFields .= "<input type='radio' name='date[{$date}]' value='B'>B</input></p>\n";
}
?>
<html>
<body>
 
<?php echo $responseMessage; ?>
<br>
<form action="test.php" method="post">
    <?php echo $formFields; ?>
    <button type="submit">Submit</button>
</form>
 
</body>
</html>
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.