Results 1 to 3 of 3

Thread: login form - how to match form data with the data in database.

  1. #1
    Join Date
    Jun 2014

    login form - how to match form data with the data in database.

    Im trying to make a log in form in PHP and Im stuck in the part where it matches the value filled by the user aginst the data stored in the database(MySql). Im not sure where Im making error as Im new to PHP so please help .


    @mysql_select_db($database) or die( "Unable to select database");
    $query="SELECT * FROM agents";
    $result=mysql_query($query) or die(mysql_error());
    //print $num;

    if($num > 0)
    $sql = "SELECT agent FROM agents WHERE agent='$_POST[agent1]'
    AND street='$_POST[street1]'";
    $result2 = mysql_query($sql) or die("Query died: fpassword");
    $num2 = mysqli_num_rows($result2);
    //print $num2;
    if($num2 > 0) //password matches
    echo "match it is !!";
    Explanation: I want my form to match the data entered by the user in "agent1 and street1" field against the data already stored in the database and if it matches then show me the next page.


  2. #2
    Join Date
    Jul 2013
    Voorheesville NY USA
    1 - post your code properly next time. Read the forum rules first.

    2 - Do NOT use the @operator. Where did you learn this? Especially on a query call which you really MUST check for success before proceeding. Why would one ever want to suppress an error report of a failed operation? Makes no sense. That's like saying "don't tell me if I'm wrong - I'm going to do it anyway."

    3 - When you read up on these functions in the PHP manual (link at top of forum site) did you not see the highlighted box telling you NOT to use the MYSQL_* functions? Use mysqlI or PDO for your db access.

    Now looking at your code:
    You do a query of an entire table 'agents'. Great. But why? You then check if you have any results at all and then you proceed to do a query against the same table again, this time only looking for a specific agent value. Great - that's more like it except one should never use unsanitized data as in input argument in a query. You need to use prepared queries (not avail btw in MySQL_* extension) to be safe. You then check the number of rows returned using an entirely different extension's function (mysqli). Why switch horses in mid-script? And lastly - if you did not find a row that matches your input values you blame it on a bad password. Why do you say that since a password has not been mentioned in your script at all?

    PS - your syntax is wrong on your post references. A PROPER (IMHO) reference to an array (which $_POST is) looks like: $_POST['index']. The index of an associative array should be in quotes unless it is a variable or a constant name.
    PS - If you're posting here you should be using:

    ini_set('display_errors', '1');

    at the top of ALL php code while you develop it!

  3. #3
    Join Date
    May 2014
    Hey there,

    As mentioned above by ginerjm there are some poor techniques used, but that's not the issue at hand.

    PHP Code:

    mysql_select_db($database) or die( "Unable to select database"); 
    $query="SELECT * FROM agents";
    $result=mysql_query($query) or die(mysql_error());
    //print $num;

    if($num 0)
    $sql "SELECT agent FROM agents WHERE agent='$_POST[agent1]'
    AND street='
    $result2 mysql_query($sql) or die("Query died: fpassword");
    $num2 mysqli_num_rows($result2);
    //print $num2;
    $num2 0//password matches
    "match it is !!";
    To get to the actual issue, you're query needs to be able to find the right row. First, you're using both MySQL and MySQLi. Ditch MySQL since it's deprecated and MySQLi is better.

    PHP Code:
    // This adds slashes to apostrophes so your query will work properly. 
    $agent mysqli_real_escape_string($con$_POST['agent1']); 
    $street mysqli_real_escape_string($con$_POST['street1']);

    // If there is an error in the query, this will kill your script and tell you what the error is so you can fix it immediately.
    $sql mysqli_query($con"SELECT agent FROM agents WHERE agent='$agent' AND street='$street'") or die(mysqli_error($con)); 
    Hope this helps.

    Last edited by jedaisoul; 08-01-2014 at 04:50 PM. Reason: advertising link removed

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Tags for this Thread

Posting Permissions

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



X vBulletin 4.2.2 Debug Information

  • Page Generation 0.09826 seconds
  • Memory Usage 2,878KB
  • Queries Executed 15 (?)
More Information
Template Usage (33):
  • (1)ad_footer_end
  • (1)ad_footer_start
  • (1)ad_global_above_footer
  • (1)ad_global_below_navbar
  • (1)ad_global_header1
  • (1)ad_global_header2
  • (1)ad_navbar_below
  • (1)ad_showthread_firstpost_sig
  • (1)ad_showthread_firstpost_start
  • (1)ad_thread_first_post_content
  • (1)ad_thread_last_post_content
  • (2)bbcode_php
  • (1)footer
  • (1)forumjump
  • (1)forumrules
  • (1)gobutton
  • (1)header
  • (1)headinclude
  • (1)headinclude_bottom
  • (3)memberaction_dropdown
  • (1)navbar
  • (4)navbar_link
  • (1)navbar_moderation
  • (1)navbar_noticebit
  • (1)navbar_tabs
  • (2)option
  • (3)postbit
  • (3)postbit_onlinestatus
  • (3)postbit_wrapper
  • (1)spacer_close
  • (1)spacer_open
  • (1)tagbit_wrapper 

Phrase Groups Available (6):
  • global
  • inlinemod
  • postbit
  • posting
  • reputationlevel
  • showthread
Included Files (26):
  • ./showthread.php
  • ./global.php
  • ./includes/class_bootstrap.php
  • ./includes/init.php
  • ./includes/class_core.php
  • ./includes/config.php
  • ./includes/functions.php
  • ./includes/functions_navigation.php
  • ./includes/class_friendly_url.php
  • ./includes/class_hook.php
  • ./includes/class_bootstrap_framework.php
  • ./vb/vb.php
  • ./vb/phrase.php
  • ./includes/functions_facebook.php
  • ./includes/functions_calendar.php
  • ./includes/functions_bigthree.php
  • ./includes/class_postbit.php
  • ./includes/class_bbcode.php
  • ./includes/functions_reputation.php
  • ./includes/functions_notice.php
  • ./packages/vbattach/attach.php
  • ./vb/types.php
  • ./vb/cache.php
  • ./vb/cache/db.php
  • ./vb/cache/observer/db.php
  • ./vb/cache/observer.php 

Hooks Called (73):
  • init_startup
  • friendlyurl_resolve_class
  • init_startup_session_setup_start
  • database_pre_fetch_array
  • database_post_fetch_array
  • init_startup_session_setup_complete
  • global_bootstrap_init_start
  • global_bootstrap_init_complete
  • cache_permissions
  • fetch_postinfo_query
  • fetch_postinfo
  • fetch_threadinfo_query
  • fetch_threadinfo
  • fetch_foruminfo
  • load_show_variables
  • load_forum_show_variables
  • global_state_check
  • global_bootstrap_complete
  • global_start
  • style_fetch
  • global_setup_complete
  • showthread_start
  • showthread_getinfo
  • strip_bbcode
  • friendlyurl_clean_fragment
  • friendlyurl_geturl
  • forumjump
  • cache_templates
  • cache_templates_process
  • template_register_var
  • template_render_output
  • fetch_template_start
  • fetch_template_complete
  • parse_templates
  • fetch_musername
  • notices_check_start
  • notices_noticebit
  • process_templates_complete
  • friendlyurl_redirect_canonical
  • showthread_post_start
  • showthread_query_postids
  • showthread_query
  • bbcode_fetch_tags
  • bbcode_create
  • showthread_postbit_create
  • postbit_factory
  • postbit_display_start
  • postbit_imicons
  • bbcode_parse_start
  • bbcode_parse_complete_precache
  • bbcode_parse_complete
  • postbit_display_complete
  • memberaction_dropdown
  • tag_fetchbit
  • tag_fetchbit_complete
  • forumrules
  • navbits
  • navbits_complete
  • build_navigation_data
  • build_navigation_array
  • check_navigation_permission
  • process_navigation_links_start
  • process_navigation_links_complete
  • set_navigation_menu_element
  • build_navigation_menudata
  • build_navigation_listdata
  • build_navigation_list
  • set_navigation_tab_main
  • set_navigation_tab_fallback
  • navigation_tab_complete
  • fb_like_button
  • showthread_complete
  • page_templates