No había visto esta respuesta.
Yo sé que hay que controlar bien las variables, sencillos controles para la variable, tipo de si el campo está vacío, caracteres introducidos, etc, pero bueno.
Me da curiosidad el tema del get, me gustaría saber poder hacer esto sin register globals.
El código es así:
Código PHP:
//error_reporting(0);
//First we create the database connection that we are going to work with.
$connection = @mysql_connect("localhost", "user", "password");
mysql_select_db("food", $connection);
//Second we take a variable to choose if wi8e fill out the formularie or if we are adding the data.
$val = $opc;
switch($val)
{
case 0:
fillouttype();
break;
case 1:
addtype();
break;
default:
echo("Error.");
exit;
}
//Function to fill the formularie out.
function fillouttype()
{
global $connection;
echo '<form action="add-type-of-food.php?opc=1" method="post" name="typeoffood">
Description Spanish: <input type="text" name="description_es" maxlength="20" /><br />
Description English: <input type="text" name="description_en" maxlength="20" /><br />
Description Japanese: <input type="text" name="description_jp" maxlength="20" />
Romaji: <input type="text" name="romaji" maxlength="20" /><br /><br />
Previously Added: <select name="previouslyadded"><option selected>Verify</option>';
$addedtypes = mysql_query("SELECT description FROM type_es ORDER BY description", $connection);
for($x = 0; $x < mysql_num_rows($addedtypes); $x++)
{
echo '<option>'.mysql_result($addedtypes, $x, "description").'</option>';
}
echo '</select><br /><br /><input type="submit" value="Submit!" name="submit" /></form>';
mysql_close($connection);
}
//Function to add the data to the database.
function addtype()
{
global $connection, $description_es, $description_en, $description_jp, $romaji;
if($description_es == '' || $description_en == '' || $description_jp == '' || $romaji == '')
{
echo 'You must fill all the fields out. Please come back and do it.<br />
<input type="button" value="Come Back!" onclick="javascript:history.go(-1)" />';
exit;
}
$tables = array("type_es", "type_en", "type_jp");
$description_value = array($description_es, $description_en, $description_jp);
foreach($tables as $value => $content)
{
if($content == "type_jp")
{
mysql_query("INSERT INTO ".$content." VALUES ('', '".$description_value[$value]."', '".$romaji."')", $connection);
}
else
{
mysql_query("INSERT INTO ".$content." VALUES ('', '".$description_value[$value]."')", $connection);
}
}
echo "<input type=\"button\" value=\"Add other!\" onclick=\"javascript:location.href='add-type-of-food.php'\" />";
mysql_close($connection);