Results 1 to 7 of 7

Thread: Form-MySQL-Display table on site

  1. #1
    Join Date
    Feb 2010
    Location
    Falkirk, Scotland
    Posts
    142
    Thanks
    21
    Thanked 4 Times in 4 Posts

    Angry Form-MySQL-Display table on site

    i want to use the following form to post data into a MySQL database and then project database entries into a table on my site! but need some guidance.

    Code:
    <form name="add" action"addfriends.php" method="post" >
            <tr class="b">
              <td width="11%">&nbsp;Name:</td>
              <td width="38%"><input type="text" name="name" id="name" /></td>
              <td width="20%">&nbsp;Profile URL: </td>
              <td width="31%"><input name="profile" type="text" id="profile" size="15" />
                &nbsp;</td>
            </tr>
            <tr class="b">
              <td>&nbsp;Gender:</td>
              <td width="31%"><input name="profile" type="text" id="profile" size="15" />
                <option value="1" selected="selected">Male</option>
                <option value="2">Female</option>
              </select></td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
            </tr>
            <tr class="b">
              <td>&nbsp;Location:</td>
              <td colspan="3"><label>
                <select name="location" size="1">
                  <option selected="selected" value="">Select Country</option>
                  //removed options for shorter post
                </select>
              </label></td>
            </tr>
            <tr class="b">
              <td>&nbsp;</td>
              <td align="right"><input name="button3" type="submit" id="button" value="Request Friends" /></td>
              <td>&nbsp;</td>
              <td>&nbsp;</td>
            </tr>
          </form>
    my database is already created with the following fields: Name, Gender, Location and Profile url.
    however, i dont know how to create an action to post data from form to database and show database on site!
    can anyone help plz?!
    Last edited by liamallan; 02-16-2010 at 07:53 PM.

  2. #2
    Join Date
    Nov 2006
    Location
    Northeast USA
    Posts
    408
    Thanks
    8
    Thanked 30 Times in 28 Posts

    Default

    Well assuming if your using MySql, you could:
    PHP Code:
    <?php
    $host
    ="localhost";
    $username="username";
    $password="password";
    $db_name="test";
    $tbl_name="test_mysql";

    mysql_connect("$host""$username""$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");
    foreach(
    $_POST as $var => $val){
    $var=$val;
    }
    $sql="INSERT INTO $tbl_name(Name, Gender, Location, Profile url)VALUES('$name', '$gender', '$location', '$profile')";
    $result=mysql_query($sql);
    if(
    $result){
    echo 
    "Successful";
    echo 
    "<BR>";
    echo 
    "<a href='index.php'>Back to main page</a>";
    }
    else {
    echo 
    "ERROR";
    }
    mysql_close();
    ?>
    and you have a typo in your HTML:
    Code:
              <td>&nbsp;Gender:</td>
              <td width="31%"><select name="gender" id="gender" />
                <option value="1" selected="selected">Male</option>
                <option value="2">Female</option>
              </select></td>
    ******BOTH UNTESTED. THERE MAY BE ERRORS********
    -Ben -- THE DYNAMIC DRIVERS
    My Links: My DD Profile||My Youtube Video Tutorials||DD Helping Coders||DD Coders In Training
    I told my client to press F5, the client pressed F, then 5, *facepalm*

  3. The Following User Says Thank You to fileserverdirect For This Useful Post:

    liamallan (02-13-2010)

  4. #3
    Join Date
    Feb 2010
    Location
    Falkirk, Scotland
    Posts
    142
    Thanks
    21
    Thanked 4 Times in 4 Posts

    Default

    thanks for your reply, when i run 'addprocess.php' (thats what i called action), i get the following message:

    Notice: Undefined variable: Name in /www/freewebhostx.com/l/i/a/liamallan/htdocs/addprocess.php on line 13

    Notice: Undefined variable: Gender in /www/freewebhostx.com/l/i/a/liamallan/htdocs/addprocess.php on line 13

    Notice: Undefined variable: Location in /www/freewebhostx.com/l/i/a/liamallan/htdocs/addprocess.php on line 13

    Notice: Undefined variable: Profile in /www/freewebhostx.com/l/i/a/liamallan/htdocs/addprocess.php on line 13
    ERROR
    any ideas?

  5. #4
    Join Date
    Sep 2008
    Location
    Bristol - UK
    Posts
    842
    Thanks
    32
    Thanked 132 Times in 131 Posts

    Default

    You need to set up the variables before being able to insert them into the database, like this:

    PHP Code:
    <?php
    $host
    ="localhost";
    $username="username";
    $password="password";
    $db_name="test";
    $tbl_name="test_mysql";

    $name mysql_real_escape_string($_POST['name']);
    $gender mysql_real_escape_string($_POST['gender']);
    $location mysql_real_escape_string($_POST['location']);
    $profile mysql_real_escape_string($_POST['profile']);

    mysql_connect("$host""$username""$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");

    $sql="INSERT INTO $tbl_name(Name, Gender, Location, Profile url)VALUES('$name', '$gender', '$location', '$profile')";
    $result=mysql_query($sql);
    if(
    $result){
    echo 
    "Successful";
    echo 
    "<BR>";
    echo 
    "<a href='index.php'>Back to main page</a>";
    }
    else {
    echo 
    "ERROR";
    }
    mysql_close();
    ?>
    This form has no validation, so you may want to consider adding that in, by using:
    PHP Code:
    if(isset($_POST['FIELD_NAME']) && !empty($_POST['FIELD_NAME'])) {
     
    $FIELD_NAME $_POST['FIELD_NAME'];
    } else{
     echo 
    "Please fill in all the required fields."

    To check whether or not the user has entered the information they need to.

  6. #5
    Join Date
    Feb 2010
    Location
    Falkirk, Scotland
    Posts
    142
    Thanks
    21
    Thanked 4 Times in 4 Posts

    Default

    thanx for replying, i used the above action and tells me:
    Notice: Undefined index: name in /www/freewebhostx.com/l/i/a/liamallan/htdocs/addprocess.php on line 8

    Warning: mysql_real_escape_string() [function.mysql-real-escape-string]: Access denied for user 'vhostswww'@'localhost' (using password: NO) in /www/freewebhostx.com/l/i/a/liamallan/htdocs/addprocess.php on line 8
    says that for lines 8, 9, 10 and 11.
    im new to php, so im pretty confused lol

  7. #6
    Join Date
    Jan 2010
    Posts
    16
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default

    What hosting company are you using? There are some hosting companies that do not use localhost for the host, and that could be where you are going wrong. And can you post your php code as well so we can make sure that it looks right.

  8. #7
    Join Date
    Feb 2010
    Location
    Falkirk, Scotland
    Posts
    142
    Thanks
    21
    Thanked 4 Times in 4 Posts

    Default

    i am with freewebhostx.com
    Code:
    <?php
    $host="localhost";
    $username="username";
    $password="password";
    $db_name="liamallan_addfriends";
    $tbl_name="addfriends";
    
    $name = mysql_real_escape_string($_POST['name']);
    $gender = mysql_real_escape_string($_POST['gender']);
    $location = mysql_real_escape_string($_POST['location']);
    $profile = mysql_real_escape_string($_POST['profile']);
    
    mysql_connect("$host", "$username", "$password")or die("cannot connect");
    mysql_select_db("$db_name")or die("cannot select DB");
    
    $sql="INSERT INTO $tbl_name(Name, Gender, Location, Profile url)VALUES('$name', '$gender', '$location', '$profile')";
    $result=mysql_query($sql);
    if($result){
    echo "Successful";
    echo "<BR>";
    echo "<a href='index.php'>Back to main page</a>";
    }
    else {
    echo "ERROR";
    }
    mysql_close();
    ?>

Bookmarks

Posting Permissions

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