Jump to content

[SOLVED] Sending data in HTML form to database using PHP


jeeves245

Recommended Posts

Hi guys,

 

Hoping to get some ideas here...

 

Each week I have some data which needs to be sent to a MySQL database. The data is in a simple HTML form. The layout of the form stays the same from week to week but the data changes.

What would be the best way to go about sending it to the database each week using PHP?

 

I'm by no means a PHP expert but i'm learning pretty quickly.

 

Cheers.

Link to comment
Share on other sites

I'm not sure about what do you want to do.

 

Your intention is to read data from that table with PHP and sending it to a mysql db?

 

How does table data change ? (e.g. after a form submit, script execution...)

 

I don't think reading data from html is a good solution. Knowing how data changes could suggest a better way.

 

Temporizing script execution is possible if you could use Cron job,  otherwise you have to call the appropriate PHP script

Link to comment
Share on other sites

I'm not sure about what do you want to do.

 

Your intention is to read data from that table with PHP and sending it to a mysql db?

 

How does table data change ? (e.g. after a form submit, script execution...)

 

I don't think reading data from html is a good solution. Knowing how data changes could suggest a better way.

 

Temporizing script execution is possible if you could use Cron job,  otherwise you have to call the appropriate PHP script

 

The data changes when I am sent a new HTML document each week from a client.

 

So it's not dynamic.

 

I just need a way to easily put the data into a database. I figured using PHP to process it would be easy enough. Could be wrong?

Link to comment
Share on other sites

With xml everything could be easier.

 

Anyway, I think you should load the HTML page into PHP (using fopen and fread) and then you could use regular expressions to find data.

 

I can't post some code now because I didn't' use regular expression so much....so I don't remember them very well. I need to make some tests before posting examples to make them fullly functionally

Link to comment
Share on other sites

Hello there,

well, just went some 'googling' and found a little piece of code that could be interesting.

you could modify it so that it will fit your situation.

 

This code for now, reads your page with the table in it and does some processing. After doing so, it writes the data in a text file with a comma as separator for each new line.

 

This is for a simple table form only and is not recommended for sending documents of great value.


<?php
$remote_url = 'http://example.com/page.html';
$local_file = 'whatever.txt';

$ch = curl_init($remote_url);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$str = curl_exec($ch);
curl_close($ch);

$table_start = strpos($str, '<table>');
$table_end = strpos($str, '</table>') + strlen('</table>');
$table_len = $table_end - $table_start;
$table = substr($str, $table_start, $table_len);

$data = str_replace('</td>', ',', $table);
$data = str_replace(',</tr>', "\n", $data);
$data = strip_tags($data);

$fp = fopen($local_file, 'w');
fwrite($fp, $data);
fclose($fp);
?> 

 

Hope this helps.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.