Problems Inputting Date into My database.

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • jerry101
    New Member
    • Jul 2008
    • 28

    Problems Inputting Date into My database.

    I am currently using a version of phpmyadmin via heartinternet.

    I can connect to my database fine, but I cannot seem to input any database into the table I have created.

    the database is called "web-32jerry" and the table is called "login" for reference sake.

    The code i have at the moment has 2 text boxes which they input their username and password in and then a submit button attached.

    I have taken bits from 2 different tutorials to help me so they are probably bits of code in there that i dont even need so it will look messy!!


    the code i have is the following;

    [code=php]
    <form method="post">
    <p>Username -
    <input name="user" type="text" id="user" size="15">
    <br>
    Password -
    <input name="pass" type="text" id="pass" size="15">
    <br> <input name="add" type="submit" id="add" value="add">
    </form>

    <?php

    if(isset($_POST['add']))
    {

    mysql_connect(" localhost", "web32-jerry", "my password")
    OR die ("Can't Access any database.");



    mysql_selectdb( "web32-jerry")
    OR die ("Cant access the 'jerry' database.");

    $user= $_POST['user'];
    $pass = $_POST['pass'];

    $query = "INSERT INTO login (host, user, pass, select_priv, insert_priv, update_ priv) VALUES ('localhost', '$user', PASSWORD('$pass '), 'Y', 'Y', 'Y')";


    mysql_query($qu ery) or die('Error , insert query failed');

    $query = "FLUSH PRIVILEGES";
    mysql_query($qu ery) or die('Error, insert query failed');
    }

    ?>
    [/code]

    any help given i will be very greatful for...

    thank you very much,

    Jerry
    Last edited by Atli; Jul 23 '08, 12:44 AM. Reason: Added [code] tags
  • amitpatel66
    Recognized Expert Top Contributor
    • Mar 2007
    • 2358

    #2
    So here you are facing a problem in conneting to the database or during inserting a record in to database table?

    Comment

    • jerry101
      New Member
      • Jul 2008
      • 28

      #3
      Crisis averted!

      Sorry, I actually figured it out earlier!!

      Cheers for the quick reply anyway dude!

      Thanks!

      Comment

      • amitpatel66
        Recognized Expert Top Contributor
        • Mar 2007
        • 2358

        #4
        Originally posted by jerry101
        Crisis averted!

        Sorry, I actually figured it out earlier!!

        Cheers for the quick reply anyway dude!

        Thanks!
        Thats good to hear that your issue is resolved. It would be helpful if you can post your solution here so that in can be helpful for other members of the forum looking for a solution of a similar issue.

        Comment

        • jerry101
          New Member
          • Jul 2008
          • 28

          #5
          I had a number of pages

          The form I used on my home page was the following;
          [code=php]
          <form name="form1" method="post" action="checklo gin.php">

          <div align="center"> Username :
          <input name="myusernam e" type="text" id="myusername" >

          Password :
          <input name="mypasswor d" type="password" id="mypassword" >
          <p>
          <input type="submit" name="Submit" value="Login">

          I then had a page with checked their login in details

          <?php
          $host="localhos t"; // Host name
          $username=""; // Mysql username
          $password=""; // Mysql password
          $db_name=""; // Database name
          $tbl_name=""; // Table name

          // Connect to server and select databse.
          mysql_connect(" $host", "$username" , "$password" )or die("cannot connect");
          mysql_select_db ("$db_name") or die("cannot select DB");

          // username and password sent from form
          $myusername=$_P OST['myusername'];
          $mypassword=$_P OST['mypassword'];

          // To protect MySQL injection (more detail about MySQL injection)
          $myusername = stripslashes($m yusername);
          $mypassword = stripslashes($m ypassword);
          $myusername = mysql_real_esca pe_string($myus ername);
          $mypassword = mysql_real_esca pe_string($mypa ssword);

          $sql="SELECT * FROM $tbl_name WHERE username='$myus ername' and password='$mypa ssword'";
          $result=mysql_q uery($sql);

          // Mysql_num_row is counting table row
          $count=mysql_nu m_rows($result) ;
          // If result matched $myusername and $mypassword, table row must be 1 row

          if($count==1){
          // Register $myusername, $mypassword and redirect to file "login_success. php"
          session_registe r("myusername") ;
          session_registe r("mypassword") ;
          header("locatio n:login_success .php");
          }
          else {
          echo "Wrong Username or Password";
          }
          ?>
          [/code]

          And that directed me to a page called login_success.p hp which I created as a members area...

          Im now having a new problem though...

          At the bottom my direction is the following;

          session_registe r("myusername") ;
          session_registe r("mypassword") ;
          header("locatio n:login_success .php");


          If I have 2 accounts created for example

          John and Jerry

          When those 2 accounts both log-in they both go to the same page (login_success. php)

          Do you know of a way I can direct each different user to a different page...

          I can't find anything about it on the Internet, surprising with the amount of phpmysql log-in form tutorials that come up....

          I was thinking of an If statement, but I can't seem to get what I've done to work! Just gives me unknown $ errors etc..

          Any ideas?
          Last edited by Atli; Jul 23 '08, 12:45 AM. Reason: Added [code] tags

          Comment

          • Atli
            Recognized Expert Expert
            • Nov 2006
            • 5062

            #6
            Hi.

            Why do you want to direct each of your users to separate pages?
            Would it not be better to simply have that one page display different content based on which user is logged in?

            Anyhow, a simple if statement should suffice:
            [code=php]
            if($userName = "Jerry") $url = "jerry.php" ;
            else if($userName = "John") $url = "john.php";
            header("Locatio n: ". $url);
            [/code]

            One thing you should look out for tho.
            The session_registe r function is very old and should not be used anymore, unless you are using PHP3, in which case you really really should upgrade!

            It is better to simply assign or unset elements in the $_SESSION array, like:
            [code=php]
            // Instead of
            session_registe r("variable") ;
            session_unregis ter("variable") ;
            // Do
            $_SESSION['variable'] = $variable;
            unset($_SESSION['variable']);
            [/code]

            And please use [code] tags when posting code examples.

            Comment

            • jerry101
              New Member
              • Jul 2008
              • 28

              #7
              Okay that's fine.

              Im not using PHP3, that's just something I saw so I used it. I will get onto changing that now! Thanks for that!

              Basically, I want each person to be directed to their own User page. It's like for example how on play.com you get directed to your own page where you can view your previous orders and personal details and no one else can!

              I need to have our clients see their own individual data but not any of the other clients we have!

              If that makes sense!

              Thanks for the help though!

              Comment

              • jerry101
                New Member
                • Jul 2008
                • 28

                #8
                I just read through your post again and yes your idea does seem more sensible!

                I just need clients to view their own information and not other peoples and that method of using the same page but displaying different data sounds like it would be more organised!

                How would I go about that then?

                I'm currently reading through my book (i.e. my bible at the moment) to see if I can figure it out hehe

                thanks

                Comment

                • jerry101
                  New Member
                  • Jul 2008
                  • 28

                  #9
                  Two ideas I was thinking of on how to do it.

                  Include Files - I could validate the username, and each username brings up a different a include file on the user page.(but then that sounds familiar to my original idea...)

                  or

                  Storing the information in the Database - I could store the information I want to display in the database and then can display it by calling it up using variables etc...


                  Sorry for all the continual posts, I just wanted to show that I am actually thinking/trying to do something about it rather than just mooch off everyone else.

                  Would any of my above ideas work?

                  Comment

                  • Atli
                    Recognized Expert Expert
                    • Nov 2006
                    • 5062

                    #10
                    That second idea is pretty much how it is usually done.

                    Most pages like these are created with a standard "template" of sorts, that is filled in using personalized data from a database (or some other source of info).

                    Like, for example, if you had a page that should display the contact info for the logged in user, you could do something like this:
                    [code=php]
                    echo "<table>";
                    echo "<tr><td>Em ail</td><td>Webpage</td><td>Phone</td></tr>";

                    $sql = "
                    SELECT email, web, phone FROM user
                    WHERE username = '{$_SESSION['UserName']}'";
                    $result = mysql_query($sq l) or die("Query failed: ". mysql_error());

                    while($row = mysql_fetch_ass oc($result)) {
                    echo "<tr>";
                    echo " <td>{$row['email']}</td>";
                    echo " <td>{$row['web']}</td>";
                    echo " <td>{$row['phone']}</td>";
                    echo "</tr>";
                    }
                    echo "</table>";
                    [/code]
                    It's a little simplistic but you get the point.

                    Comment

                    Working...