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(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>[/PHP]