How to insert data into DB which would be altered by selected number


I was looking at TeNDoLLA's code above and he's picking up $maintenanceDate1 from in page described values, where's I have these values in DB.

I am stuck with this and don't know how to pick up value that's been selected from drop-down list.
Here's the code where you select maintenance interval, how do I pick up the selected value later on?

        <label for="Platform">Maintenance Interval<span style="color:red;">*</span>:</label>
        <select name="maintenanceint" required>
          <option value=""> - </option>
			do {  
          <option value="<?php echo $row_rsPlatform['platform']?>"><?php echo $row_rsPlatform['platform']?></option>
			} while ($row_rsPlatform = mysql_fetch_assoc($rsPlatform));
			  $rows = mysql_num_rows($rsPlatform);
			  if($rows > 0) {
				  mysql_data_seek($rsPlatform, 0);
				  $row_rsPlatform = mysql_fetch_assoc($rsPlatform);
		<span class="tiny">Months</span>

I have tried to get the value this way, but it doesn't work:

$v = $_POST['maintenanceint'];
$mdate = mdate('Y-m-d');
$mdate = strtotime(date("Y-m-d", strtotime($mdate)) . " +$v month");
$mdate = date("Y-m-d", $mdate);
<input type="hidden" name="nextmaint" value="<?php echo htmlspecialchars($mdate); ?>" />


You didn't show the code for the <form> but whatever method it has wouldn't you just need to get the value of nextmaint variable?


If you look couple posts earlier I have posted entire page code.

Nextmaint is the result of formula maintenanceint + entry date


Sorry. Let me rephrase that.

... the POST or GET nextmaint variable.

In other words, not what is giving that input its value, but how you get the value when the form is submitted.

Or are you not asking that?


I need to get the maintenanceint value when user selects it (because nextmaint value should be calculated at the same page and recorded when submit button pressed). All the select-able options are taken from DB.


The code posted earlier is old obsolete code. What's your current attempt at PDO database code look like?


When you view the HTML of your form, what values is it putting into the option tags? Are they the number of months for the maintenance interval? That is, are the values appearing correctly?

I ask because the code appears to do what you want - retrieve the selected option value, add it to todays date as a number of months, then format the date. Obviously I can't tell from the names whether you're outputting the correct values - "platform" as a column name doesn't suggest that it contains a maintenance interval to me.

This bit of code:

<option value="<?php echo $row_rsPlatform['platform']?>"><?php echo $row_rsPlatform['platform']?></option>

could be simplified. Because the value that you submit to the handler code is the same as the value you display for the user to select, there's no need to specify the value separately.

And this bit

 $rows = mysql_num_rows($rsPlatform);

isn't needed, because you already have this line earlier

$totalRows_rsPlatform = mysql_num_rows($rsPlatform);

What does "doesn't work" mean? Where exactly does that code sit in relation to the total code dump you showed earlier? That is, why are you sticking it in a hidden form entry, when it needs to be in the processing section?


Platform is basically a selection table for maintint which has values of 1, 3, 6, 12 (which is months), yes it could be named differently but I left as it was.

The code inserted at the bottom:

$v = $_POST['maintenanceint'];
$mdate = mdate('Y-m-d');
$mdate = strtotime(date("Y-m-d", strtotime($mdate)) . " +$v month");
$mdate = date("Y-m-d", $mdate);
<input type="hidden" name="nextmaint" value="<?php echo $mdate; ?>" />

<?php include('includes/footer.php'); ?>

Is it bad place to put it there?


Yes, it needs to be inside the form processing section of your code, because you only have a value in the $_POST array when you click on the "submit" button.

So it needs to be inside the section that starts here:

if ((isset($_POST["MM_insert"])) && ($_POST["MM_insert"] == "form1")) {

Put it before your sprintf() function, because you'll need to use the value you calculate in that function.


This is totally unreadable. I would suggest to use DateTimeImmutable to create a more readable version:

$mdate = new DateTimeImmutable()->modify("+$v month")->format('Y-m-d');