0
votes

In Joomla, I am trying to post a form created directly in an article via php DirectPHP and save data to database. I don't know why but sometimes it saves the data and sometimes not. It looks like there was some cache or something because if I get the field value displayed it still shows me the first value of the form I filled out at first. If I make any change to the code (e.g. add one row to echo whatever it works but then again the form displays this last record). Any ideas please (I don't want to use Chronoforms component).

The code in the article:

if($_GET['sent']=="ok")
{
$db = mysqli_connect("localhost", "xxxx", "xxxx", "xxxx") or die("Database not found");
mysqli_query($db, "INSERT INTO subscribe_form VALUES('','".$_GET['email_address']."')");
mysqli_close($db);
$form_sent = 1;
echo "Thank you, your email address was saved.";
}
if($form_sent==0)
{
echo "Enter your email address if you want to get subscribed.";
echo "<form action='index.php' method='get'>";
echo "</br></br>Your email address: <input type='text' name='email_address'/>";
echo "<input type='hidden' name='sent' value='ok'/>";
echo "<input type='hidden' name='option' value='com_content'>";
echo "<input type='hidden' name='Itemid' value='240'>";
echo "<input type='hidden' name='id' value='36'>";
echo "<input type='hidden' name='view' value='article'>";
echo "<input type='submit' name='odeslano' value='SAVE' />";
}
?>
1
You really need to look at re-writing your code to use the Joomla API. Currently, anyone can easily inject a load of values to your database - Lodder
Yes I know, I will but first I need to resolve the issue with saving to database. - Lukáš Ježek
Some solid hints here and plenty of other great/explained examples at Joomla Stack Exchange. - mickmackusa

1 Answers

0
votes

The reason why your code sometimes saves the data and sometimes it doesn't is because if the email contains an escape character then it can (silently) break the query. You should cleanup $_GET['email_address'] before saving it to the database.