Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Mar 2012
    Location
    UK
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Code example required

    Hi,

    Newbie here, so sorry if this is stupid question.

    Would some one be kind enought to give me a example of some PHP and SQL code, I have looked on the net for an answer and not been able to find what I am looking for, or I am not knowledgable to recognise what I need.

    I have a form that has normal text variables, one is a html select variable and another is a variable that is selected from a sql table.

    What I could use is the code to insert the data to a table as what I have tried just doesnt work.

    As I am here I may as well ask as I know I am going to have trouble with it. I would like a date inserted into my database which is selected from html drop boxes. The drop boxes produce the 3 numbers for the date but how do I input those 3 mumbers into one date field as a date.

    Thanks in advance

  • #2
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,297
    Thanks
    23
    Thanked 612 Times in 611 Posts
    I add a little to the code at http://www.javascriptkit.com/script/...ateform2.shtml

    You can change things to get the date the way you want to store it.
    Code:
    <html>
    <head></head>
    <script type="text/javascript">
    
    /***********************************************
    * Drop Down Date select script- by JavaScriptKit.com
    * This notice MUST stay intact for use
    * Visit JavaScript Kit at http://www.javascriptkit.com/ for this script and more
    ***********************************************/
    
    var monthtext=['Jan','Feb','Mar','Apr','May','Jun','Jul','Aug','Sept','Oct','Nov','Dec'];
    
    function populatedropdown(dayfield, monthfield, yearfield){
    var today=new Date()
    var dayfield=document.getElementById(dayfield)
    var monthfield=document.getElementById(monthfield)
    var yearfield=document.getElementById(yearfield)
    for (var i=0; i<31; i++)
    dayfield.options[i]=new Option(i, i+1)
    dayfield.options[today.getDate()]=new Option(today.getDate(), today.getDate(), true, true) //select today's day
    for (var m=0; m<12; m++)
    monthfield.options[m]=new Option(monthtext[m], monthtext[m])
    monthfield.options[today.getMonth()]=new Option(monthtext[today.getMonth()], monthtext[today.getMonth()], true, true) //select today's month
    var thisyear=today.getFullYear()
    for (var y=0; y<20; y++){
    yearfield.options[y]=new Option(thisyear, thisyear)
    thisyear+=1
    }
    yearfield.options[0]=new Option(today.getFullYear(), today.getFullYear(), true, true) //select today's year
    }
    
    function day_date()
    {
    	day_date = document.getElementById('yeardropdown').value;
    	day_date += '-'+document.getElementById('monthdropdown').value;
    	day_date += '-'+document.getElementById('daydropdown').value;
    	document.getElementById('post_date').innerHTML = day_date;
    }
    </script>
    
    <body onLoad="checkPage();">
    
    <form action="" name="someform">
    <select id="daydropdown">
    </select>
    <select id="monthdropdown">
    </select>
    <select id="yeardropdown">
    </select>
    </form>
    
    
    <input type="button" value="Get Date" onclick="day_date();">
    <div id="post_date"></div>
    
    <script type="text/javascript">
    
    //populatedropdown(id_of_day_select, id_of_month_select, id_of_year_select)
    window.onload=function(){
    populatedropdown("daydropdown", "monthdropdown", "yeardropdown")
    }
    </script>
    </body>
    </html>

  • #3
    New Coder
    Join Date
    Mar 2012
    Location
    UK
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks sunfighter, ill take a look at that.

    As I didnt get an answer to my primary question I thought I would post my code that doesnt work. Please remember when you look at it that I am very much a newbie, so when you have finshed laughing and picked yourself off the floor could someone tell me why it doesnt work.

    Thank you guys.

    PHP Code:
    <?php
        
    require ('includes/config.inc.php');
        require (
    MYSQL);
        
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Falconry Logs</title>
    <style type="text/css">
    <!--
    -->
    </style>
    <link href="styles/mystyles.css" rel="stylesheet" type="text/css" />
    </head>

    <body>

    <div class="container">
      <div class="header"><a href="#"><img src="Images/logo.jpg" width="961" height="100" alt="Logo" /></a> 
        </div>
      <div class="sidebar1">
        <?php    
       
    // ob_start();
       
    session_start();

    if(
    $_SESSION['user_id'] > '1'){
          include(
    'includes/navbar1.inc.php');
    } else {
          include(
    'includes/navbar.inc.php');
    }  
             
    ?>
        </div>
      <div class="content">
         <h3 align="left" class="style2">Add your bird(s) to the database</h3>
        <form action="add_bird.php" method="post">
        <fieldset>
        <p><b>Bird Name:</b> <input type="text" name="bird_name" size="30" maxlength="30" /></p>
        <p><b>Sex:</b> <select name="sex"> <option value="Female">Female</option> <option value="Male">Male</option> </select>
        <p><b>Species:</b> 
        <?php
        $q 
    = ("SELECT common_name, species_id FROM species");
        
    $r mysqli_query($dbc$q);
        
    ?>
        <select id="species_id" name="species_id">    
        <option value=''></option>    
      <?php 
        
    while ($row mysqli_fetch_array($r)) 
        {       
        echo 
    "<option value=\"".$row['species_id']."\">".$row['common_name']."</option>\n  ";
       }   
       
    ?> </select>
       <?php
    //$todo=$_POST['todo'];
    if(isset($todo) and $todo=="submit"){
    $month=$_POST['month'];
    $dt=$_POST['dt'];
    $year=$_POST['year'];
    $date_value="$dt/$month/$year";
    echo 
    "dd/mm/yyyy format :$date_value<br>";
    }
    ?>
    <form method=post name=f1 action=''><input type=hidden name=todo value=submit>
    <p><b>Date of birth / Ownership:
    Date<select name=dt >
    <option value='01'>01</option>
    <option value='02'>02</option>
    <option value='03'>03</option>
    <option value='04'>04</option>
    <option value='05'>05</option>
    <option value='06'>06</option>
    <option value='07'>07</option>
    <option value='08'>08</option>
    <option value='09'>09</option>
    <option value='10'>10</option>
    <option value='11'>11</option>
    <option value='12'>12</option>
    <option value='13'>13</option>
    <option value='14'>14</option>
    <option value='15'>15</option>
    <option value='16'>16</option>
    <option value='17'>17</option>
    <option value='18'>18</option>
    <option value='19'>19</option>
    <option value='20'>20</option>
    <option value='21'>21</option>
    <option value='22'>22</option>
    <option value='23'>23</option>
    <option value='24'>24</option>
    <option value='25'>25</option>
    <option value='26'>26</option>
    <option value='27'>27</option>
    <option value='28'>28</option>
    <option value='29'>29</option>
    <option value='30'>30</option>
    <option value='31'>31</option>
    </select>
    </b><select name=month value=''>Select Month</option>
    <option value='01'>January</option>
    <option value='02'>February</option>
    <option value='03'>March</option>
    <option value='04'>April</option>
    <option value='05'>May</option>
    <option value='06'>June</option>
    <option value='07'>July</option>
    <option value='08'>August</option>
    <option value='09'>September</option>
    <option value='10'>October</option>
    <option value='11'>November</option>
    <option value='12'>December</option>
    </select>

    Year(yyyy)<input type=text name=year size=4 value=2012>

    <p><b>Breeder</b> (Optional): <input type="text" name="breeder" size="30" maxlength="30" /></p>
    <p><b>Mentor</b>  (Optional):<input type="text" name="mentor" size="30" maxlength="30" /></p>

    </p>

       <div align="center"><input type="submit" name="submit" value="Add bird to database" /></div>
        </form>    
        <?php

        $page_title 
    'add_bird';
          if (
    $_SERVER['REQUEST_METHOD'] == 'POST') { // Handle the form.
      
          // Need the database connection:
        // require (MYSQL);
        
        // Trim all the incoming data:
        
    $trimmed array_map('trim'$_POST);

        
    // Assume invalid values:
        
    $bn $sex $species FALSE;
        
        
    // Check for a bird name:
        
    if (preg_match ('/^[A-Z \'.-]{2,20}$/i'$trimmed['bird_name'])) {
            
    $bn mysqli_real_escape_string ($dbc$trimmed['bird_name']);
        } else {
            echo 
    '<p class="error">Please enter your birds name!</p>';
            }

        
    // Check for a sex:
        
    if (preg_match ('/^[A-Z \'.-]{2,40}$/i'$trimmed['sex'])) {
            
    $sex mysqli_real_escape_string ($dbc$trimmed['sex']);
        } else {
            echo 
    '<p class="error">Please enter the sex of your bird!</p>';
        }
        
        
    // Check for a species:
        
    if (preg_match ('/^[A-Z \'.-]{2,40}$/i'$trimmed['species_id'])) {
            
    $species mysqli_real_escape_string ($dbc$trimmed['species_id']);
        } else {
            echo 
    '<p class="error">Please enter the sex of your bird!</p>';
        }
        
        
    // Check for a date:
        
    if (preg_match ('/^[A-Z \'.-]{2,40}$/i'$trimmed['dt'])) {
            
    $dt mysqli_real_escape_string ($dbc$trimmed['dt']);
        } else {
            echo 
    '<p class="error">Please enter the date of birth or date of ownership!</p>';
        }
        
        
    // Check for a month:
        
    if (preg_match ('/^[A-Z \'.-]{2,40}$/i'$trimmed['month'])) {
            
    $month mysqli_real_escape_string ($dbc$trimmed['month']);
        } else {
            echo 
    '<p class="error">Please enter the date of birth or date of ownership!</p>';
        }
        
        
    // Check for a year:
        
    if (preg_match ('/^[A-Z \'.-]{2,40}$/i'$trimmed['year'])) {
            
    $year mysqli_real_escape_string ($dbc$trimmed['year']);
        } else {
            echo 
    '<p class="error">Please enter the date of birth or date of ownership!</p>';
        }
            
        
    // Check for a breeder:
        
    if (preg_match ('/^[A-Z \'.-]{2,40}$/i'$trimmed['breeder'])) {
            
    $bd mysqli_real_escape_string ($dbc$trimmed['breeder']);
        } else {
            echo 
    '<p class="error">Please enter the breeder or previous owner</p>';
        }
        
        
    // Check for a mentor:
        
    if (preg_match ('/^[A-Z \'.-]{2,40}$/i'$trimmed['mentor'])) {
            
    $mt mysqli_real_escape_string ($dbc$trimmed['mentor']);
        } else {
            echo 
    '<p class="error">Please enter the breeder or previous owner</p>';
        }
        
        if (
    $user_id $bn $sex $species) { // If everything's OK...
        
        // Add the bird to the database:
                
    $q "INSERT INTO bird (user_id, bird_name, species_id, sex) VALUES ('$user_id', '$bn', '$species_id', '$sex')";
                
    $r mysqli_query ($dbc$q) or trigger_error("Query: $q\n<br />MySQL Error: " mysqli_error($dbc));

                if (
    mysqli_affected_rows($dbc) == 1) { // If it ran OK.
                
                // Finish the page:
                    
    echo '<h3>Bird Added</h3>';
                    exit();
                    
                } else {
                    
                    
    // If it did not run OK.
                    
    echo '<p class="error">Its gone horribly wrong!!.</p>';
                }
        } else { 
    // If one of the data tests failed.
            
    echo $user_id$bn$species$sex;
            echo 
    '<p class="error">Please try again.</p>';
        }

        
    mysqli_close($dbc);

    // End of the main Submit conditional.
    ?> 
        
        </div>
      <div class="footer">
        <p>
        <?php include('./includes/footer.inc.php'); ?>
        </p>
        </div>
      </div>
    </body>
    </html>

  • #4
    New Coder
    Join Date
    Mar 2012
    Location
    UK
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts
    My current code

    PHP Code:
    <?php
        
    require ('includes/config.inc.php');
        require (
    MYSQL);
        
    // Assume invalid values:
        
    $bn $sex $species $month $dt $year $date_value $db $mt FALSE;
        
    ?>
        
        
        <?php

        $page_title 
    'add_bird';
          if (
    $_SERVER['REQUEST_METHOD'] == 'POST') { // Handle the form.
      
          // Trim all the incoming data:
        
    $trimmed array_map('trim'$_POST);

        
        
        
    // Check for a bird name:
        
    if (preg_match ('/^[A-Z \'.-]{2,20}$/i'$trimmed['bird_name'])) {
            
    $bn mysqli_real_escape_string ($dbc$trimmed['bird_name']);
        } else {
            echo 
    '<p class="error">Please enter your birds name!</p>';
        }

        
    // Check for a sex:
        
    if (preg_match ('/^[A-Z \'.-]{2,40}$/i'$trimmed['sex'])) {
            
    $sex mysqli_real_escape_string ($dbc$trimmed['sex']);
        } else {
            echo 
    '<p class="error">Please enter the sex of your bird!</p>';
        }
        
        
    // Check for a species:
        
    if (preg_match ('/^[A-Z \'.-]{2,40}$/i'$trimmed['species_id'])) {
            
    $species mysqli_real_escape_string ($dbc$trimmed['species_id']);
        } else {
            echo 
    '<p class="error">Please enter the sex of your bird!</p>';
        }
        
        
    // Check for a date:
        
    if (preg_match ('/^[A-Z \'.-]{2,40}$/i'$trimmed['dt'])) {
            
    $dt mysqli_real_escape_string ($dbc$trimmed['dt']);
        } else {
            echo 
    '<p class="error">Please enter the date of birth or date of ownership!</p>';
        }
        
        
    // Check for a month:
        
    if (preg_match ('/^[A-Z \'.-]{2,40}$/i'$trimmed['month'])) {
            
    $month mysqli_real_escape_string ($dbc$trimmed['month']);
        } else {
            echo 
    '<p class="error">Please enter the date of birth or date of ownership!</p>';
        }
        
        
    // Check for a year:
        
    if (preg_match ('/^[A-Z \'.-]{2,40}$/i'$trimmed['year'])) {
            
    $year mysqli_real_escape_string ($dbc$trimmed['year']);
        } else {
            echo 
    '<p class="error">Please enter the date of birth or date of ownership!</p>';
        }
            
        
    // Check for a breeder:
        
    if (preg_match ('/^[A-Z \'.-]{2,40}$/i'$trimmed['breeder'])) {
            
    $bd mysqli_real_escape_string ($dbc$trimmed['breeder']);
        } else {
            echo 
    '<p class="error">Please enter the breeder or previous owner</p>';
        }
        
        
    // Check for a mentor:
        
    if (preg_match ('/^[A-Z \'.-]{2,40}$/i'$trimmed['mentor'])) {
            
    $mt mysqli_real_escape_string ($dbc$trimmed['mentor']);
        } else {
            echo 
    '<p class="error">Please enter the breeder or previous owner</p>';
        }
        
        if (
    $user_id $bn $sex $species $month $dt $year $date_value $bd) { // If everything's OK...
        
        
    $
        
        
    // Add the bird to the database:
                
    $q "INSERT INTO bird (user_id, bird_name, species, sex, dob, breeder, mentor) VALUES ('$user_id', '$bn', '$species', '$sex', '$date_value', '$bd', '$mt')";
                
    $r mysqli_query ($dbc$q) or trigger_error("Query: $q\n<br />MySQL Error: " mysqli_error($dbc));

                if (
    mysqli_affected_rows($dbc) == 1) { // If it ran OK.
                
                // Finish the page:
                    
    echo '<h3>Bird Added</h3>';
                    echo (
    " user_id = $user_id ,
                            bird_name = $bn , 
                            sex = $sex ,
                            species = $species ,
                            month = $month ,
                            date = $dt ,
                            year = $year ,
                            date value = $date_value ,
                            breeder = $bd ,
                            mentor = $mt"
    );
                            
                    exit();
                    
                } else {
                    
                    
    // If it did not run OK.
                    
    echo '<p class="error">Its gone horribly wrong!!.</p>';
                }
        } else { 
    // If one of the data tests failed.
            
    echo $user_id$bn$species$sex;
            echo 
    '<p class="error">Please try again.</p>';
        }

        
    mysqli_close($dbc);
        }
     
    // End of the main Submit conditional.
    ?> 



    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Falconry Logs</title>
    <style type="text/css">
    <!--
    -->
    </style>
    <link href="styles/mystyles.css" rel="stylesheet" type="text/css" />
    </head>

    <body>

    <div class="container">
      <div class="header"><a href="#"><img src="Images/logo.jpg" width="961" height="100" alt="Logo" /></a> 
        </div>
      <div class="sidebar1">
        <?php    
       
    // ob_start();
       
    session_start();

    if(
    $_SESSION['user_id'] > '1'){
          include(
    'includes/navbar1.inc.php');
    } else {
          include(
    'includes/navbar.inc.php');
    }  
             
    ?>
        </div>
      <div class="content">
         <h3 align="left" class="style2">Add your bird(s) to the database</h3>
        <form action="add_bird.php" method="post">
        <fieldset>
        <p><b>Bird Name:</b> <input type="text" name="bird_name" size="30" maxlength="30" /></p>
        <p><b>Sex:</b> <select name="sex"> <option value="Female">Female</option> <option value="Male">Male</option> </select>
        <p><b>Species:</b> 
        <?php
        $q 
    = ("SELECT common_name, species_id FROM species");
        
    $r mysqli_query($dbc$q);
        
    ?>
        <select id="species_id" name="species_id">    
        <option value=''></option>    
      <?php 
        
    while ($row mysqli_fetch_array($r)) 
        {       
        echo 
    "<option value=\"".$row['species_id']."\">".$row['common_name']."</option>\n  ";
       }   
       
    ?> </select>
       <?php
    //$todo=$_POST['todo'];
    if(isset($todo) and $todo=="submit"){
    $month=$_POST['month'];
    $dt=$_POST['dt'];
    $year=$_POST['year'];
    $date_value="$dt/$month/$year";
    echo 
    "dd/mm/yyyy format :$date_value<br>";
    }
    ?>
    <form method=post name=f1 action=''><input type=hidden name=todo value=submit>
    <p><b>Date of birth / Ownership:
    Date<select name=dt >
    <option value='01'>01</option>
    <option value='02'>02</option>
    <option value='03'>03</option>
    <option value='04'>04</option>
    <option value='05'>05</option>
    <option value='06'>06</option>
    <option value='07'>07</option>
    <option value='08'>08</option>
    <option value='09'>09</option>
    <option value='10'>10</option>
    <option value='11'>11</option>
    <option value='12'>12</option>
    <option value='13'>13</option>
    <option value='14'>14</option>
    <option value='15'>15</option>
    <option value='16'>16</option>
    <option value='17'>17</option>
    <option value='18'>18</option>
    <option value='19'>19</option>
    <option value='20'>20</option>
    <option value='21'>21</option>
    <option value='22'>22</option>
    <option value='23'>23</option>
    <option value='24'>24</option>
    <option value='25'>25</option>
    <option value='26'>26</option>
    <option value='27'>27</option>
    <option value='28'>28</option>
    <option value='29'>29</option>
    <option value='30'>30</option>
    <option value='31'>31</option>
    </select>
    </b><select name=month value=''>Select Month</option>
    <option value='01'>January</option>
    <option value='02'>February</option>
    <option value='03'>March</option>
    <option value='04'>April</option>
    <option value='05'>May</option>
    <option value='06'>June</option>
    <option value='07'>July</option>
    <option value='08'>August</option>
    <option value='09'>September</option>
    <option value='10'>October</option>
    <option value='11'>November</option>
    <option value='12'>December</option>
    </select>

    Year(yyyy)<input type=text name=year size=4 value=2012>

    <p><b>Breeder</b> (Optional): <input type="text" name="breeder" size="30" maxlength="30" /></p>
    <p><b>Mentor</b>  (Optional):<input type="text" name="mentor" size="30" maxlength="30" /></p>

    </p>

       <div align="center"><input type="submit" name="submit" value="Add bird to database" /></div>
        </form>    
        </div>
      <div class="footer">
        <p>
        <?php include('./includes/footer.inc.php'); ?>
        </p>
        </div>
      </div>
    </body>
    </html>
    My Errors!!
    [dbc] => mysqli Object
    (
    )

    [mt] => Louis
    [db] =>
    [date_value] => Miguel
    [year] => Miguel
    [dt] => Miguel
    [month] => Miguel
    [species] => Miguel
    [sex] => Miguel
    [bn] => Miguel
    [page_title] => add_bird
    [trimmed] => Array
    (
    [bird_name] => test
    [sex] => Male
    [species_id] => 2
    [todo] => submit
    [dt] => 04
    [month] => 03
    [year] => 2011
    [breeder] => Miguel
    [mentor] => Louis
    [submit] => Add bird to database
    )

  • #5
    Senior Coder
    Join Date
    Jan 2011
    Location
    Missouri
    Posts
    4,297
    Thanks
    23
    Thanked 612 Times in 611 Posts
    At line 148 you have
    Code:
        <form action="add_bird.php" method="post">
        <fieldset>
    neither have a close tag. And you later have
    Code:
    <form method=post name=f1 action=''><input type=hidden name=todo value=submit>
    on line 176. I think the 176 is where you want the form to start.

    This code block
    PHP Code:
       <?php
    //$todo=$_POST['todo'];
    if(isset($todo) and $todo=="submit"){
    $month=$_POST['month'];
    $dt=$_POST['dt'];
    $year=$_POST['year'];
    $date_value="$dt/$month/$year";
    echo 
    "dd/mm/yyyy format :$date_value<br>";
    }
    ?>
    Never gets a chance to exicute. I think a version of it should be placed in add_bird.php instead.

    You don't show add_bird.php. Why not give it a go and we'll see what is right and what is wrong with it. OK?

  • #6
    New Coder
    Join Date
    Mar 2012
    Location
    UK
    Posts
    11
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Thanks Sunfighter you help is very much appreciated.

    I have totally re-written my page and I have everything working except the date. The database is always getting the time as 0000-00-00.

    I have a variable which has the right date so I guess is a SQL issue. Here is my current SQL insert line.

    Can you tell me what is wrong as I am guessing at the DATE part of the command.

    Code:
    $q = "INSERT INTO bird (user_id, bird_name, sex, species_id, ringno, dob) VALUES ($_SESSION[user_id], '$_POST[bird_name]', '$_POST[sex]', $_POST[species_id], '$_POST[ringno]', 'DATE(%Y, %m, %d, [m_date]')";


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •