how to insert values into mysql, after parsing a submiting textbox?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • blitzztriger
    New Member
    • Jul 2007
    • 10

    how to insert values into mysql, after parsing a submiting textbox?

    Hello!!
    how do i insert values into mysql , after parsing a submiting textbox??
    I made the arrays, so this should be a basic insertion of them in the db, but something is missing, or in the wrong place...can anyone help me??

    i didnt made anything in the VALUES (unless the POST [textbox]), because i bet the error is there (yes, the arrays are missing):

    $sql = "INSERT INTO dgplanets ( ID, coords , player name , alliance , name , ground , orbit , metal% , mineral% , food% , energy% , pop ) VALUES ('".$_POST['textbox']."')"; */

    here“s the code :
    [PHP]<?php

    /*
    * makes the connection
    * and select the database
    */
    $conexao = mysql_connect(" localhost", "root", "12345")
    or die ("Error connecting to db");
    $db = mysql_select_db ("dg")
    or die ("error selecting db");

    //here is where i should input the arrays??
    $sql = "INSERT INTO dgplanets ( ID, coords , player name , alliance , planet name , ground , orbit , metal% , mineral% , food% , energy% , pop ) VALUES ('".$_POST['textbox']."')"; */




    $_POST['data'] = (empty($_POST['data'])) ? '' : $_POST['data'];

    $data = array(
    'parsed' => FALSE,
    'player' => array('name' => ''),
    'planet' => array()
    );

    if (!empty($_POST['data']))
    {
    $subject = trim($_POST['data']);
    $subject = str_replace(arr ay("\t", "\r", "\r\n"), array(' ', "\n", "\n"), $subject);
    $subject = preg_replace(ar ray('/[ ]+/', '/([ ]*\n+[ ]*)+/') , array(' ', "\n"), $subject);

    //echo '<pre>';
    //echo $subject;
    //echo '</pre>';

    //
    // First we find the player name
    //
    $pattern = '/';
    $pattern .= '(.+) [Logout]\n'; // player name
    $pattern .= '(?:';
    $pattern .= ')*?';
    $pattern .= '/iU'; // tirase ?

    $matches = array();

    if (preg_match($pa ttern, $subject, $matches))
    {
    $data['player']['name'] = $matches[1];
    }

    //
    // Now we parse the planet data
    //
    $pattern = '/';
    $pattern .= '(.+) - \((\d+)\) (.+) (.+)\n';
    $pattern .= '([\d,]+) \(\+([\d,]+)\) (\d+)% ([\d,]+) \(\+([\d,]+)\) (\d+)% ([\d,]+) \(\+([\d,]+)\) (\d+)% ([\d,]+) \(\+([\d,]+)\) (\d+)%
    ';
    $pattern .= '/i';

    $matches = array();
    if ($size = preg_match_all( $pattern, $subject, $matches))
    {
    $data['player']['planets'] = $size;

    foreach ($matches[0] as $key => $val)
    {
    $matches[0][$key] = str_replace("\n ", ' ', $val);
    }


    //echo '<pre>';
    //print_r($matche s);
    //echo '</pre>';

    for ($i = 0; $i < $size; $i++)
    {
    $planet = array(
    'planet.name' => $matches[1][$i],
    'id' => $matches[2][$i],
    'ground' => $matches[3][$i],
    'orbit' => $matches[4][$i],
    'metal.ouput' => str_replace(',' , '', $matches[6][$i]),
    'metal.stored' => str_replace(',' , '', $matches[5][$i]),
    'mineral.abudan ce' => $matches[10][$i],
    'mineral.ouput' => str_replace(',' , '', $matches[9][$i]),
    'mineral.stored ' => str_replace(',' , '', $matches[8][$i]),
    'food.abudance' => $matches[13][$i],
    'food.ouput' => str_replace(',' , '', $matches[12][$i]),
    'food.stored' => str_replace(',' , '', $matches[11][$i]),
    'energy.abudanc e' => $matches[16][$i],
    'energy.ouput' => str_replace(',' , '', $matches[15][$i]),
    'energy.stored' => str_replace(',' , '', $matches[14][$i]),
    'metal.abudance ' => $matches[7][$i],
    'pop' => str_replace(',' , '', $matches[17][$i]),
    );
    $data['planet'][] = $planet;
    }
    $data['parsed'] = TRUE;
    }
    }







    /*
    * executes the query
    */
    $sql = mysql_query($sq l)
    or die ("there was a mistake");
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dt d">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitl ed Document</title>
    </head>

    <body>
    <h1>INSERT PLANET LIST</h1>

    <form action="test.ph p" method="post">

    <label for="texto"></label>
    <textarea name="textbox" id="text" rows="10" cols="30" />
    </textarea><br />

    <input type="submit" value="insert">

    </form>
    </body>
    </html>[/PHP]
  • blitzztriger
    New Member
    • Jul 2007
    • 10

    #2
    any clue?!?! you can give just a small example :)

    Comment

    • tscott
      New Member
      • Jul 2007
      • 22

      #3
      Hey, Yeah it is on $_POST. Post is defined by the script referring to it so therefore you must redefine the variable so it can be edited.

      For Example:
      $post = $_POST['whatever'];

      Then do

      $post = explode('Sepera tor' , $post);

      Then send it to you query with post instead of $_POST['post'];

      I like your coding style ;).

      ~Tyler

      Comment

      Working...