Register

If this is your first visit, please click the Sign Up now button to begin the process of creating your account so you can begin posting on our forums! The Sign Up process will only take up about a minute of two of your time.

Page 1 of 2 1 2 LastLast
Results 1 to 10 of 20
  1. #1
    Member
    Join Date
    Jan 2005
    Posts
    53
    Member #
    8598
    I thought this worked but it seems not.

    The connect to database is no problem but when I submit it just reloads the page without adding to the database and going to the success $display_block message.

    Here is the code (up to the connect to db stage):
    PHP Code:
    <?php
    if ($_POST['op'] != "add") {
        
    //bring in the form
        
    $display_block "<hr>Add Student and Course</hr>
        <form method=\"post\" action=\""
    $_SERVER['PHP_SELF'] . "\">
        <p><strong>Username: (your email address)</strong>
            <input type=\"text\" name=\"username\" size=30 maxlength=75></p>
        <p><strong>First Name:</strong>
            <input type=\"text\" name=\"fname\" size=30 maxlength=75></p>
        <p><strong>Last Name:</strong>
            <input type=\"text\" name=\"lname\" size=30 maxlength=75></p>
        <p><strong>Course Name:</strong>
        <select name=\"coursename\">
              <option>How to List and Sell Commerical Real Estate</option>
              <option>Marketing Small Businesses for Sale</option>
              <option>The 55+ Specialist</option>
        </select></p>
        <p><input type=\"submit\" name=\"submit\" value=\"Add Entry\"></p>
        </form>"
    ;
    global 
    $user$fname$lname$course;
            
    $user $_POST['username'];
            
    $fname $_POST['fname'];
            
    $lname $_POST['lname'];
            
    $course $_POST['coursename'];

    } else if(
    $_POST['op'] == "add") {
        
    //check for required fields
        
    if (($user == " ")  || ($fname == " ")  || ($lname == " ")  || ($course == " ")) {
            
    header("Location: addstudent_coursetest.php");
            exit;
            }
    I should mention that for some reason in past trials I have had to use $HTTP_POST_VARS instead of $_POST. I asked everyone and their dog but no one could figure that out. I am working on Dreamweaver MX 2003, both my machine and the host have PHP 5+ and MySQL 4+ and my browsers are up to date. That's a hole other kettle of fish!

    I am at my wits end and could really use some help, please and thank you.

  2.  

  3. #2
    Senior Member rosland's Avatar
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    1,944
    Member #
    2096
    I don't have time to elborate so I'll keep it short.
    Through a quick glance at your code here and your other post a few clicks down, you have a very messy/verbose coding style. It's very hard to look for errors.

    A few points:
    • When you use curly brackets, try to keep the opening and closing bracket on the same 'tab' level. That way it's much easier to keep track of which curly brace belongs to which block statement.
    • When you embed blocks of code within other blocks of code, use proper indentation. Otherwise it's close to impossible to see what belongs where.
    • When you call superglobals ($_POST, $_REQUEST etc), seperate them from the variable string. I know PHP 5.x accepts the style, but error-checking is so much more difficult when all dynamic content is camouflaged within normal strings.
    • If you have a substantial amount of HTML to be dynamically presented (echoed), write in clear text instead of hiding it within a variable. Then you avoid all the escape stuff, and it's easier to debug.
    • In the textfile supplied in your previous post, there seem to be a closing bracket missing.
    • All of the above is examplified below.

    PHP Code:
    ///// Indent brackets like this:
    if(statement)
    {
        if(
    statement)
        {
            
    # do something;
        
    }
        elseif(
    statement)
        {
            if(
    statement)
            {
                
    # do something else;
            
    }
            else
            {
                
    # do something different;
            
    }        
        }
    }
    else
    # do something default

    **************************

    ///not like this:
    if(statement){
        if(
    statement){
            
    # do something;
            
    }
    elseif(
    statement){
    if(
    statement){
                
    # do something else;
    }
            else{
      
    # do something different;
    }
    }
        
        
    # do something default;

    Superglobals:
    PHP Code:
    #Like this:
    $var "This is the name: ".$_POST['name']." from the form";


    # Not like this
    $var "This is the name: $_POST[name] from the form"
    Large blocks of HTML:
    PHP Code:
    <?php 
    if(condition)
    {
    ?>
        <form name="form1" method="post" action="">
              <input type="text" name="textfield">Name<br />
              <input type="text" name="textfield">Number<br />
              <input type="submit" name="Submit" value="Submit">
        </form>
    <?php
    }
    else
    print 
    "That's alright then!;
    ?>
    S. Rosland

  4. #3
    Senior Member rosland's Avatar
    Join Date
    Jul 2003
    Location
    Norway
    Posts
    1,944
    Member #
    2096
    BTW,
    $HTTP_POST_VARS is a deprecated style. It is still supported, but the shorthand $_POST is the one to be used.

    If you have PHP 5.x installed, it's "impossible" that the deprecated version works while the proper version fails.
    Make sure you don't have spelling errors (syntax), and double check that you actually have the PHP version you claim through a "phpinfo()" call.

    From the manual:
    $_POST
    Variables provided to the script via HTTP POST. Analogous to the old $HTTP_POST_VARS array (which is still available, but deprecated).
    S. Rosland

  5. #4
    Member
    Join Date
    Jan 2005
    Posts
    53
    Member #
    8598
    Thank you S. Rosland. When striking out on the self-taught road you always need someone to show the finer points.

    Here, is hopefully, easier code to read:
    PHP Code:
    if ($_POST ['op'] != "add"
    {
        
    //bring in the form
        
    $display_block "<hr>Add Student and Course</hr>
                <form method=\"post\" action=\""
    $_SERVER['PHP_SELF'] . "\">
                        <p><strong>Username: (your email address)</strong>
                        <input name=\"username\" type=\"text\" value=\"Username\" size=30 maxlength=75></p>>
                        <input name=\"fname\" type=\"text\" value=\"First Name\" size=30 maxlength=75></p>>
                        <input name=\"lname\" type=\"text\" value=\"Last name\" size=30 maxlength=75></p>>
                        <input name=\"username\" type=\"text\" value=\"Username\" size=30 maxlength=75></p>>
                        <p><strong>Course Name:</strong>
                        <select name=\"coursename\">
                                  <option>Course A</option>
                                  <option>Course B</option>
                                <option>Course C/option>
                        </select></p>
                        <p><input type=\"submit\" name=\"submit\" value=\"Add Entry\"></p>
                </form>"
    ;
    global 
    $user$fname$lname$course;
    $user $_POST ['username'];
    $fname $_POST ['fname'];
    $lname $_POST ['lname'] ;
    $course $_POST ['coursename'];

    else if(
    $_POST ['op'] == "add"
    {
                
    //check for required fields
                
    if (($user == " ")  || ($fname == " ")  || ($lname == " ")  || ($course == " ")) 
                {
                        
    header ("Location: addstudent_coursetest.php");
                        exit;
                } 
    The code from this point on seems to work if I run it without the form and place an string value in the variables.

    (I did check on PHP versions: my machine PHP5, the server host PHP4.1.2.) I have a theory that it is a Dreamweaver MX 2003 glitch because if I use any of the PHP toolbar options they always appear in the deprecated style. I will have to decide whether to put gas in the car or upgrade Dreamweaver.

    Anyway, I hope the code is easier on the eyes and mind and that someone can offer suggestions. Thank you, in advance!

  6. #5
    Member
    Join Date
    Jan 2005
    Posts
    53
    Member #
    8598
    I have realised that I needed a hidden input field for 'op'. I have added <input name=\"op\" type=\"hidden\" value=\"add\"> but still no success.

  7. #6
    Member
    Join Date
    Jan 2005
    Posts
    53
    Member #
    8598
    Wouldn't anyone help...please!

  8. #7
    Senior Member
    Join Date
    May 2003
    Location
    UK
    Posts
    2,354
    Member #
    1326
    I think you want the PHP code to run, when they click the 'op' button?

    Your HTML form button would be
    Code:
    <input type="submit" name="submit" value="go" />
    Then, your PHP would be
    PHP Code:
    if($_POST['submit']=="go") {
      
    // php code here
    }
    ?> 
    Hope this helps

  9. #8
    Senior Member visualAd's Avatar
    Join Date
    Jan 2003
    Location
    Slough, UK
    Posts
    201
    Member #
    434
    I answered your question here

  10. #9
    Member
    Join Date
    Jan 2005
    Posts
    53
    Member #
    8598
    Yes, VisualAd that answered my question and thank you very much...I posted a thank you the next day but for some reason it doesn't seem to have made it.

    I learned quite a lot from your post and it was truly appreciated.


    I do have another question to throw out to the masses. As I have a better understanding of using the form to insert into my database. I now realise that there is a change I should make. At present I am showing a list of course names but in fact I should be getting that list from my database, displaying it and just inserting a student_id into the course table when a new student adds another course. Hope, I am making sense.

    So, I was thinking that I should get the course name from the table.
    PHP Code:
    $get_course "SELECT course_name FROM course";
        if (! 
    mysql_query ($get_course)) 
            {
                 
    $error mysql_error();
                 return 
    false;
            } 
    Then in the form (this is where my brain starts to melt) I want the <select> field to display the courses from the course table.

    Another concern is, in learning about the relationships between the tables, the student - course is a many-to-many and I should make it into an one-to-many by adding a table containing only student_id and course_id (student_courseTable). So, if that is the case where should I be putting the student_id.

    I must admit this is a great example to learn on, maddening as it is I am learning a great deal.

    So, if someone could steer me in the right direction I would really appreciate the help. Thank you.

  11. #10
    Senior Member visualAd's Avatar
    Join Date
    Jan 2003
    Location
    Slough, UK
    Posts
    201
    Member #
    434
    You should give each ccourse a uniquie ID in your course database. You can then reference a coursei nthe student talbe simply using its ID - this will enable you rename courses easily and cuts down on data redundancy.

    About you many to many, yes, youu should create a linking table and turn it in to two many to one relationships. When displaying a list of courses, you should use the linking table. If you need any information like the course name, you can then construct a query which will join the three tables together such that:
    Code:
    SELECT student.name, course.name FROM student,course,student_course
        WHERE student.id=student_course.student_id AND
             course.id=student_course.course_id


Page 1 of 2 1 2 LastLast

Remove Ads

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
All times are GMT -6. The time now is 10:52 PM.
Powered by vBulletin® Version 4.2.3
Copyright © 2019 vBulletin Solutions, Inc. All rights reserved.
vBulletin Skin By: PurevB.com