/    Sign up×
Community /Pin to ProfileBookmark

Need help with script

I’m desperate for a solution to a problem I have been experiencing for a while now.

[B][U]Background:[/U][/B]

I have created a relatively simple four field form. The visitor has the option whether to choose one or all four of the fields. The form has a search process. It searches a MySQL database against:

  • field 1 (name – if chosen),

  • field 2 (location – if chosen)
    and fields 3 & 4 (date to and date from – if chosen)
  • N.B: Date to and Date from are populated from a javascript pop up calender. I can not immediately see if this is adding to the problem or not.

    [B][U]Problem:[/U][/B]

    If the visitor is forced to choose all three options then the search process works fine and displays the correct results.

    If the visitor is able to pick and choose fields, the search results – although display results – does not display results according to the choices made.

    [B][U]Scripts:[/U][/B]

    The search form has the following fields and names:

    Name = nm3
    Location = County
    Start Date = start
    End Date = to

    The PHP search script is as follows:

    [CODE]if (!$_POST[‘start’]) {
    $start = ‘1900-01-01’ ;
    } else {
    $start = $_POST[‘start’] ;
    }

    if (!$_POST[‘to’]) {
    $to = ‘2050-01-01’ ;
    } else {
    $to = $_POST[‘to’] ;
    }

    if ((!$_POST[‘nm3’]) && (!$_POST[‘County’])) {
    $result = mysql_query(“select * from table WHERE date BETWEEN ‘$start’ and ‘$to’ “) or die(mysql_error());
    } else if (!$_POST[‘nm3’]) {
    $result = mysql_query(“select * from table WHERE county='{$_POST[‘County’]}’ and date BETWEEN ‘$start’ and ‘$to’ “) or die(mysql_error());
    } else if (!$_POST[‘County’]) {
    $result = mysql_query(“select * from table WHERE name='{$_POST[‘nm3’]}” and date BETWEEN ‘$start’ and ‘$to’ “) or die(mysql_error());
    } else {

    $result = mysql_query(“select * from table WHERE name=’$nm3′ and county=’$County’ and date BETWEEN ‘$start’ and ‘$to’ “) or die(mysql_error());
    }

    $result1 = mysql_num_rows($result);[/CODE]

    Any questions please feel free to ask….

    Thanks,
    CC

    to post a comment
    PHP

    16 Comments(s)

    Copy linkTweet thisAlerts:
    @Wisest_GuyJan 07.2008 — Change these 2 lines:
    [CODE] } else if (!$_POST['nm3']) {
    $result = mysql_query("select * from table WHERE county='{$_POST['County']}' and date BETWEEN '$start' and '$to' ") or die(mysql_error());
    } else if (!$_POST['County']) {
    $result = mysql_query("select * from table WHERE name='{$_POST['nm3']}'' and date BETWEEN '$start' and '$to' ") or die(mysql_error());
    [/CODE]

    To:
    [CODE] } else if (!$_POST['nm3']) {
    $result = mysql_query("select * from table WHERE county='".$_POST['County']."' and date BETWEEN '$start' and '$to' ") or die(mysql_error());
    } else if (!$_POST['County']) {
    $result = mysql_query("select * from table WHERE name='".$_POST['nm3']."' and date BETWEEN '$start' and '$to' ") or die(mysql_error());
    [/CODE]

    Also, you need to make sure the variables put in the query string are properly escaped or someone could ruin your tables.
    Copy linkTweet thisAlerts:
    @cc976aauthorJan 07.2008 — Thanks for the reply. I have made the changes but the search results have exactly the same affect.

    It simply displays all entries with a date. It does not narrow down by either name, location or date??? ?
    Copy linkTweet thisAlerts:
    @callumdJan 07.2008 — This might be easier if we could see your table structures and HTML form code.
    Copy linkTweet thisAlerts:
    @cc976aauthorJan 07.2008 — Yes, of course.

    The form itself is below:

    [CODE]<form name="sample" action="search.php">
    <table width="90%" border="0" cellspacing="0" cellpadding="0">
    <tr valign="middle">
    <td width="25%"><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Name:</font></td>
    <td width="75%">
    <input name="nm3" type="text" id="nm3">

    <font size="2" face="Verdana, Arial, Helvetica, sans-serif">&nbsp; </font></td>
    </tr>
    <tr>
    <td><font size="2" face="Verdana, Arial, Helvetica, sans-serif">&nbsp;</font></td>
    <td><font size="2" face="Verdana, Arial, Helvetica, sans-serif">&nbsp;</font></td>
    </tr>
    <tr>
    <td><font size="2" face="Verdana, Arial, Helvetica, sans-serif">County:</font></td>
    <td><font size="2" face="Verdana, Arial, Helvetica, sans-serif">
    <select name="County" id="County">
    <option value="">Please Select...</option>
    <option value="Bedfordshire">Bedfordshire</option>
    <option value="Berkshire">Berkshire</option>
    <option value="Buckinghamshire">Buckinghamshire</option>
    <option value="Cambridgeshire">Cambridgeshire</option>
    <option value="Cheshire">Cheshire</option>
    <option value="Cornwall">Cornwall</option>
    <option value="Cumberland">Cumberland</option>
    <option value="Derbyshire">Derbyshire</option>
    <option value="Devon">Devon</option>
    <option value="Dorset">Dorset</option>
    <option value="Durham">Durham</option>
    <option value="Essex">Essex</option>
    <option value="Gloucestershire">Gloucestershire</option>
    <option value="Hampshire">Hampshire</option>
    <option value="Herefordshire">Herefordshire</option>
    <option value="Hertfordshire">Hertfordshire</option>
    <option value="Huntingdonshire">Huntingdonshire</option>
    <option value="Kent">Kent</option>
    <option value="Lancashire">Lancashire</option>
    <option value="Leicestershire">Leicestershire</option>
    <option value="Lincolnshire">Lincolnshire</option>
    <option value="Middlesex">Middlesex</option>
    <option value="Norfolk">Norfolk</option>
    <option value="Northamptonshire">Northamptonshire</option>
    <option value="Northumberland">Northumberland</option>
    <option value="Oxfordshire">Oxfordshire</option>
    <option value="Rutland">Rutland</option>
    <option value="Shropshire">Shropshire</option>
    <option value="Somerset">Somerset</option>
    <option value="Staffordshire">Staffordshire</option>
    <option value="Suffolk">Suffolk</option>
    <option value="Surrey">Surrey</option>
    <option value="Sussex">Sussex</option>
    <option value="Warwickshire">Warwickshire</option>
    <option value="Westmorland">Westmorland</option>
    <option value="Wiltshire">Wiltshire</option>
    <option value="Worcestershire">Worcestershire</option>
    <option value="Yorkshire">Yorkshire</option>
    </select>
    </font></td>
    </tr>
    <tr>
    <td><font size="2" face="Verdana, Arial, Helvetica, sans-serif">&nbsp;</font></td>
    <td><font size="2" face="Verdana, Arial, Helvetica, sans-serif">&nbsp;</font></td>
    </tr>
    <tr>
    <td valign="top"><font size="2" face="Verdana, Arial, Helvetica, sans-serif">Date:</font></td>
    <td valign="top"><font size="2" face="Verdana, Arial, Helvetica, sans-serif">
    <input name="start" type="text" value="" size="15">
    <a href="javascript://" onClick="getCalendarFor(document.sample.start)"><img src="img/cal.gif" width="16" height="16" border="0" alt="Click Here to Pick up the date"></a> </input>
    From


    <input name="to" type="text" id="to" value="" size="15">
    <a href="javascript://" onClick="getCalendarFor(document.sample.to)"><img src="img/cal.gif" width="16" height="16" border="0" alt="Click Here to Pick up the date"></a> </input>
    To

    </font></td>
    </tr>
    <tr>
    <td><font size="2" face="Verdana, Arial, Helvetica, sans-serif">&nbsp;</font></td>
    <td><font size="2" face="Verdana, Arial, Helvetica, sans-serif">&nbsp;</font></td>
    </tr>
    <tr>
    <td><font size="2" face="Verdana, Arial, Helvetica, sans-serif">
    <input type="submit" name="Submit" value="Submit">
    </font></td>
    <td><font size="2" face="Verdana, Arial, Helvetica, sans-serif">&nbsp;</font></td>
    </tr>
    <tr>
    <td>&nbsp;</td>
    <td>&nbsp;</td>
    </tr>
    </table>
    </form>[/CODE]



    The table that shows the results on the Search page is as follows:

    [CODE]<table width="650" border="1" align="center">
    <tr>
    <td colspan="4">
    <?
    if ($result1 == 0)
    {
    echo "Sorry, we were not able to announcements that matches or includes your search criteria";

    }
    else if ($result1 == 1)
    {
    echo "We have found <b>1</b> match<br>";
    }
    else {
    echo "We have found <b>$result1</b> matches <br>";
    }


    ?>
    </td>
    </tr>
    <?php
    while($row = mysql_fetch_array($result))
    {
    ?>
    <tr valign="top">
    <td width="166"><?php echo $row[date] ; ?>,</td>
    <td width="175" height="60"><a href="<? echo $row[url] ; ?>"><?php echo $row[name] ; ?>,</a></td>
    <td width="162"><?php echo $row[county] ; ?>,</td>
    <td width="119"><img src="<?php echo $row[arrow] ; ?>"></td>
    </tr>
    <? } ?>
    </table>[/CODE]



    Thanks,

    CC
    Copy linkTweet thisAlerts:
    @stephan_gerlachJan 07.2008 — How about this one?

    [code=php]




    $sql = 'SELECT * FROM table WHERE ( date >= "'.$start.'" AND date <= "'.$to.'")';

    if (isset($_POST['nm3'])) {
    $sql .= ' AND name="'.$nm3.'" ';
    }

    if (isset($_POST['County'])) {
    $sql .= 'AND county="'.$County.'"';
    }

    [/code]



    Also another question. The submitted date is it in the form of YYYY-MM-DD and is the date in the column in the database also YYYY-MM-DD
    Copy linkTweet thisAlerts:
    @bathurst_guyJan 07.2008 — Please remember guys no talk regarding money is permitted on the forum, donations or otherwise. Keep it to the forum rules and you can remain a member.
    Copy linkTweet thisAlerts:
    @cc976aauthorJan 07.2008 — How about this one?

    [code=php]




    $sql = 'SELECT * FROM table WHERE ( date >= "'.$start.'" AND date <= "'.$to.'")';

    if (isset($_POST['nm3'])) {
    $sql .= ' AND name="'.$nm3.'" ';
    }

    if (isset($_POST['County'])) {
    $sql .= 'AND county="'.$County.'"';
    }

    [/code]



    Also another question. The submitted date is it in the form of YYYY-MM-DD and is the date in the column in the database also YYYY-MM-DD[/QUOTE]



    I have tried this but receive the following error:

    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /homepages/37/d218141466/htdocs/search.php on line 101


    Line 101 of search.php is while($row = mysql_fetch_array($sql))

    I am guessing that this is not valid?

    ...and yes date is stored in database under YYYY-MM-DD format


    Please remember guys no talk regarding money is permitted on the forum, donations or otherwise. Keep it to the forum rules and you can remain a member.[/QUOTE]

    Sorry, didn't realise. Don't expect help for free, but can understand the rules.


    Thanks,

    CC
    Copy linkTweet thisAlerts:
    @cc976aauthorJan 09.2008 — Does anyone have a solution? ?
    Copy linkTweet thisAlerts:
    @slevenJan 09.2008 — maybe you need to change your database table column type to DATE and default value '0000-00-00' ;

    another solution is using php datetime function to change time string to int .
    Copy linkTweet thisAlerts:
    @cc976aauthorJan 09.2008 — maybe you need to change your database table column type to DATE and default value '0000-00-00' ;

    another solution is using php datetime function to change time string to int .[/QUOTE]



    Database table coloumn type is DATE and default is '0000-00-00'

    ?
    Copy linkTweet thisAlerts:
    @cc976aauthorJan 09.2008 — I am hoping this is going to help a little more.

    I have made the search script really simplistic, as in

    [CODE]$sql = mysql_query ("SELECT * FROM announce WHERE date BETWEEN '$start' AND '$to' ");[/CODE]

    This works fine and displays the correct results in date range. If I change to:....

    [CODE] if (!$_POST['start']) {
    $start = '1900-01-01' ;
    } else {
    $start = $_POST['start'] ;
    }

    if (!$_POST['to']) {
    $to = '2050-01-01' ;
    } else {
    $to = $_POST['to'] ;
    }

    $sql = mysql_query ("SELECT * FROM announce WHERE date BETWEEN '$start' AND '$to' ");[/CODE]


    ....it displays all entries ? All I am simply trying to do is if the visitor does not choose and start and end date it displays all entries. In the above example dates were chosen, so it should have limited results to those just in the date ranges. Any ideas??


    Also on the form, for visual purposes, the date format is DD-MM-YYYY (UK date format), but the DATE field I believe has to be stored as YYYY-MM-DD. If I search in format DD-MM-YYYY it displays no entries - if I search in format YYYY-MM-DD it displays the results.

    How can I have the form show format DD-MM-YYYY but searches correctly on the database (the column in the database is a DATE field).


    Thanks,

    CC
    Copy linkTweet thisAlerts:
    @cc976aauthorJan 09.2008 — o.k worked out it doesn't like 1900-01-01 and 2050-01-01 - so changed to 2000-01-01 and 2010-01-01 - now this part works.

    Although if I use the below it works fine:

    [CODE]if (!$_POST['start']) {
    $start = '2000-01-01' ;
    } else {
    $start = $_POST['start'] ;
    }

    if (!$_POST['to']) {
    $to = '2010-01-01' ;
    } else {
    $to = $_POST['to'] ;
    }

    $sql = mysql_query("select * from announce where date BETWEEN '$start' and '$to' ");[/CODE]



    ... but this just displays all records if any of the search fields, or no search fields, are completed:


    [CODE] if (!$_POST['start']) {
    $start = '2000-01-01' ;
    } else {
    $start = $_POST['start'] ;
    }

    if (!$_POST['to']) {
    $to = '2010-01-01' ;
    } else {
    $to = $_POST['to'] ;
    }


    if ((!$_POST['nm3']) && (!$_POST['County'])) {
    $sql = mysql_query("select * from announce where date BETWEEN '$start' and '$to' ");

    } else if (!$_POST['nm3']) {

    $sql = mysql_query("select * from announce where county='$County' and date BETWEEN '$start' AND '$to' ");

    } else if (!$_POST['County']) {

    $sql = mysql_query("select * from announce where name='$nm3' and date BETWEEN '$start' AND '$to' ");

    }[/CODE]



    ? ? ?

    Plus still have the following issue:

    On the form, for visual purposes, the date format is DD-MM-YYYY (UK date format), but the DATE field I believe has to be stored as YYYY-MM-DD. If I search in format DD-MM-YYYY it displays no entries - if I search in format YYYY-MM-DD it displays the results.

    How can I have the form show format DD-MM-YYYY but searches correctly on the database (the column in the database is a DATE field).[/QUOTE]
    Copy linkTweet thisAlerts:
    @bathurst_guyJan 10.2008 — On the form, for visual purposes, the date format is DD-MM-YYYY (UK date format), but the DATE field I believe has to be stored as YYYY-MM-DD. If I search in format DD-MM-YYYY it displays no entries - if I search in format YYYY-MM-DD it displays the results.

    How can I have the form show format DD-MM-YYYY but searches correctly on the database (the column in the database is a DATE field).[/QUOTE]
    Rather than having them in the one text input, why not have three seperate input fields then use php to butt them together.

    For example:&lt;label&gt;Day: &lt;input type="text" name="day"&gt;&lt;/label&gt;
    &lt;label&gt;Month: &lt;input type="text" name="month"&gt;&lt;/label&gt;
    &lt;label&gt;Year: &lt;input type="text" name="year"&gt;&lt;/label&gt;

    $date = $_POST['year'] . "-" . $_POST['month'] . "-" . $_POST['day'];
    Copy linkTweet thisAlerts:
    @cc976aauthorJan 10.2008 — Rather than having them in the one text input, why not have three seperate input fields then use php to butt them together.

    For example:&lt;label&gt;Day: &lt;input type="text" name="day"&gt;&lt;/label&gt;
    &lt;label&gt;Month: &lt;input type="text" name="month"&gt;&lt;/label&gt;
    &lt;label&gt;Year: &lt;input type="text" name="year"&gt;&lt;/label&gt;

    $date = $_POST['year'] . "-" . $_POST['month'] . "-" . $_POST['day'];
    [/QUOTE]



    This would make sense but the search form has a javascript pop up calender, so results are fed in to one field.

    Is there a way to show DD-MM-YYYY - convert it to YYYY-MM-DD before carrying out the search?

    Thanks,

    Dean
    Copy linkTweet thisAlerts:
    @MrCoderJan 10.2008 — Rather than having them in the one text input, why not have three seperate input fields then use php to butt them together.

    For example:&lt;label&gt;Day: &lt;input type="text" name="day"&gt;&lt;/label&gt;
    &lt;label&gt;Month: &lt;input type="text" name="month"&gt;&lt;/label&gt;
    &lt;label&gt;Year: &lt;input type="text" name="year"&gt;&lt;/label&gt;

    $date = $_POST['year'] . "-" . $_POST['month'] . "-" . $_POST['day'];
    [/QUOTE]


    [code=php]
    $date = (int)$_POST['year'] . "-" . (int)$_POST['month'] . "-" . (int)$_POST['day'];
    [/code]


    Typecast to avoid sql injection.
    Copy linkTweet thisAlerts:
    @MrCoderJan 10.2008 — This would make sense but the search form has a javascript pop up calender, so results are fed in to one field.

    Is there a way to show DD-MM-YYYY - convert it to YYYY-MM-DD before carrying out the search?

    Thanks,

    Dean[/QUOTE]


    [code=php]
    $date = "31-12-2008";

    $new_date = implode("-", array_reverse(explode("-", $date)));
    [/code]
    ×

    Success!

    Help @cc976a spread the word by sharing this article on Twitter...

    Tweet This
    Sign in
    Forgot password?
    Sign in with TwitchSign in with GithubCreate Account
    about: ({
    version: 0.1.9 BETA 6.14,
    whats_new: community page,
    up_next: more Davinci•003 tasks,
    coming_soon: events calendar,
    social: @webDeveloperHQ
    });

    legal: ({
    terms: of use,
    privacy: policy
    });
    changelog: (
    version: 0.1.9,
    notes: added community page

    version: 0.1.8,
    notes: added Davinci•003

    version: 0.1.7,
    notes: upvote answers to bounties

    version: 0.1.6,
    notes: article editor refresh
    )...
    recent_tips: (
    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @meenaratha,
    tipped: article
    amount: 1000 SATS,

    tipper: @Yussuf4331,
    tipped: article
    amount: 1000 SATS,
    )...