Storing Array Values into a Table...

Status
Not open for further replies.

Rascagua

New member
Jun 18, 2007
281
3
0
I have been looking all over the internet for a tutorial or something.

here is my code, basically, it does this:

splits some information into an array, and inserts each seperate array value into a table on my database...

only problem is:

When im hitting submit... it just loads.. and loads... and loads... then finally it says that it ran out of time

PHP:
<?php      

      //array values form
      $form = '<form method="post" action="splitstring.php">
      <textarea name="textfield" cols="30" rows="20"></textarea>
       <input type="submit"/>
    </form>';
      echo $form ;

      //split form value into array
      if($_POST["textfield"]!=""){
      $keywords = $_POST["textfield"];
      $keywords = split("[\n]+", $keywords);

     // Connect to the database

      $DB = mysql_connect("localhost", "root", "");
      mysql_select_db("database", $DB);

       // Create the qyery string
      $query = "INSERT INTO table(word) VALUES";

      // Loop through the array
      for($i = 0, $c = count($keywords); $i < $c; $i + 1) {
        // Add the next batch of values to the query string
        $query .= "({$keywords[$i]})";

        // Add a comma is this is not the last batch
        if($i + 1 < $c) {
          $query .= ", ";
        }
      }

      // Execute the query
      $RESULT = mysql_query($query, $DB);

      // Check the results

      if(!!$RESULT) {
        echo "Query was successfull";
      }
      else {
        echo "Query failed
                    <blockquote>". $query ."</blockquote>
                    <blockquote>". mysql_error($DB) ."</blockquote>";
      }
     
      }

?>

help? or a snippet of code to shorten this? or a pointer in the right direction would be greatly appreciated.
 


so, all I would need to do is:

PHP:
serialize($array)
mysql_query(INSERT INTO table name (field) VALUES ($array)
?

if so... thank you very much stanley
 
PHP:
$keywords = $_POST["textfield"];
$keywords = split("[\n]+", $keywords);

at first glance i noticed you are first defining $keyword as a $variable and then on the next line its being defined as an array..
 
ozone, i don't see anything wrong with that code.
split() turns what was once a string into an array by chopping up the string at every [\n]+
so if $keywords = "blah\nbleh\nblarg" then splitting it turns it into $keywords = array("blah","bleh","blarg")
 
not sure where you are going with that ozone, but there's nothing wrong with that code. split() turns a string into an array.

also, serializing has no place here as far as i can tell.

Rascagua, anytime you get a timeout while using a loop, the loop is the culprit. in your case, you are trying to add the value of 1 to $i but your syntax is off. $i + 1 is NOT the same as $i++. the former adds the number 1 to the variable $i but doesn't store the new value, so $i isn't being incremented and still equals 0, thus causing the infinity of death loop.

also, you need to watch your syntax in the mysql query. the values need quotes around them to be treated properly.

i would suggest using echo to see what your variables look like when you're testing your scripts, that's how i found your problems.

below is what i ended up with and it works fine. notice i also added:
- the empty() function to your initial test of $_POST
- took out your double !! on $RESULT (i don't get why you did that?)
- and print_r to test if split() was doing it's job




PHP:
<?php      

      //array values form
      $form = '<form method="post" action="test.php">
      <textarea name="textfield" cols="30" rows="20"></textarea>
       <input type="submit"/>
    </form>';
      echo $form ;

      //split form value into array
      if(!empty($_POST["textfield"])){
      $keywords = $_POST["textfield"];
      $keywords = split("[\n]+", $keywords);
      print_r($keywords);
      echo "<BR>";
     // Connect to the database

      include('db.php');

       // Create the qyery string
      $query = "INSERT INTO test (testWord) VALUES";

      // Loop through the array
      for($i = 0, $c = count($keywords); $i < $c; $i++) {
        // Add the next batch of values to the query string
        $query .= "('{$keywords[$i]}')";

        // Add a comma is this is not the last batch
        if($i + 1 < $c) {
          $query .= ",";
        }
        echo $query;
        echo "<BR>";
        echo $c;
        echo "<BR>";
      }

      // Execute the query
      $RESULT = mysql_query($query);

      // Check the results

      if($RESULT) {
        echo "Query was successfull";
      }
      else {
        echo "Query failed
                    <blockquote>". $query ."</blockquote>
                    <blockquote>". mysql_error() ."</blockquote>";
      }
     
      }

?>
 
Status
Not open for further replies.