SitePoint Sponsor

User Tag List

Results 1 to 8 of 8
  1. #1
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Alright, I'm stuck.

    I have a simple file that allows people to insert data into a database, and then add some options as well - the limit is 8 options. I have 8 text fields, and a dropdown that lets you choose how many you want (anywhere from 2-8)...

    Now, I'm trying to get it so that whatever number they choose, the value of the form fields up to that number will be inserted into the SQL database - If you choose 5 and enter text into the first 6 boxes, tough luck - you don't get anymore.

    Here's the code for the form:

    PHP Code:
        <form action="<?php echo($PHP_SELF); ?>" method="post">
        Title: <input type=text name="title" size="40" maxlength="40">
        <p>
            Number: <select name="num">
        <option value="2"> == 2 == </option>
        <option value="3"> == 3 == </option>
        <option value="4"> == 4 == </option>
        <option value="5"> == 5 == </option>
        <option value="6"> == 6 == </option>
        <option value="7"> == 7 == </option>
        <option value="8"> == 8 == </option>
        </select><br>
        <b>Poll Options:</b><br><hr><br>
            Option 1: <input type="text" name="option_1" size="40" maxlength="25"><br>
            Option 2: <input type="text" name="option_2" size="40" maxlength="25"><br>
            Option 3: <input type="text" name="option_3" size="40" maxlength="25"><br>
            Option 4: <input type="text" name="option_4" size="40" maxlength="25"><br>
            Option 5: <input type="text" name="option_5" size="40" maxlength="25"><br>
            Option 6: <input type="text" name="option_6" size="40" maxlength="25"><br>
            Option 7: <input type="text" name="option_7" size="40" maxlength="25"><br>
            Option 8: <input type="text" name="option_8" size="40" maxlength="25"><br>
        <input type="submit" name="addsubmit" value="Add">
        </form>
    Here's the code I'm having trouble with (processed when the form is submitted, of course):

    Code:
          $num = ($num > 8) ? 8 : $num;
    
              for ($i = 1; $i <= $num; $i++) {
      	    $var = "option_$i";
      	    optionInsert($$var, 25, $pid);
      	  }
    As you can see, each field in the form is in this format: option_1, option_2, etc - the $$var variable returns a value of "$option_1", "$option_2", etc - I'm trying to pass this to the "optionInsert" function, which inserts the data into the database.

    It's not inserting however, and after some testing, I think the problem is with the above code - can I not use "variable variables" that way?

  2. #2
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Chris your code looks okay what does the function optionInsert() look like?
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  3. #3
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It looks like this (SQL details removed, of course)

    Code:
      function optionInsert($option, $num, $pid) {
        $boolean = (strlen($option) > $num) ? 0 : 1;
    
        if ($boolean == 1) {
          $db = @mysql_connect("localhost", "user_name", "user_password");
          if (!$db) { echo("<p>Unable to connect to the database server at this time.</p>"); }
          if (!@mysql_select_db("db_name")) { echo("<p>Unable to locate the " . 
                                                  "database at this time.</p>"); }
          $query = "INSERT INTO table_name SET optiontext='$option', pid='$pid'";
              if (!mysql_query($query)) {
    	    echo("Error...cannot add $option<br>");
              }
        } else {
        echo("Option text too long.<br>");
        }
      }
    I'm sure you'll see several ways to make it more efficient. Overall it seems like the data isn't being passed to the function - echoing out the $$var variable prints "$option_1", for example, rather than the actual VALUE of $option_1...

  4. #4
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That is really weird becuase I tried your exact code took out the function, and just printed $$var and it gave me the values I put in the text fields, what version of PHP you using?
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  5. #5
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Oh, it's not inserting into the database, but it's giving it blank values for "pid" and "optiontext", just so you know, so obviously the value of $$var is blank - I suppose creating a variable variable like that overwrites any old one.

  6. #6
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Stand by - I may have it working - don't go out of your way to help me yet, I don't want you to waste your time! BRB

  7. #7
    Dumb PHP codin' cat
    Join Date
    Aug 2000
    Location
    San Diego, CA
    Posts
    5,460
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Weird Iused this code with your form and the function still took the values and printed them to the screen, you say the reocrds get added with blank values?

    <?
    if ($addsubmit) {
    $num = ($num > 8) ? 8 : $num;

    function printit($var) {
    print $var."<br>";
    }

    for ($i = 1; $i <= $num; $i++) {
    $var = "option_$i";
    printit($$var);
    }
    }
    ?>
    Please don't PM me with questions.
    Use the forums, that is what they are here for.

  8. #8
    SitePoint Wizard TWTCommish's Avatar
    Join Date
    Aug 1999
    Location
    Pittsburgh, PA, USA
    Posts
    3,910
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well this is odd - I changed my function around to disapprove of blank variables, and now it works! I'm a tad confused as to why it didn't work, but I'm glad it's working now.

    Thanks for your time - I sure feel stupid.


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
  •