Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 15 of 15
  1. #1
    New Coder
    Join Date
    Apr 2004
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    php/mySQL form help??

    Hello everyone,

    I have a dream. Well, actually, I have a form...& a mySQL db table for it. The dream would be getting the two to work together as I would like them to...& I'm just wondering if someone might be able to give me a hand?

    The little setup below actually works. It's basically a tutorial I picked up from somewhere else. I've gone through & done some tweaking to it to make it compatible with my site, of course...& have successfully used it. The results are the data being inserted into the db & a copy of the form's submitted data sent to my email. Which is exactly what I want.

    I simply wish to add a couple additional fields to the form...for instance, let's say userage & locale...& achieve the same results with the data inserted & a copy to my email.

    I know that I will either need to add rows to mySql's db table - or create a new one...& I don't necessarily have a problem putting the form together. Of course, my difficulties lie in the php/mySQL instructions (the *feedback* portion of this post).

    I'm just beginning to really *get* PHP...just beginning. I learned & successfully implemented php includes yesterday...WOOOOO HOOOOOOO!! Which was a major ordeal for me. When I finally realized how easy it was (except for the .htaccess part) I about fell over.

    I am very, very, very green with mySQL...although I have run through a couple of tutorials...obviously.

    Anyway - my apologies for the length of this post, but better too much info than not enough, right??

    Here are the steps I have gone through in setting up this *form/shoutbox*...& the process my form goes through:


    CREATE mySQL TABLE:

    Code:
    CREATE TABLE feedback ( 
       name varchar(50), 
       date varchar(40), 
       email varchar(50), 
       comment longtext, 
       id int(4) DEFAULT '0' NOT NULL auto_increment, 
       PRIMARY KEY (id) 
    );

    CONFIG.INC FILE:


    Code:
    <?php 
    $PostPage = "post.php"; //Where people will sign the guestbook 
    $gbPage = "view.php";  //Where people will view the guestbook 
    
    $allowHTML = 0;    //Change this if you want people to post HTML in posts   1 = Yes, 0 = No 
    
    $notify = 0; //When this is set, you will be emailed every time your guestbook is signed   1 = yes, 0 = No 
    $my_email = "youremail@emailhost.com"; //Enter your email address here 
    $subject = "New Guestbook Entry" ; //This is the subject of the notification email 
    
    
    $username = "myname_mydb"; //Database Username 
    $password = "mypassword"; //Database password 
    $db_name =  "username_db"; //Database name 
    
    $limit = 20; //This is the number of entries that will be displayed per page 
    ?>

    POST:

    Code:
    <? 
    // only change this include if you renamed the file 
    include ("config.inc.php"); 
    
    echo $error1; ?> 
    
    
    <FORM ACTION="feedback.php" METHOD=POST> 
                  <P> Name:<BR> 
                    <INPUT TYPE=text NAME=name SIZE=30 maxlength=80> 
                    <BR> 
                    Email&nbsp;(Not Required):<BR> 
                    <INPUT TYPE=text NAME=email SIZE=30 maxlength=80> 
                    <br> 
                    Comments: <BR> 
                    <textarea name="comments" cols="30" rows="5" wrap="VIRTUAL"></textarea> 
                    <BR> 
                  </P> 
      <P><INPUT TYPE=submit NAME=gb VALUE="Add Message"> 
                    &nbsp;&nbsp;Please press only once: this may take a moment. 
                </FORM>


    FEEDBACK:

    Code:
    <?php 
    // only change this include if you renamed the file 
    include ("config.inc.php"); 
    
    // lets make sure they fill out the form before they submit 
    if (($name == "") AND ($email == "") AND ($comments == "")): 
    
    // This is the error message displayed...change this if you want... 
        $error1 = "<font face='Arial' color='Red'><STRONG>Please fill out the form:</STRONG></font><br><br>";     
         
        include($PostPage); 
    
    else: 
    if ($allowHTML == 0): 
    $name = ereg_replace("<","&lt;",$name); 
    $name = ereg_replace(">","&gt;",$name); 
    $email = ereg_replace("<","&lt;",$email); 
    $email = ereg_replace(">","&gt;",$email); 
    $url = ereg_replace("<","&lt;",$url); 
    $url = ereg_replace(">","&gt;",$url); 
    $urltitle = ereg_replace("<","&lt;",$urltitle); 
    $urltitle = ereg_replace(">","&gt;",$urltitle); 
    $referral = ereg_replace("<","&lt;",$referral); 
    $referral = ereg_replace(">","&gt;",$referral); 
    $comments = ereg_replace("<","&lt;",$comments); 
    $comments = ereg_replace(">","&gt;",$comments); 
    
    endif; 
    
    //connect to the database 
    
    // change localhost to your db host name 
    $mysql_link = mysql_pconnect( "localhost", "$username", "$password") // Enter your database info here 
                   or die( "Unable to connect to server"); 
    mysql_select_db( "$db_name") or die( "Unable to select database"); 
    
    $insert = "INSERT INTO feedback(name, date, email, comment) VALUES('$name', SYSDATE(), '$email', '$comments')"; 
    $mysql_insert = mysql_query($insert, $mysql_link) 
        or die("please notify $my_email that the script is not inserting entries<BR><BR>$name,<br>$email,<br>$comments<br><br>$insert"); 
    
    // Email the new post 
    
    if ($notify == 1): 
    
    $comments = ereg_replace("<BR>","\n",$comments); 
    
    mail 
    ( 
    "$my_email", 
    "guestbook entry", 
    "Name : $name 
    Email : $email 
    Note: 
    
    $comments\n 
    ", 
    "From: $email\n" 
    ); 
    endif; 
    
    //check for double entries 
    
    $name_chk = $name; 
    $email_chk = $email; 
    $comments_chk = $comments; 
    
    include($gbPage); 
    
    endif; 
    
    ?>

    VIEW:

    Code:
    <?php 
    // only change this include if you renamed the file 
    include ("config.inc.php"); 
    
    //connect 
    // change localhost to your db host name 
    mysql_pconnect( "localhost", "$username", "$password") 
                   or die( "Unable to connect to SQL server"); 
    mysql_select_db( "$db_name") or die( "Unable to select database"); 
    
        $numresults=mysql_query("SELECT name, date, email, comment FROM feedback"); 
        $numrows=mysql_num_rows($numresults) 
                or die ("query 1 failed"); 
         
    if (empty($offset)) { 
        $offset=0; 
    } 
    
    $query = "SELECT name, date, email, comment FROM feedback order by date DESC limit $offset,$limit"; 
    
    $result = mysql_query ($query) 
        or die ("query 2 failed"); 
             
    while ($row = mysql_fetch_row ($result)) 
    { 
    for ($i = 0; $i < mysql_num_fields ($result); $i++) 
        { 
        if ($i > 0) 
            print ("<br>");     
        if ($i == 0){ 
            print "<b>Name: </b>";     
        } 
        else if ($i == 1){ 
            print "<b>Date: </b>"; 
        } 
        else if ($i == 2){ 
            print "<b>Email: </b>"; 
        } 
        else{ 
            print "<b>Comment:</b><br>"; 
        } 
             
        print ($row[$i]); 
             
        } 
    print "<br><br><center><hr></center>"; 
    print ("<P>"); 
    } 
    if ($offset >= 3) { 
    $prevoffset = $offset - $limit; 
    print "<a href=\"view.php?offset=$prevoffset\">PREV</a>&nbsp;\n"; 
    } 
    $pages=intval($numrows/$limit); 
    if ($pages < ($numrows/$limit)){ 
    $pages=($pages + 1); 
    } 
    for ($i = 1; $i <= $pages; $i++) { 
    $newoffset = $limit*($i-1); 
    if ($newoffset == $offset) { 
    print "$i&nbsp;\n"; 
    } else { 
    print "<a href=\"view.php?offset=$newoffset\">$i</a>&nbsp;\n"; 
    } 
    } 
    
    //show next if not last 
    if (! ( ($offset/$limit) == ($pages - 1) ) && ($pages != 1) ) { 
    $newoffset = $offset+$limit; 
    print "<a href=\"view.php?offset=$newoffset\">NEXT</a><p>\n"; 
    } 
    ?>

    Thanks much for any help, ideas, or suggestions. They are certainly greatly, greatly appreciated.

    Best regards to all,

    Humble Dude

    P.S. - Is there a PHP instruction that will open a particular link within my site in a new browser window? Similar to the "Open New Browser" client-side in Dreamweaver MX?

    Thank you...

  • #2
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    So if you want to add 2 new fields, you'll have to update every file except the config file.

    You first of all have to add a column (one at a time) to the MySQL table. Use the alter command:
    http://goldenink.com/mysql/cheat_sheet.html

    Then in your post page (the form) you may need to add some form fields accordingly. Give them names that reflect the columns. If you're not taking this data from the user (for example, a timestamp, or HTTP_REFERRER or something...), then you don't need to make this field in the form.

    In the feedback page, you need to make additions in a couple of places:
    -If you've made form fields, then change "field_name" to what you called it:
    Code:
    if ($allowHTML == 0): 
     ...
     $field_name = ereg_replace("<","&lt;",$field_name); 
     $field_name = ereg_replace(">","&gt;",$field_name);
    -Now you have to insert into your database, so remember what you called your columns:
    Code:
    $insert = "INSERT INTO feedback(name, date, email, comment, col1, col2, ...) VALUES('$name', SYSDATE(), '$email', '$comments', $field_name, ...)";
    -Then you have to update your email accordingly:
    Code:
    mail 
    ( 
    "$my_email", 
    "guestbook entry", 
    "Name : $name 
    Email : $email 
    Field_name1 : $field_name1
    Field_name2 : $field_name2
    ...
    Note: 
    
    $comments\n 
    ", 
    "From: $email\n" 
    );

    Then you've just got to update your view. You basically have to make sure you're selecting that column(s) in your SELECT ... FROM strings, and then you can follow the steps. (I'm getting lazy here... but I'm assuming you can grasp what's required now..).

    Let us know if you have any troubles.

    Good luck,
    Sadiq.

  • #3
    New Coder
    Join Date
    Apr 2004
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thanks much...

    Thanks for your help. I think I almost have this thing working like I want...

    But I am still a bit stuck. I am trying to insert into my table *talkback* the ip & browser info of the user submitting the form.

    Do I achieve this by:

    Code:
    $insert = "INSERT INTO talkback(name, address, age, locale, comments, date, ip, browser) VALUES('$name', '$address', '$age', '$locale', '$comments', SYSDATE(), $_SERVER['HTTP_REMOTE_ADDR'], $_SERVER['HTTP_USER_AGENT'])";

    OR

    Code:
    $insert = "INSERT INTO talkback(name, address, age, locale, comments, date, ip, browser) VALUES('$name', '$address', '$age', '$locale', '$comments', SYSDATE(), $ip['HTTP_REMOTE_ADDR'], $browser['HTTP_USER_AGENT']);

    or neither of the above??

    Thanks much for any help or suggestions.

    Best regards,
    Humble Dude

  • #4
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Generally I'd need to know what the table looked like (like the column types and all that stuff), but I think I know what might be wrong: you're missing quotes around some values.

    Code:
    $insert = "INSERT INTO talkback(name, address, age, locale, comments, date, ip, browser) VALUES('$name', '$address', '$age', '$locale', '$comments', SYSDATE(), '$_SERVER['HTTP_REMOTE_ADDR']', '$_SERVER['HTTP_USER_AGENT']')";
    Because I'm guessing that IP and Browser are text or varchar or something like that, so it's expecting text/string.

    Another quick test would be to
    PHP Code:
    echo $insert
    and see what that looks like, and actually try to execute that query in MySQL (through phpMyAdmin or the MySQL console..) and see what happens.

    Let me know how that works out for you.

    Good luck,
    Sadiq.

  • #5
    New Coder
    Join Date
    Apr 2004
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thanks again...

    I greatly appreciate your help with this.

    I don't know if this will help you or not, but my table looks like this:

    Code:
    CREATE TABLE `talkback` ( 
    `id` INT(11) NOT NULL AUTO_INCREMENT, 
    `comments` TEXT NOT NULL, 
    `name` VARCHAR(80) NOT NULL, 
    `address` VARCHAR(80) NOT NULL,
    `age` VARCHAR(3) NOT NULL,
    `locale` VARCHAR(100) NOT NULL, 
    `date` VARCHAR(50) NOT NULL,
    `ip` VARCHAR(30) NOT NULL,
    `browser` VARCHAR(50) NOT NULL, 
    PRIMARY KEY (`id`))
    Right now, I have my code set up like this:

    Code:
    $insert = "INSERT INTO talkback(name, address, age, locale, comments, date, ip, browser) VALUES('$name', '$address', '$age', '$locale', '$comments', SYSDATE(), '$ip($_SERVER['HTTP_REMOTE_ADDR']', '$browser($_SERVER['HTTP_USER_AGENT']')";
    which I know isn't right because now I am getting:
    Parse error: parse error, expecting `T_STRING' or `T_VARIABLE' or `T_NUM_STRING'
    for that particular line (45) of my code.



    I have also tried:

    Code:
    $insert = "INSERT INTO talkback(name, address, age, locale, comments, date, ip, browser) VALUES('$name', '$address', '$age', '$locale', '$comments', SYSDATE(), '($_SERVER['HTTP_REMOTE_ADDR']', '($_SERVER['HTTP_USER_AGENT']')";
    but have received the same error.

    Any idea what I'm doing wrong??

    Thank you once again,
    Humble Dude

  • #6
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Ok, let's try something a little bit different:
    PHP Code:
    $insert "INSERT INTO talkback(id, name, address, age, locale, comments, date, ip, browser) VALUES(null, '".$name."', '".$address."', '".$age."', '".$locale."', '".$comments."', ".SYSDATE().", '".$_SERVER['HTTP_REMOTE_ADDR']."', '".$_SERVER['HTTP_USER_AGENT']."')"
    So I did a couple of things:
    • I added the id field and set it to null so that it auto increments. (BTW: I just noticed that you've set it to ID to NOT NULL.. that might be a problem?).
    • I put the variables outside of the string, and just concatenated them so that they're still part of the string. The idea is to get them outside of quotes.



    expecting T_STRING generally means expecting terminating string, so a closed quote.

    Hope that works for you!
    Sadiq.

  • #7
    New Coder
    Join Date
    Apr 2004
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Thanks...

    And the REMOTE ADDR & USER AGENT info will get inserted into the *ip* & *browser* rows of my table respectively?? In the order that the insert is carried out?

    Forgive me for being so ignorant...lmao. I'm trying really hard...

    Thanks a load,
    Humble Dude

  • #8
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yep, the order that you list your columns in have to match the order you list your values in. So if you switch ip and browser, you'll have to switch REMOTE ADDR and USER AGENT accordingly.

    Did you try out that code? Did it work?

    Sadiq.

  • #9
    New Coder
    Join Date
    Apr 2004
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    And again...

    Thanks...

    I'm going to try it out in just a bit...I got tied up with some stuff.

    I'll definitely let you know how it goes...this looks right...compared to what I've seen...which is how I learn, basically.

    I get something that works - THEN break it down to learn what it does...by going to the manual & other mySQL sites. I generally only ask in the forum if a) I'm in a really big hurry or b) I am just completely lost. So I really hate to be a pain about this one, but I am in just a little bit over my *skill level* at this point, I think...I'm still *just a beginner* with PHP & especially with mySQL. Gads, I only just recently learned how to use php includes...lmao. But it's starting to come now...

    Thanks tons for your help...I will let you know how it goes!!

    Humbly,
    Humble Dude

  • #10
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Cool. Hey no worries man, that's how most of us learn: by trial & error & codingforums!

    Good luck,
    Sadiq.

  • #11
    New Coder
    Join Date
    Apr 2004
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Grrrrr....

    Nope, not yet...getting closer, I think...lol...I'll jack around with it a bit & see what damage I can do tonight...

    Fatal error: Call to undefined function: sysdate()
    Is that where I might try *l dS of F Y h:i:s A*??

    Or should I try a different function?? Perhaps NOW...or date_format?

    Thanks, Sadiq...

    Humble Dude

  • #12
    Senior Coder
    Join Date
    Feb 2004
    Posts
    1,206
    Thanks
    0
    Thanked 0 Times in 0 Posts
    No I'm sorry, my fault (again!).

    For some reason I thought sysdate() was a PHP function, but it's in fact a MySQL function, so it should be left within the quotes:
    PHP Code:
    $insert "INSERT INTO talkback(id, name, address, age, locale, comments, date, ip, browser) VALUES(null, '".$name."', '".$address."', '".$age."', '".$locale."', '".$comments."', SYSDATE(), '".$_SERVER['HTTP_REMOTE_ADDR']."', '".$_SERVER['HTTP_USER_AGENT']."')"
    And that should do it! Let me know..

    Sadiq.

  • #13
    New Coder
    Join Date
    Apr 2004
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    will know in just a bit now...

    Working on that code right now. It's looking good, though...I'm out of the post page & into the *view* page right now...this should be it though. I received an email with the form's data...& it was (obviously) inserted in mySQL. Super, super cool, dude. I can't thank you enough for all your help.

    By the way...

    Do you know any JAVA?? I know squat about it, to be perfectly honest with you.

    I was using a target_blank on a link I wanted to open in a new window. But, as you probably know, there is no control of the window's size that way - so I ended up using my first bit of javascript to acquire control of that darn window!

    This is the script I'm using for my popup (which I generated over here at wsabstract):http://wsabstract.com/popwin/index.shtml
    Code:
    <script>
    
    //Popup Window Script
    //By JavaScript Kit (http://javascriptkit.com)
    //JavaScript tutorials and over 400+ free scripts
    
    function openpopup(){
    var popurl="mypopup.php"
    winpops=window.open(popurl,"","width=600,height=400,scrollbars,resizable,")
    }
    
    </script>
    Throughout my site, I expect there to be only 2-3 pages that will include a popup of any sort...& probably only one per page. But there is an instance in which I would like 2 different links on 1 page to open in separate windows.

    Is there a way to supplement the above to enable me to use 2 (or possibly more) links on the same page as a popup on click? Or - is this a PHP *if-else* statement I need to script? (Which I'm going to try & do anyway when I get a few minutes...might be a good exercise for me: if window A has been opened already, open window B, etc...I'd just like to know if there's a way to get it done now.)

    Thanks very, very much once again...

    Regards,

    Humble Dude

  • #14
    New Coder
    Join Date
    Apr 2004
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Hmmm...

    I think I just figured the JAVA thing out...make multiple functions in the script?? naming them (for instance) pop1, pop2, pop3, etc???

  • #15
    New Coder
    Join Date
    Apr 2004
    Posts
    12
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Well...

    The form is getting better...I think...lol. I'm down to the last 50 or so lines of code.

    Your stuff worked great!! The form gets submitted, everything (except the ip address) gets inserted in the table, & I get a copy of the name, email, age, location, & comments in my email. YES!! I'm not sure why I'm not picking up the ip address yet, I haven't looked at it yet...I'm looking at my *query 2 failed* error on my *view* page. Then I'm going to peek at that ip thingy & see what I can see...if you know what I mean. At any rate, this form should be good to go in an hour or 2. I'm hoping, anyway...would be cool to get his thing going tonight...

    If you're curious, my query 2 is (well, here's the last 50 lines):

    Code:
    mysql_pconnect( "localhost", "$username", "$password") 
                   or die( "Unable to connect to SQL server"); 
    mysql_select_db( "$db_name") or die( "Unable to select database"); 
    
        $numresults=mysql_query("SELECT name, address, age, locale, comments FROM talkback"); 
        $numrows=mysql_num_rows($numresults) 
                or die ("query 1 failed"); 
         
    if (empty($offset)) { 
        $offset=0; 
    } 
    
    $query = "SELECT name, address, age, locale, comments FROM talkback order by date DESC limit$offset,$limit"; 
    
    $result = mysql_query ($query) 
        or die ("query 2 failed"); 
             
    while ($row = mysql_fetch_row ($result)) 
    { 
    for ($i = 0; $i < mysql_num_fields ($result); $i++) 
        { 
        if ($i > 0) 
            print ("<br>");     
        if ($i == 0){ 
            print "<b>Name: </b>";     
        }
        if ($i > 0) 
            print ("<br>");     
        if ($i == 0){ 
            print "<b>Email address: </b>";     
        }
        if ($i > 0) 
            print ("<br>");     
        if ($i == 0){ 
            print "<b>Age: </b>";     
        }
        if ($i > 0) 
            print ("<br>");     
        if ($i == 0){ 
            print "<b>Location: </b>";     
        }
        if ($i > 0) 
            print ("<br>");     
        if ($i == 0){ 
            print "<b>Comments: </b>";     
        }
    
         
        print ($row[$i]); 
             
        } 
    print "<br><br><center><hr></center>"; 
    print ("<P>"); 
    } 
    if ($offset >= 3) { 
    $prevoffset = $offset - $limit; 
    print "<a href=\"view.php?offset=$prevoffset\">PREV</a>&nbsp;\n"; 
    } 
    $pages=intval($numrows/$limit); 
    if ($pages < ($numrows/$limit)){ 
    $pages=($pages + 1); 
    } 
    for ($i = 1; $i <= $pages; $i++) { 
    $newoffset = $limit*($i-1); 
    if ($newoffset == $offset) { 
    print "$i&nbsp;\n"; 
    } else { 
    print "<a href=\"view.php?offset=$newoffset\">$i</a>&nbsp;\n"; 
    } 
    } 
    
    //show next if not last 
    if (! ( ($offset/$limit) == ($pages - 1) ) && ($pages != 1) ) { 
    $newoffset = $offset+$limit; 
    print "<a href=\"view.php?offset=$newoffset\">NEXT</a><p>\n"; 
    } 
    ?>
    I have no idea what the hay I'm looking at in this part, but I'm slowly figuring it out. Hahahaha...I know there are a few if/else statements in there, which is something I'm just learning now with the help of a book. I don't even know if these will work like I expect them to yet, but I'll keep trying! Hahahaha...I a couple that I knew worked & ccopied & pasted them & changed the values & variables accordingly (I think...lol). Like I said, I just want to get things working & then learn on the fly while I'm reading & running through tutorials.

    Hey! Is there any money in this stuff????!!!! This is a lot of work!!!!!! LMAO....

    It is a lot of fun, though.

    Thanks very, very much for all your help, Sadiq...it is greatly, greatly appreciated...

    Back in a while,
    Humble Dude


  •  

    Posting Permissions

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