Web Analytics Made Easy -
StatCounter php/mysql help needed - CodingForum


No announcement yet.

php/mysql help needed

  • Filter
  • Time
  • Show
Clear All
new posts

  • php/mysql help needed

    I've got a contacts db with the usual fields (name, address, telephone, etc) in a table called "contacts" (obviously). I've been trying to work out how to make a form for a site that will allow me to enter the details into relevant boxes, hit a submit button, and the details get entered into the db, but I just cant get my head around it.

    Can anyone post even a basic template of the code I would need to use???


  • #2
    ok, assuming you have already made the HTML form, and assigned each element a unique name, you start off by creating a file conn.php or whatever you want to hold the connection details. This should be something like the following:

    PHP Code:

    $dbusername 'your_username';
    $dbpasswd 'your_password';
    $database_name 'your_database';

    $connection mysql_connect("$dbhost","$dbusername","$dbpasswd"
        or die (
    "Couldn't connect to server.");
    $db mysql_select_db("$database_name"$connection)
        or die(
    "Couldn't select database.");
    Then you create a file add.php or something and include the conn.php file:

    PHP Code:
    include ('db.php');

    $first_name $_POST['first_name'];
    $last_name $_POST['last_name'];
    $email_address $_POST['email_address'];

    # I did that to make the variables easier to remeber/read/recall etc

    $first_name stripslashes($first_name);
    $last_name stripslashes($last_name);
    $email_address stripslashes($email_address);

    # Then use stripslashes() incase the user entered any apostrophes or something

    $sql mysql_query("INSERT INTO users (first_name, last_name, email_address) VALUES ('$first_name', '$last_name', '$email_address')") or die (mysql_error()); 

    Correct anything I've done wrong, but I think that is an appropriate skeleton, you should add perhaps a mail() function and form validation.


    • #3
      Thanks newmand2 !!! I havent got the HTML form done yet - that was going to be my next question

      When I see the code I can understand what it does, so can edit it easily enough - I just cant get my head around starting it all off!!!


      • #4
        yes it is very hard to learn HOW to go about creating it, but once you have the general gist of it you can develop it easily


        • #5
          ok the HTML form. Whats the problem, you havent done it, you dont know how?


          • #6
            Originally posted by newmand2
            ok the HTML form. Whats the problem, you havent done it, you dont know how?
            Bit of both to be honest with you. I did a couple about a year ago for searching a db, but lost the files when my server died (yeah, I learnt the hard way about daily backups). I never really got around to getting to grips with adding to the db via a html/php page. I used to use mysqlfront, but I cant connect to my current webhost with it, and phpmyadmin is a pain !!!!


            • #7
              hmmm... OK, I'm still unclear about what you require help with? PHPmyAdmin is something that the db owner uses, unfortuantely I'm not too skilled in that region.

              I'd read up on creating HTML pages if you are unsure what you are doing, and visit the HTML/CSS of these forums


              • #8
                I can use phpmyadmin myself, but there will be a number of other people having to add data to the db, and if I let them loose at the root of the db, I know they will end up screwing it up. So I'm trying to create a web page with the fields for them to type the requested data, and then hit a button for it to insert the data into the db.

                That way, the only thing I have to worry about it duplicate entries.

                Sorry for the trouble.