-1

hi friends I have created dropdown dates while inserting user details into database.Here when i input one of the dropdown dates it dosen't get updated into database ,instead it gives default value 1970-01-01.Here i checked in my jquery the line var end_date=$("#end_date").val() when i give alert for this it dosen;t alert any value for end_date.Can u please point out where i have gone wrong, thank you in advance. chapter_subscription_search.php

<?php
include('db.php');
$start_date=date("Y-m-d");
?>
<html>
<head><title>Chapter Subscriptions</title>
<script src="js/jquery-1.12.4.js"></script>
  <script src="js/jquery-ui.js"></script>
  </head>
  <body>
  <header> <img src="images/ipoint.png" class="logo" /> USER REGISTRATION</header>
<div class="container">
<h1 style="text-align:center">ADDING THE CHAPTER SUBSCRIPTION DETAILS</h1>
<style>
#display {
color:red;
font-size:12px;
text-align:center;
}
.logo {
padding:5px;
float:left;
}
header {
background-color:#074e7c;
height:60px;
width:100%;
text-align:center;
color:white;
font-size:40px;

}
#wrap {
text-align:center;
}
</style>
<form name="chapter_subscriptions" id="chapter_subscriptions" action="#" method="post">
<table align='center' border='1'>

    <tr>

           <td> <label for="userid">UserId</label></td>
            <td ><input id="userid" name="userid"  type="text" /></td>


        </tr>
        <tr>
        <td> <label for="chapter_no">ChapterNumber</label></td>
        <td ><input id="chapter_no" name="chapter_no" type="text"/></td>
        </tr>
        <tr>
        <td> <label for="start_date">StartDate</label></td>
        <td ><input id="start_date" name="start_date" type="text"/></td>
        </tr>
        <tr>
        <td> <label for="end_date">EndDate</label></td>
        <td ><select name="end_date" id="end_date">
<option value=<?php $end_date = date('Y-m-d', strtotime("+3 months"));?>><?php echo $end_date; ?></option>
<option value=<?php $end_date = date('Y-m-d', strtotime("+6 months"));?>><?php echo $end_date;?> </option>
<option value=<?php $end_date = date('Y-m-d', strtotime("+9 months"));?>><?php echo $end_date;?> </option>

</select>
</td>
        </tr>



      <tr>

           <td> <label for="chap_lic">License</label></td>
           <td ><input id="chap_lic" name="chap_lic" type="text" />  </td>

        </tr>
        </table>
        <div id="wrap">
        <input type="submit" name="insert" id="insert" value="insert">
        <input type="submit" name="edit" id="edit" value="edit">
        </div>

        </form>
        </div>
<script type="text/javascript">
$(document).ready(function(){
    $("#insert").click(function(e){

        var userid = $("#userid").val();
        var chapter_no = $("#chapter_no").val();
        var start_date = $("#start_date ").val();
        var end_date = $("#end_date").val();
        var chap_lic = $("#chap_lic").val();
        var dataString='userid='+userid+'&chapter_no='+chapter_no+'&start_date='+start_date+'&end_date='+end_date+'&chap_lic='+chap_lic;
        alert(dataString);
        if(userid==""||chapter_no==""||chap_lic=="") {
            document.getElementById("display").innerHTML="Please Enter The Fields";
        } else {
            $.ajax({
                type: "POST",
                url: "chapter_subscription_insert.php",
                data: dataString,
                cache: false,
                success: function(result){
                    //alert("submitted"+result);
                    $('#display').html(result);

                },
                error: function (xhr, ajaxOptions, thrownError) {
                    alert(xhr.status);
                    alert(thrownError);
                }
            });
            e.preventDefault(); 
        }
    });
});
</script>

            <div id="display">
            </div>

        </body>

        </html>

chapter_subscription_insert.php

<html>
<head><title>Insert</title></head>
<body>
<?php
include('db.php');
?>
<div id="display">
<?php
$userid=mysql_real_escape_string($_POST['userid']);
$chapter_no=mysql_real_escape_string($_POST['chapter_no']);
$start_date=mysql_real_escape_string($_POST['start_date']);
$start_date=date("Y-m-d");

$end_date1 = mysql_real_escape_string(($_POST['end_date']));


$end_date=date("Y-m-d",strtotime($end_date1));
//echo $end_date;
$chap_lic=mysql_real_escape_string($_POST['chap_lic']);
$str="insert into chapter_subscriptions(userId,chapter_no,start_date,end_date,chap_lic) values($userid,$chapter_no,'$start_date','$end_date','$chap_lic')";

$query=mysql_query($str);
if($query)
{
    $display="Success";
} else {
    $display="Failed";
}
echo $display;
?>
</div>
</body>
</html>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Vradhit
  • 59
  • 6
  • what's the date column's type? – Funk Forty Niner Mar 17 '17 at 11:52
  • 1
    Your script is at risk of [SQL Injection Attack](http://stackoverflow.com/questions/60174/how-can-i-prevent-sql-injection-in-php) Have a look at what happened to [Little Bobby Tables](http://bobby-tables.com/) Even [if you are escaping inputs, its not safe!](http://stackoverflow.com/questions/5741187/sql-injection-that-gets-around-mysql-real-escape-string) Use [prepared parameterized statements](http://php.net/manual/en/mysqli.quickstart.prepared-statements.php) – RiggsFolly Mar 17 '17 at 11:52
  • Does dataString not need to be a JSON object for the ajax request? – Jamie Mar 17 '17 at 11:53
  • Take a look at the `page source` I bet there is no date in the `value=""` attribute of your ` – RiggsFolly Mar 17 '17 at 11:53
  • `mysql_real_escape_string(($_POST['end_date']))` there shouldn't be 2 bracket sets here btw. – Funk Forty Niner Mar 17 '17 at 11:53
  • Every time you use [the `mysql_`](http://stackoverflow.com/questions/12859942/why-shouldnt-i-use-mysql-functions-in-php) database extension in new code **[a Kitten is strangled somewhere in the world](http://2.bp.blogspot.com/-zCT6jizimfI/UjJ5UTb_BeI/AAAAAAAACgg/AS6XCd6aNdg/s1600/luna_getting_strangled.jpg)** it is deprecated and has been for years and is gone for ever in PHP7. If you are just learning PHP, spend your energies learning the `PDO` or `mysqli` database extensions and prepared statements. [Start here](http://php.net/manual/en/book.pdo.php) – RiggsFolly Mar 17 '17 at 11:54
  • Some sensible code indentation would be a good idea. It helps us read the code and more importantly it will help **you debug your code** [Take a quick look at a coding standard](http://www.php-fig.org/psr/psr-2/) for your own benefit. You may be asked to amend this code in a few weeks/months and you will thank me in the end. – RiggsFolly Mar 17 '17 at 11:54
  • @RiggsFolly I have that toothache again. – Funk Forty Niner Mar 17 '17 at 11:55
  • @Fred-ii- [This comment](http://stackoverflow.com/questions/42856831/dropdown-date-is-not-updating-into-database-while-inserting#comment72819671_42856831) Why need 2 sets of braces – RiggsFolly Mar 17 '17 at 11:56
  • @RiggsFolly for better bracing? lol no idea. Ok, I'm out. – Funk Forty Niner Mar 17 '17 at 11:57
  • 2
    Serious you need to take people's comments seriously... all your mysql questions, you have been told to stop using `mysql_*` functions, but u continue using them – Masivuye Cokile Mar 17 '17 at 11:58
  • @MasivuyeCokile comments are too often ignored. +1 – Funk Forty Niner Mar 17 '17 at 12:09
  • @Fred-ii- people are just looking for magic answers, then comeback again the following day ask another question, but what they ask was mentioned in the comments – Masivuye Cokile Mar 17 '17 at 12:10
  • @MasivuyeCokile *sigh*, I know this all too well. If they all knew how to "ping" someone, it'd make it all that much more easier for everyone. Then again, many do not know how "Stack rolls" and just post, leave, then come back hoping for what you said; a "magic" answer. *C'est la vie!* – Funk Forty Niner Mar 17 '17 at 12:12
  • @MasivuyeCokile [It's happening again...](http://stackoverflow.com/q/42857229/) – Funk Forty Niner Mar 17 '17 at 12:30

2 Answers2

2

you have not echoed the values of option:

        <select name="end_date" id="end_date">
            <option value=<?php $end_date = date('Y-m-d', strtotime("+3 months"));?>><?php echo $end_date; ?></option>
            <option value=<?php $end_date = date('Y-m-d', strtotime("+6 months"));?>><?php echo $end_date;?> </option>
            <option value=<?php $end_date = date('Y-m-d', strtotime("+9 months"));?>><?php echo $end_date;?> </option>

        </select>

change above code to

         $enddate1 = date('Y-m-d', strtotime("+3 months"));
         $enddate2 = date('Y-m-d', strtotime("+6 months"));
         $endate3 = date('Y-m-d', strtotime("+9 months"));
         <select name="end_date" id="end_date">
             <option value=<?php echo $end_date1; ?>><?php echo $end_date1; ?></option>
             <option value=<?php echo $end_date2; ?>><?php echo $end_date2; ?> </option>
             <option value=<?php echo $end_date3;?>><?php echo $end_date3;?> </option>

        </select>
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
rupesh
  • 277
  • 3
  • 15
0

Error is your dropdown

value = "<?php echo $end_date = date('Y-m-d', strtotime("+3 months"));?>"

<select name="end_date" id="end_date">
    <option value=<?php echo $end_date = date('Y-m-d', strtotime("+3 months"));?>><?php echo $end_date; ?></option>
    <option value=<?php echo $end_date = date('Y-m-d', strtotime("+6 months"));?>><?php echo $end_date;?> </option>
    <option value=<?php echo $end_date = date('Y-m-d', strtotime("+9 months"));?>><?php echo $end_date;?> </option>
</select>
GYaN
  • 2,327
  • 4
  • 19
  • 39