Forum Moderators: coopster

Message Too Old, No Replies

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

         

blitzztriger

5:48 pm on Jul 20, 2007 (gmt 0)

10+ Year Member



hello, after trying with the text file, i would like to try with a textbox :

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

/*
* 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(array("\t", "\r", "\r\n"), array(' ', "\n", "\n"), $subject);
$subject = preg_replace(array('/[ ]+/', '/([ ]*\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($pattern, $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($matches);
//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.abudance' => $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.abudance' => $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($sql)
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.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>

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

<form action="test.php" 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>

Habtom

9:11 am on Jul 22, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



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

What are the % for?

Shouldn't the number of fields and input be the same?

blitzztriger

4:49 pm on Jul 22, 2007 (gmt 0)

10+ Year Member



oh! the % its in the colunm name, metal% its the column name hehe
and yes, the input should be = to fields, but was i sayed, thats my problem ;) i left "blank" for that someone could help me :)

eelixduppy

7:22 am on Jul 23, 2007 (gmt 0)



The variables have to go within the query. We are you getting the information from? Is it stored in the $_POST array or in the $planet array? If the latter, you must define the query AFTER you create the $planet array and use the values from that array within your query. Try it yourself to see what you get. Also, if you are including user-submitted date into the query, you should be using mysql_real_escape_string [php.net] to escape the string before use.