-1

Question 1: I have multi line inputs(dynamically generated) which is storing the input value in MySql database. I have a input field which gets value only once and I want this value to be stored in the table repeatedly.

Question 2: How can I Increment date in dynamically generated field by one day using the user input as first date and pass the incremented date to my database?

LINK TO THE CODE

MY insert.php file:-

<?php
//insert.php
$connect = mysqli_connect("localhost", "root", "", "crondiet");
if(isset($_POST["break_name"]))
{
 $date_entry = $_POST["date_entry"];
 $cid_code = $_POST["cid_code"];
 $break_name = $_POST["break_name"];
 $mid_meal = $_POST["mid_meal"];

 $lunch_name = $_POST["lunch_name"];

 $evening_snacks = $_POST["evening_snacks"];
 $dinner_name = $_POST["dinner_name"];

 print_r($date_entry);

 $query = '';
 for($count = 0; $count<count($break_name); $count++)
 {
  $date_entry_clean = mysqli_real_escape_string($connect, $date_entry[$count]);
  $cid_code_clean = mysqli_real_escape_string($connect, $cid_code[$count]);
  $break_name_clean = mysqli_real_escape_string($connect, $break_name[$count]);
  $mid_meal_clean = mysqli_real_escape_string($connect, $mid_meal[$count]);
  $lunch_name_clean = mysqli_real_escape_string($connect, $lunch_name[$count]);
  $evening_snacks_clean = mysqli_real_escape_string($connect, $evening_snacks[$count]);
  $dinner_name_clean = mysqli_real_escape_string($connect, $dinner_name[$count]);
  if($date_entry_clean != '' &&$cid_code_clean != '' && $break_name_clean != ''&& $mid_meal_clean != '' && $lunch_name_clean != ''&& $evening_snacks_clean != '' && $dinner_name_clean != '')
  {
   $query .= '
   INSERT INTO dietwithmoremeals(date_entry,cid_code,break_name,mid_meal,lunch_name,evening_snacks,dinner_name) 
   VALUES("'.$date_entry_clean.'","'.$cid_code_clean.'","'.$break_name_clean.'","'.$mid_meal_clean.'",  "'.$lunch_name_clean.'","'.$evening_snacks_clean.'", "'.$dinner_name_clean.'"); 
   ';
  }
 }

 if($query != '')
 {
  if(mysqli_multi_query($connect, $query))
  {
   echo 'Item Data Inserted';
  }
  else
  {
   echo 'Error';
  }
 }
 else
 {
  echo 'All Fields are Required';
 }
}
?>
Parth Pandya
  • 1,050
  • 7
  • 15
  • 22
Sahil Kashyap
  • 329
  • 2
  • 10
  • Question 2: DATE_ADD, http://www.devshed.com/c/a/mysql/date-arithmetic-with-mysql/ – Nic3500 Jun 08 '18 at 11:31
  • Question 1: not sure I understand, you can reuse a variable more than once... Not clear to me. – Nic3500 Jun 08 '18 at 11:32
  • I'm unable to write proper JQUERY code. I want user to input date and then store the date in mysql db and whenever the user press "add" button that date gets incremented and goes in db. I'm generating the date box over and over and it is asking for input and if I remove the dynamically generated date box it says data offset. I know the problem is with the array and using the this keyword for user input. I tried passing a variable but it is not working. – Sahil Kashyap Jun 09 '18 at 08:56
  • I also want the customer ID to be passed once by the user and then that customer ID gets copied multiple times, MY solution was to get the value of coustmer ID input field in a variable and then passing the variable to the array using loop. I tried but it's not working.I write awful jquery code. see the link https://jsfiddle.net/Physcocybernatics/jv7tn5gc/ – Sahil Kashyap Jun 09 '18 at 09:04
  • Learn about prepared statements to prevent sql injection – Jens Jun 09 '18 at 09:31

1 Answers1

0

In your query you can add one day to your inserted values:

   $query .= '
   INSERT INTO dietwithmoremeals(date_entry,cid_code,break_name,mid_meal,lunch_name,evening_snacks,dinner_name) 
   VALUES("'.$date_entry_clean.'" + INTERVAL 1 DAY,"'.$cid_code_clean.'","'.$break_name_clean.'","'.$mid_meal_clean.'",  "'.$lunch_name_clean.'","'.$evening_snacks_clean.'", "'.$dinner_name_clean.'"); 
   ';
A. Colonna
  • 852
  • 7
  • 10
  • Thanks it is increment date but all the inputted dates are getting incremented by one. I cannot figure out how to take just one date and store it and then increment the date by one. :) I'm unable to write proper jquery code for it. THANKS A LOT – Sahil Kashyap Jun 09 '18 at 08:47