Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: my first database project & a paying client!!

  1. #1

    my first database project & a paying client!!

    so, i've only been studying php for a short (very short) while, but get this email from a potential client tonight:

    Hi Brendan,

    Thanks for the follow up!

    Thatís a cool site you have there. I havenít been able to do a lot with mine yet. What I need will be a web based front end to a database. Basically Iíll need people to fill out info in the web site and then either have it saved automatically to a database or emailed it to me so I can process it. No need for cool looking graphics


    Let me know if thatís something youíve done before.

    this gentleman owns a business that buys old houses from people, and then resells them.


    well, i havent done it before.. but lol.. whats that got to do with anything?

    i'm still VERY MUCH a newbie with php, but feel comfortible enough with web technologies to try and tackle it..

    i've seen html forms that send the info straight to your email. but if i recall right, the info is sorta hard to read through. and since i believe this guy has a programing background, i'd prefer a way to send the info straight to a database.

    any suggestions on approaching this project, or heh heh.. maybe where to learn how to do it? are there any pre-built php/html forms out there i could use and modify? HELP!! i might be biting off more than i can chew.. but i'd rather have somethitn to chew on than be hungry..

  2. #2
    it's pretty easy to send the info from a html or php form to a mysql database. let's see what i can whip up...
    PHP Code:
    <?php
    $user 
    "USERNAME"//account username
    $pass "PASSWORD"//account password
    $db "DATABASE"//database name
    $link mysql_connect"localhost"$user$pass ); //connect to database
    function enter() //enters information into database
    {
    global 
    $db//globalize variables
    mysql_select_db$db );
        global 
    $subject;
        global 
    $name;
        global 
    $date;
        global 
    $entry;
        
    $date date("Ymd"); // below, set tablename to the name of your table
        
    $query "INSERT INTO tablename ( subject,
            name,
            date,
            entry
             )
            values( '
    $subject', 
                '
    $name',
                '
    $date',
                '
    $entry'            
                 )"
    ;
        
    mysql_query$query$link )
        or die (
    "couldn't add to table: "
        
    .mysql_error() );
    }
    function 
    write_form()
    {
        print 
    "<form action=\"$PHP_SELF\" method=\"POST\" enctype=\"multipart/form-data\">";
        print 
    "<p><strong>Name</strong><br><input type=\"text\" name=\"name\" value=$name>";
        print 
    "<p><strong>Subject</strong><br><input type=\"text\" name=\"subject\" value=$subject>";
        print 
    "<p><strong>Entry</strong>";
        print 
    "<br><textarea name=\"entry\" cols=50 rows=7></textarea>";
        print 
    "<p><input type=\"submit\" value=\"Submit\">";
        print 
    "</form>";
    }
    if( 
    $entry == "" )
    {
        
    write_form(); // if the entry field is blank, write the form again
    }
    else
    {
        
    enter();
        print 
    "<p>Thank you $name. Your message has been sent.</p>";
        print 
    "<p>Entry: $entry</p>";
    }
    ?>
    this is a php form by the way. another option is to use an html form which accesses a php file to enter info into the database. this code assumes you have a mysql database setup with fields for ID, subject, name, date, entry. tablename would be the name of your table. of course, you can add other fields.

  3. #3
    Quote Originally Posted by bwh2
    it's pretty easy to send the info from a html or php form to a mysql database. let's see what i can whip up...

    ..................

    this is a php form by the way. another option is to use an html form which accesses a php file to enter info into the database. this code assumes you have a mysql database setup with fields for ID, subject, name, date, entry. tablename would be the name of your table. of course, you can add other fields.
    hey!! thanks a lot!!
    going over your code has helped a lot!!
    i was looking for a way to validate the form, & came across this tutorial:
    http://www.php-mysql-tutorial.com/form-validation-with-php.php
    but i'm not quite savvy enough to make the j.script work with the php code i got from you... i'll try again early this week..
    currently, i'm trying to get this phpmyadmin thing figured out for creating the database to connect to..

    again - THANKS A LOT!!! your code gave me something to refer back to while pouring through a TON of online tutorials!! i'm sure after i get my phpmyadmin questions figured out, i'll be back with a question or two about the code you gave me!



  4. #4

  5. #5
    I use simple regular expressionsform validation, easy to use and provides a nice prompt for users. Here is a sample, where the comment for "if it is ok" is the section you would put your processing code, yours will look different than mine but the regular expressions will look the same.
    PHP Code:
    if ($submitmessage == "SUBMIT") {
        
    //error checking using regular expressions
        
    if (!eregi('^[a-zA-Z0-9_\-\.]+@[a-zA-Z0-9\-]+\.[a-zA-Z0-9\-\.]+$'$email)) {
            echo 
    "<br>That is not a valid email address, please go <a href='javascript:history.back(1)'>back</a> and try again.";
            exit();
        }else if (!
    eregi('[a-zA-Z0-9]'$name)){
            echo 
    "<br>Please go <a href='javascript:history.back(1)'>back</a> and fill the name field.";
            exit();
        }else if (!
    eregi('[a-zA-Z0-9]'$message)){
            echo 
    "<br>Please go <a href='javascript:history.back(1)'>back</a> and fill the message field.";
            exit();
        
    //if it's ok then add the message
        
    }else{
        
    $sql "INSERT INTO smsdata SET name='$name', email='$email', message='$message', ip='$ip', date='$date'";
        if (@
    mysql_query($sql)) {
            echo 
    "<p>Your message has been added.</p>";
        }else{
            echo 
    "<p>There was an error adding your message.</p>";
        }
    }

    In simpler times this meant something
    <embed src="http://www.smorgyporf.com/footer.swf" quality="high" bgcolor="#130e14" width="300" height="59"/embed>

  6. #6
    Quote Originally Posted by bwh2
    you're welcome. if you need help w/phpmyadmin let me know. it's what i use to setup my databases.

    thanks for the offer!!!
    i just saw a demonstration of how to create a database using navicat - which looked SUPER easy. i'm told that phpMyAdmin is also simple to use.. but i cant seem to figure out what to do after i hit "create database"

    fortunatly, my project is pretty straightforward - a web form collecting a few pieces of data (name, email address, phone#, etc, etc. etc.) after the user enters it, and sends it to the DB. i know to do that, i need to add rows, tables, and collums.. just not sure "where" to do that. and i also know i'm gonna need a password, user, and id's for the php. just dont know where to set those up in phpMyAdmin either.

    anychance you could show me how to create somthing simple with phpMyAdmin? i figure if i know how to create a simple DB with just a few pieces of info (name/email/phone #) then i'd be able to modify that to my speciffic needs by adding more to it later.

  7. #7
    Quote Originally Posted by GOLGO-13
    anychance you could show me how to create somthing simple with phpMyAdmin? i figure if i know how to create a simple DB with just a few pieces of info (name/email/phone #) then i'd be able to modify that to my speciffic needs by adding more to it later.
    yeah, i'll whip up something with screenshots in the morning when i'm on my good computer.

  8. #8
    man.. its like CHRISTMAS IN HERE!!



    (hmm.. let me go see if i can go find someone in here to go help.. )

  9. #9
    sorry, i forgot that my schedule was ridiculous today. i'll post the screenshots and whatnot on phpadmin tomorrow just after 4.

  10. #10
    Quote Originally Posted by bwh2
    sorry, i forgot that my schedule was ridiculous today. i'll post the screenshots and whatnot on phpadmin tomorrow just after 4.
    no sweat, after some searching, i found a tutorial online, and was able to get the tables part figured out!

    heh heh.. now after completing that tutorial, i *think* i'm having some path problems.. i either get this error:

    Warning: Unknown MySQL Server Host 'localhost/private/API/brendan' (0) in c:\phpdev\private\api\script.php on line 16

    Warning: MySQL Connection Failed: Unknown MySQL Server Host 'localhost/private/API/brendan' (0) in c:\phpdev\private\api\script.php on line 16
    Unable to connect to database
    refferancing this part of the code (line 16 starts @ "mysql_" ):
    PHP Code:
    $DBhost "localhost/private/API/brendan";
    //$DBuser = "user";
    //$DBpass = "pass";
    $DBName "brendan";
    $table "details";
    mysql_connect($DBhost) or die("Unable to connect to database"); //took these out of this line of code: $DBuser,$DBpass. was mysql_connect($DBhost,$DBuser,$DBpass) 
    or when i change the code to this:
    PHP Code:
    $DBhost "localhost"
    i dont get any errors, but nothing happens when i enter the info into the web form. it doesnt write to the database. you'll also notice i commented out the password, and user, cause i cant figure out where to create those inside phpMyAdmin

    so, in phpMyAdmin, when you create a database, "where" does it save the database to (by default)? and is there an extension on it?

    this path: "localhost/private/API/brendan" was my *best guess* (i moved what i "thought" was the db from my Mysql folder, into the folder i was currently working in).

    inside of the "brendan" folder, are "details.frm", "details.myd" and "details.myi" ("details" is the name of my table, in the 'brendan' database)

  11. #11
    I out the mysql connection in a varliable called link:

    PHP Code:
    $link = @mysql_connect("localhost",$username,$password); 
    then you just call $link whenever you need to access the the database. Also I don't understand why your reffrencing the table into a variable?

    Sample of a full conention:
    PHP Code:
    //link up to mysql database 
    $username="your_username"
    $password="your_password"
    $database="your_database_name"
    $link = @mysql_connect("localhost",$username,$password); 

    if (!
    $link){ 
        echo 
    "<p> Could not connect to the database server. </p>"
            exit(); 
    }else{ 
        
    //for connection test, comment out if successfull
    echo "<p> Connected to databse server successfully. </p>"

      
    mysql_select_db($database$link) || die ("Unable to select database"); 

    if (!@
    mysql_select_db("$database") ) { 
        echo 
    "<p> Unable to locate the message database. </p>"
            exit(); 
    }else{ 
        
    //for connection test, comment out if successfull
    echo "<p> Connected to the message database successfully. </p>"

    BTW: you have to put the username and pass info in the mySQL connect function criteria.
    In simpler times this meant something
    <embed src="http://www.smorgyporf.com/footer.swf" quality="high" bgcolor="#130e14" width="300" height="59"/embed>

  12. #12
    aknatn
    thanks so much for BOTH your posts!!
    i'm off to the gym now, but i'll give your last suggestion a try when i get back!

  13. #13
    Quote Originally Posted by GOLGO-13
    aknatn
    thanks so much for BOTH your posts!!
    i'm off to the gym now, but i'll give your last suggestion a try when i get back!
    NP, the basic stuff is always one of the biggest hurdles. sql syntax is pretty straight forward, and PHP is really good at manipulating MySQL, it's actually kinda fun when you get going.
    In simpler times this meant something
    <embed src="http://www.smorgyporf.com/footer.swf" quality="high" bgcolor="#130e14" width="300" height="59"/embed>

  14. #14
    hmm.. so i *thought* i found where to change the user/password info, and after i made the changes, it told me:
    You have updated the privileges for admin@%.
    Remember reload the server.
    but when i run the script i get this retunred in the browser:
    Could not connect to the database server.
    when i run and comment out the password, it says i'm connected, but wont write to the database after entering the info into the forms. i hit "submit" and it just clears the forms, and does not return the: "Thank you $name. Your message has been sent." message.

    these are the other errors i found will poking my head around phpMyAdmin:

    PMA Database ... not OK[ Documentation ]
    General relation features Disabled
    Your configuration file contains settings (root with no password) that correspond to the default MySQL privileged account. Your MySQL server is running with this default, is open to intrusion, and you really should fix this security hole.

    (note if you are using phpdev for local development only then only localhost (127.0.0.1) i.e. you have rights on the database and the above warning is not strictly relevant)
    (fyi.. i DONT have anything important stored in there.. but it'd be nice to know how to fix that hole)

    here are the complete scripts i'm using:
    PHP Code:
    <?
    $user
    ="user";
    $password="mypassword";
    $database="mydatabase";
    ?>

    (i took the opening and closing php tags out of this one, cause the forms were showing up in this post..)
    PHP Code:
    include("dbinfo.inc.php");
    //link up to mysql database
    $link = @mysql_connect("localhost",$username,$password); 

    if (!
    $link){
        echo 
    "<p> Could not connect to the database server. 

    "
    ;
            exit();
    }else{
        
    //for connection test, comment out if successfull
    echo "<p> Connected to databse server successfully. 

    "
    ;
    }
      
    mysql_select_db($database$link) || die ("Unable to select database");

    if (!@
    mysql_select_db("$database") ) {
        echo 
    "<p> Unable to locate the message database. 

    "
    ;
            exit();
    }else{
        
    //for connection test, comment out if successfull
    echo "<p> Connected to the message database successfully. 

    "
    ;
    }

    function 
    enter() //enters information into database
    {
    global 
    $db//globalize variables
    mysql_select_db$db );
        global 
    $subject;
        global 
    $name;
        global 
    $date;
        global 
    $entry;
        
    $date date("Ymd"); // below, set tablename to the name of your table
        
    $query "INSERT INTO mytable ( subject,
            name,
            date,
            entry
             )
            values( '
    $subject',
                '
    $name',
                '
    $date',
                '
    $entry'            
                 )"
    ;
        
    mysql_query$query$link )
        or die (
    "couldn't add to table: "
        
    .mysql_error() );
    }
    function 
    write_form()
    {
        print 
    "<form action=\"$PHP_SELF\" method=\"POST\" enctype=\"multipart/form-data\">";
        print 
    "<p><strong>Name</strong><br><input type=\"text\" name=\"name\" value=$name>";
        print 
    "<p><strong>Subject</strong><br><input type=\"text\" name=\"subject\" value=$subject>";
        print 
    "<p><strong>Entry</strong>";
        print 
    "<br><textarea name=\"entry\" cols=50 rows=7></textarea>";
        print 
    "<p><input type=\"submit\" value=\"Submit\">";
        print 
    "</form>";
    }
    if( 
    $entry == "" )
    {
        
    write_form(); // if the entry field is blank, write the form again
    }
    else
    {
        
    enter();
        print 
    "<p>Thank you $name. Your message has been sent.</p>";
        print 
    "<p>Entry: $entry</p>";

    i've found another tutorial, that shows how to do the same thing... but its not as "elegant" of a soloution as the ideas i got from aknatn & bwh2. (waaaay more pages invloved, waaaay more code, & harder to modify for my end goal, etc..)

    however, i WAS able to write to the database using that other tutorial...
    (still had the comment out the password though). it was just "ugly" and anything but compact.

    so.. i'm not sure if i've changed the code above somehow in a bad way, or if i havent changed the password properly.. or.. or of anyting esle i might have messed up..

    i much prefer aknatn & bwh'stemplate (the code above) and would much rather use thier suggestions as a base.. than the other stuff i've found.

    what am i doing wrong?
    (my first guess is i havent changed the password properly)

  15. #15
    I suggest verifying globals are turned on and that your password is correct. Your host should verify these both for you.
    In simpler times this meant something
    <embed src="http://www.smorgyporf.com/footer.swf" quality="high" bgcolor="#130e14" width="300" height="59"/embed>

Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

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

Posting Permissions

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

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012