Need help moving form info into a MySQL DB...

Discussion in 'OT Technology' started by brds, Mar 11, 2007.

  1. brds

    brds OT Supporter

    Joined:
    Jun 26, 2006
    Messages:
    17,652
    Likes Received:
    15
    Location:
    Atlanta
    Okay, so i'm trying to move text strings that were entered in a form to a db i have set up on my website. I can successfully connect to the db and everything. I have NO idea how to submit the form items to the DB. I've googled for it for hours and can't find anything.

    Here's the .php i've created so far (simply connects to the db):

    Code:
    <?php
    $username = "USERNAME";
    $password = "PASSWORD";
    $hostname = "MYSQLHOST";
    $dbh = mysql_connect($hostname, $username, $password)
        or die("Unable to connect to the MySQL Host.");
    print "Successfully connected to the MySQL Host.<br>";
    $selected = mysql_select_db("d60350713",$dbh)
        or die("Unable to connect to MYSQL database (d60350713).");
    print "Successfully connected to the MySQL database (d60350713)";
    
    mysql_close($dbh);
    ?>
    
    Is there a way i can specify which cell in the table for the form item to go into... i.e. - PLACE formitem1 into MYSQL DB Column1 Row 2...

    OR

    Hopefully there's a way to make it do that automatically.

    If anybody could please help a MySQL newb out... it'd be greatly appreciated.
     
  2. Tk

    Tk Well-Known Member

    Joined:
    Dec 23, 2001
    Messages:
    23,086
    Likes Received:
    285
    Location:
    nw iowa
    ummm.... its an SQL statement, i'll give you that much.....
     
  3. Tk

    Tk Well-Known Member

    Joined:
    Dec 23, 2001
    Messages:
    23,086
    Likes Received:
    285
    Location:
    nw iowa
    maybe an 'INSERT' statement?


    lolz
     
  4. brds

    brds OT Supporter

    Joined:
    Jun 26, 2006
    Messages:
    17,652
    Likes Received:
    15
    Location:
    Atlanta
    Hmmm... i should know how to use those since i had to do a mysql project in one of my classes about 2 wks ago... but it seems to have escaped me at the moment....
     
  5. Tk

    Tk Well-Known Member

    Joined:
    Dec 23, 2001
    Messages:
    23,086
    Likes Received:
    285
    Location:
    nw iowa
    you have an email address? ill send you a couple treats.

    edit: actually, never mind. this will do.

    http://www.w3schools.com/php/php_mysql_insert.asp
     
  6. brds

    brds OT Supporter

    Joined:
    Jun 26, 2006
    Messages:
    17,652
    Likes Received:
    15
    Location:
    Atlanta
  7. brds

    brds OT Supporter

    Joined:
    Jun 26, 2006
    Messages:
    17,652
    Likes Received:
    15
    Location:
    Atlanta
    Hmm... that website doesn't show me how to relate the values entered in the form to the php script. Is there a way to say something along the lines of:

    $MYSQL_Name = $_GET(from the form)[$Name Given By User];

    *EDIT: I lied... it does *
     
  8. Tk

    Tk Well-Known Member

    Joined:
    Dec 23, 2001
    Messages:
    23,086
    Likes Received:
    285
    Location:
    nw iowa
    yeah, i looked through it to make sure it had examples of what you needed. :)

    that w3schools.com page is pretty nice.....

    also bookmark the php online manual, if you didnt do that from the other thread i posted in.

    http://www.php.net/manual/en/

    and heres the mysql manual:
    http://dev.mysql.com/doc/refman/5.1/en/index.html
     
  9. brds

    brds OT Supporter

    Joined:
    Jun 26, 2006
    Messages:
    17,652
    Likes Received:
    15
    Location:
    Atlanta
    Hmm... i did the mysql lines like it says in the w3schools site... I loaded the php onto my website and ran the script. It says that the data was entered into the database... However, when i load up the database, all fields were entered with empty-ness.

    Anybody see anything wrong with:
     
  10. brds

    brds OT Supporter

    Joined:
    Jun 26, 2006
    Messages:
    17,652
    Likes Received:
    15
    Location:
    Atlanta
    Bump b/c my form entries still aren't being put into the DB. I can view the db on my site and see that the fields are empty. I can edit these empty fields by hand and then view them in my output php script though...
     
  11. gnp

    gnp New Member

    Joined:
    Mar 11, 2007
    Messages:
    280
    Likes Received:
    0
    what does the markup for your form look like, and have you tried echo'ing the incoming form variables back to the page so you know they are actually getting there?
     
  12. brds

    brds OT Supporter

    Joined:
    Jun 26, 2006
    Messages:
    17,652
    Likes Received:
    15
    Location:
    Atlanta
    EIS.html file (form):

    <form name="EIS" method="post" action="post.php">
    <table width="798px" border="0">
    <tr>
    <td>Employee Name:</td>
    <td><input type="text" name="Name" /></td>
    </tr>
    <tr>
    <td>Phone Number:</td>
    <td><input type="text" name="Phone" /></td>
    </tr>
    <tr>
    <td>Employee E-mail:</td>
    <td><input type="text" name="Email" /></td>
    </tr>
    <tr>
    <td>Employee's Building:</td>
    <td>
    <select name="Building" id="Building">
    <option value="Academic Diving/Marine Lab" selected="selected">Academic Diving/Marine Lab</option>
    *** LIST ALL OTHER OPTIONS ***
    </select>
    </td>
    <tr>
    <td>Office Number:</td>
    <td><input type="text" name="Office" /></td>
    </tr>
    <tr>
    <td>Employee's Operating System:</td>
    <td>
    <select name="OS" id="OS">
    <option value="Windows" selected="selected">Windows</option>
    <option value="Mac OS">Mac OS</option>
    <option value="Linux">Linux</option>
    <option value="Unix">Unix</option>
    <option value="Other">Other</option>
    </select>
    </td>
    </tr>
    <tr>
    <td>Employee's IP Address:</td>
    <td><input type="text" name="IP" /></td>
    </tr>
    <tr>
    <td>Employee's MAC Address:</td>
    <td><input type="text" name="MAC" /></td>
    </tr>
    </table>
    <br /><br />
    <input type="submit" name="submit" value="Submit Information" />
    </form>



    Here's my php file that reads in the form and is SUPPOSED to submit it to the MySQL database:

    post.php:

    <?php
    $username = "*USER NAME*";
    $password = "*PASSWORD*";
    $hostname = "MYSQLHOST";
    $dbh = mysql_connect($hostname, $username, $password)
    or die("Unable to connect to the MySQL Host.");
    print "Successfully connected to the MySQL Host.<br>";
    $selected = mysql_select_db("d60350713",$dbh)
    or die("Unable to connect to MYSQL database (d60350713).");
    print "Successfully connected to the MySQL database (d60350713)";


    $sql="INSERT INTO EIS (Name, Phone, Email, Building, Office, OS, IP, macid)
    VALUES
    ('$_POST[Name]','$_POST[Phone]','$_POST','$_POST[Building]','$_POST[Office]','$_POST[OS]','$_POST[IP]','$_POST[macid]')";

    if (!mysql_query($sql,$dbh))
    {
    die("Error: " . mysql_error());
    }
    echo "One record has been added";


    mysql_close($dbh);
    ?>



    And Here's my last file... the one that shows all the entries in a html table:

    get.php:

    <?php
    $username = "*UserName*";
    $password = "*Password*";
    $hostname = "MYSQLHOST";
    $dbh = mysql_connect($hostname, $username, $password)
    or die("Unable to connect to the MySQL Host.");
    print "Successfully connected to the MySQL Host.<br>";
    $selected = mysql_select_db("d60350713",$dbh)
    or die("Unable to connect to MYSQL database (d60350713).");
    print "Successfully connected to the MySQL database (d60350713)";
    echo "<br />";
    $result = mysql_query("SELECT * FROM EIS");
    echo "<br />EMPLOYEE INFORMATION:";
    echo "<table border='1'>
    <tr bgcolor=#990000>
    <th><font color=#FFFFFF>Name</th>
    <th><font color=#FFFFFF>Phone Number</font></th>
    <th><font color=#FFFFFF>Email Address</font></th>
    <th><font color=#FFFFFF>Work Building</font></th>
    <th><font color=#FFFFFF>Work Office</font></th>
    <th><font color=#FFFFFF>Operating System</font></th>
    <th><font color=#FFFFFF>IP Address</font></th>
    <th><font color=#FFFFFF>MAC Address</font></th>
    </tr>";
    while ($row = mysql_fetch_array($result))
    {
    echo "<tr>";
    echo "<td>" . $row['Name'] . "</td>";
    echo "<td>" . $row['Phone'] . "</td>";
    echo "<td>" . $row['Email'] . "</td>";
    echo "<td>" . $row['Building'] . "</td>";
    echo "<td>" . $row['Office'] . "</td>";
    echo "<td>" . $row['OS'] . "</td>";
    echo "<td>" . $row['IP'] . "</td>";
    echo "<td>" . $row['macid'] . "</td>";
    echo "</tr>";
    }
    echo "</table>";
    mysql_close($dbh);

    ?>
     
  13. Slid.

    Slid. I'm a guy.

    Joined:
    Oct 25, 2001
    Messages:
    1,928
    Likes Received:
    0
    Location:
    NH
    Before the if (!mysql_query($sql,$dbh)) line add:

    echo $sql . "<br><br>";

    Also add after the echo:

    print_r($_POST);
    print_r($_REQUEST);

    Personally I prefer to use $_REQUEST over $_POST as it retrieves variables from both $_POST and $_GET. print_r will print out everything under the $_POST and $_REQUEST arrays. Print the $sql variable should show you if anything is wrong with the sql.
     

Share This Page