How do you post http form data to MySql database?

General questions relating to Joomla! 3.x. Please consider the availability of third-party templates and extensions when deciding which Joomla version to use.

Moderator: General Support Moderators

Forum rules
Forum Rules
Absolute Beginner's Guide to Joomla! <-- please read before posting.
Forum Post Assistant - If you are serious about wanting help, you should use this tool to help you post.
Post Reply
Noob_Nb1
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Thu Aug 16, 2018 4:46 pm

How do you post http form data to MySql database?

Post by Noob_Nb1 » Thu Aug 16, 2018 5:20 pm

Hi,

I want to be able to insert values in my MySql databse. The values will be posted via the http POST method to a form in Joomla. I have inserted the code in an article in Joomla but it won't accept my php code:

<?php
// The global $_POST variable allows you to access the data sent with the POST method by name

$ID = htmlspecialchars($_POST['ID']);
$lat = htmlspecialchars($_POST['lat']);
$lon = htmlspecialchars($_POST['lon']);
$dato = htmlspecialchars($_POST['dato']);
$kl = htmlspecialchars($_POST['kl']);

// I have also tried with (MySQLi and PDO)!
$conn = mysqli_connect("1.111.111.11:3306", "user", "password","Database");

if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
echo "Connected successfully";

$sql = "INSERT INTO Tabel (Column1, Column2)
VALUES ('ID', 'lat')";

if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}

$conn->close();
?>

How can this work?

User avatar
Webdongle
Joomla! Master
Joomla! Master
Posts: 35197
Joined: Sat Apr 05, 2008 9:58 pm

Re: How do you post http form data to MySql database?

Post by Webdongle » Thu Aug 16, 2018 5:54 pm

You will most likely need to create a component to handle that.
http://www.weblinksonline.co.uk/
https://www.weblinksonline.co.uk/updating-joomla.html
"The definition of insanity is doing the same thing over and over again, but expecting different results": Albert Einstein

Noob_Nb1
Joomla! Fledgling
Joomla! Fledgling
Posts: 2
Joined: Thu Aug 16, 2018 4:46 pm

Re: How do you post http form data to MySql database?

Post by Noob_Nb1 » Thu Aug 16, 2018 6:44 pm

Damm it, I was hoping for a more, like this... answer.

User avatar
Webdongle
Joomla! Master
Joomla! Master
Posts: 35197
Joined: Sat Apr 05, 2008 9:58 pm

Re: How do you post http form data to MySql database?

Post by Webdongle » Thu Aug 16, 2018 7:14 pm

To create a form that displays in the component area and put/get to/from the database you will need to create a component. A component is created like this https://docs.joomla.org/J3.x:Developing ... _Component . Or use an extension to create a form https://extensions.joomla.org/category/ ... ack/forms/
http://www.weblinksonline.co.uk/
https://www.weblinksonline.co.uk/updating-joomla.html
"The definition of insanity is doing the same thing over and over again, but expecting different results": Albert Einstein

annahersh
Joomla! Enthusiast
Joomla! Enthusiast
Posts: 135
Joined: Wed Aug 15, 2018 8:23 pm

Re: How do you post http form data to MySql database?

Post by annahersh » Thu Aug 16, 2018 9:57 pm

You won't be able to simply place PHP coding in an article and parse it. You'll need an extension to process that. See https://extensions.joomla.org/extension/sourcerer/

Don't use PHP globals to process anything. Joomla has sanitized methods.

To connect to Joomla DB

Code: Select all

JFactory::getDbo();
To get the post data

Code: Select all

JFactory::getApplication()->input->post->get('ID');
To send to DB

Code: Select all

JFactory::getDbo()->execute();
of course there's a bit more for this

So your complete code (assuming you still want to place it in an article, using the plugin mentioned above). Note: I have not tested this. It's merely my conception of what you desire.

Code: Select all

$db = JFactory::getDbo();
$post = JFactory::getApplication()->input->post;

$ID = $post->get('ID');
$lat = $post->get('lat');

$columns = ['col1', 'col2'];
$values = [$db->quote($ID), $db->quote($lat)];

$db->setQuery(
	$db->getQuery(true)->clear()
	->insert( $db->quoteName('#__Tabel') )
	->columns( $db->quoteName($columns) )
	->values( implode(',', $values) )
	);

if( !is_null($post->get('senddata')) ) {
try{
$db->execute();
}
catch(Exception $e) {
	echo JText::sprintf('JLIB_DATABASE_ERROR_FUNCTION_FAILED', $e->getCode(), $e->getMessage());
	return;
}
}
HTML

Code: Select all

<form action="" method="post">
<input type="text" name="ID" value="" />
<input type="text" name="lat" value="" />
<input type="submit" name="senddata" value="Submit" />
</form>
See more about Joomla database methods at the following
https://docs.joomla.org/Inserting,_Upda ... _JDatabase
https://docs.joomla.org/Selecting_data_using_JDatabase
https://docs.joomla.org/Selecting_data_ ... ple_Tables

SharkyKZ
Joomla! Ace
Joomla! Ace
Posts: 1123
Joined: Fri Jul 05, 2013 10:35 am
Location: Unknown

Re: How do you post http form data to MySql database?

Post by SharkyKZ » Fri Aug 17, 2018 6:22 am

You can use a tool like https://ibrini.com/en/ or http://component-creator.com/ to easily create custom extension.


Post Reply

Return to “General Questions/New to Joomla! 3.x”