post data to mysql DB from external source using a link - php

I have a web based, php, DB system using mysql. users login, enter data in forms, click submit, and data is entered and can be read, analyzed, reported on, etc etc etc.
now we have a data provider who wants to send data to the mysql DB via a link, an external submission to the DB from a source not logged in to the system
"The most common way of how we get data over to our clients is by posting over our data to your system. In order to do this I would need a URL to post the data to as well as the
required fields names."
I am not exactly sure what this means.
http://mysite.com/data.php?first_name=bob&last_name=smith // ??
how do i set up, in the above example, data.php, so the data provider can input via the link and not have to post into a form?
Typically, i have an html form like this, on a page we'll call form1.php:
<html><body>
<form method="POST" NAME="form1" action="data.php">
<input type='text' name='first_name'>
<input type='text' name='last_name'>
<input type=submit value="Submit">
</form>
</body></html>
data.php looks like this:
<?
include("dbinfo.inc.php"); // has the access info for the DB, how to connect
$first_name = mysql_real_escape_string($_POST['first_name']);
$last_name = mysql_real_escape_string($_POST['last_name']);
$query = "INSERT INTO table1 (first_name, last_name) VALUES ('$first_name','$last_name')";
mysql_query($query);
mysql_close();
?>
<html>
<head>
<meta HTTP-EQUIV="REFRESH" content="0; url=form1.php">
</head>
</html>
But in this example, the user would have to log in and post to the form. what is the way for the user to generate a link with the pertinent data, and have it submit via data.php not from a form, but directly from a link?
TIA

The GET method sends the data into the link. Your for should look like this:
<form method="GET" NAME="form1" action="data.php">
<input type='text' name='first_name'>
<input type='text' name='last_name'>
<input type=submit value="Submit">
</form>
This will send the browser, for example, to http://mysite.com/data.php?first_name=bob&last_name=smith
In PHP you can take these variables using $lastname=$_GET['last_name'];
So you have to change only the method you take the data from the form in your data.php:
<?
include("dbinfo.inc.php"); // has the access info for the DB, how to connect
$first_name = mysql_real_escape_string($_GET['first_name']);
$last_name = mysql_real_escape_string($_GET['last_name']);
$query = "INSERT INTO table1 (first_name, last_name) VALUES ('$first_name','$last_name')";
mysql_query($query);
mysql_close();
?>
<html>
<head>
<meta HTTP-EQUIV="REFRESH" content="0; url=form1.php">
</head>
</html>
Some help: http://www.w3schools.com/tags/ref_httpmethods.asp

You could learn about the webservices and REST to do it.
To persist given data, you should do it via a POST request instead of GET request (or PUT if it's an update).
The client will only use your URI and make a POST or PUT request to persist or update datas, and make a GET request to get datas and DELETE to remove datas.

Related

How to access php from html?

I am sorry if this is a repeating question, but I can't find anyone who might have the same question as me yet. I am new in html and php, and I am trying to learn more by practising, basically creating a registration form and keep all the data in the MySQL database, by using xampp. I have created a registration form and saved it as a .html file format, but how can I use php to send the user input from html to the MySQL database? I have seen a lot of examples on Google, but most of them are using php file format, for example instead of registration.html, they use registration.php. I really do appreciate your help. Thank you very much StackOverflow community members.
P/S: I use Visual Studio Code to do this
Step 1: write this code in your html file
in form action redirect it to the php file
<!DOCTYPE html>
<html>
<head>
<title>form</title>
</head>
<body>
<form method="post" action="st.php">
<input type="text" name="text">
<input type="submit" name="submit_btn">
</form>
</body>
</html>
Step 2 create st.php file and call the action
<?php
if(isset($_POST['submit_btn'])){
//your code
header('Location:yourHTMLFile.html?status=success');//redirect to your html with status
}
?>
An example to insert some data in to the MySQL database using PHP
1. Create a Index.php page in a new folder(“764”) created under public_html folder present in your home directory
To use a PHP script on your web page, you just need to end the file name with .php and make sure the permissions on the file are set correctly. Any files to be accessed by the web server must be publically readable, but none of your PHP files (nor the directory containing them) may be group or publically writable. Following are the commands you can use for setting the permissions in linux:
chmod 755 public_html
chmod 644 public_html/764/index.php
The PHP page will have two simple text boxes for the user to enter some data in to it. Label them to be Firstname and Lastname.
INDEX.PHP
<html>
<body>
<h1>A small example page to insert some data in to the MySQL database using PHP</h1>
<form action="insert.php" method="post">
Firstname: <input type="text" name="fname" /><br><br>
Lastname: <input type="text" name="lname" /><br><br>
<input type="submit" />
</form>
</body>
</html>
We also need to make sure that the form method attribute is “post” so as to access the data being entered in a reliable way in the next page being directed “insert.php” so that the data being entered in the textboxes can then be saved to the database in the “insert.php” page.
To connect to MySQL :-
Before you can access your MySQL database, you must contact the system administrators to request an account.
Once the administrators have notified you that your account has been created, you may connect using the following instructions.
Go to http://localhost/phpmyadmin and type your MySQL ID and password being given.
Now enter the new table name “nametable” , number of fields in that table as “2” and hit GO button.
Enter the field names to be “firstname” and “lastname” and keep the length attributes to be “20” for both the fields. The default type of VARCHAR is kept as it is.
After the table fields are being created, the following screen will be shown to you.
Now we need to make a connection to the MySQL database and then send this entered data from our textboxes. For that we create a new PHP page “insert.php” and use the following connection strings to the connection variable $con
After making a connection, a SQL query is being written to enter this data in to the MySQL database being created (“nametable”)
To tell the user that the data is being entered we set the echo to "1 record added"
INSERT.PHP
<html>
<body>
<?php
$con = mysql_connect("localhost","cis_id","password");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("cis_id", $con);
$sql="INSERT INTO nametable (fname, lname)
VALUES
('$_POST[fname]','$_POST[lname]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con)
?>
</body>
</html>
Now type the index page URL in your browser and enter some data in to the textboxes. Submit the Query.
For browsing the data, you need to click on Database: cis_id on top of the page (http://localhost/phpmyadmin) to get to the database tables page. Then click on the browse button as shown below, beside the “nametable” which you have already created to browse through the entered data.
the data being entered in to the MySQL database table being created.
Here is the reference.
http://people.cs.ksu.edu/~hankley/d764/tut06/GopisettyPHP.html
Note: Please write questions in proper way and internet is everything to get whatever you want. Hope you will search first and when you are helpless, post question with piece of code. Then only we can help in this forum.

How to have user input text to field, have text sent to database, and then displayed on a page?

I am creating a website for a small personal project. Basically I need a text field where the user puts a quote, a submit button which sends it to a data base, and then all the quotes in the database displayed below that. I cannot figure out how to do this.
I have tried to use Javascript to display a quote after it is submitted, however everything disappears upon reloading the page, and it is not saved to a database.
Try the following
first Set action form to php file (index.php).
<form method="Post" action="form.php">
<fieldset class="form-group">
<label for="label">Username</label>
<input type="text" class="form-control" name="username" >
</fieldset>
<fieldset class="form-group">
<label for="f">Lastname</label>
<input type="text" class="form-control" name="lastname" >
</fieldset>
<button type="button" name="submit" class="btn btn-primary">Submit</button>
form.php this will be second page
<?php
$server="localhost";
$username ="root";
$password ="";
$database ="you_database";
$connection =mysqli_connect($server ,$username ,$password,$database); //start connection
if ($connection==TRUE) // test connection
{
echo "connected succefull";
}
if (isset($_POST['submit'])) //test submit name in form
{
// declare parameter to initialized in text fild in form
// and those fild should appear in serve (apache mysql)
$username =$_POST['username'];
$lastname =$_POST['lastname'];
$query ="INSERT INTO your_table VALUES(NULL, '$username' ,'$lastname' ");
$result =mysqli_query($connection ,$query);
if ($result == TRUE) {
echo "data insert succefull";
}else{
echo "failed to insert";
}
}
mysqli_error($connection);
?>
You will need to use server side code to achieve this
Javascript is client side so data cannot be inserted to a database
These links should help you achieve what you need
https://www.w3schools.com/php/php_forms.asp
https://www.w3schools.com/php/php_mysql_insert.asp
https://www.w3schools.com/php/php_mysql_select.asp
You can do it on several ways:
Standard html form
Set action form to php file. If you click submit then page will be refeshed.
In php script you should veryfy if data are correct and sava data into database
or file
Redirect user back to page with form. Bellow form you should fetch and display
rows from database.
Using AJAX - without refreshing page
After click send button you can verify data in Javascript and send
them (via AJAX) to backend (php script)
In backend verify if data are ok and save them to db/file. Backend should output JSON results. Message info ans status.
In Javascript you should parse JSON result and make some actions like, show message to user.
Next you can fetch (via ajax) list of all (or eg. last 10) rows from table in database

Prevent browser form-resubmission alert

How can I avoid the the browser form-resubmission alert?
This question seems to have been discussed a lot here on SO, for example:
Preventing form resubmission
Prevent Back button from showing POST confirmation alert
Never ever respond with a body to a POST-request
What I do not get from the previous discussion, is how I can use the posted data and incorporate it into the html. The previous links discuss how to use the php header function to send a get request to itself. But when sending this get request, the posted data will no longer be available to the new page, (since we cannot use the php post method..)
I would like to do this without using the php or javascript session storage technique (or saving the posted data to a temporary mySQL database).
For a simple example:
<html>
<body>
<form action="post.php" method="post">
User name: <input type="text" name="user"><br>
<input type="submit" value="Submit">
</form>
</body>
</html>
where post.php is:
<html>
<body>
<?php
echo "<p>".$_POST['user']."</p>";
?>
</body>
</html>
Pressing CTRL-R in google chrome on the second page brings up the alert.
Do a redirect from post.php. Save data in session or in database and retrieve from redirect page.
Example Scenario:
Submit the form
Save the user record to db, get the id of the new record e.g. in $id
redirect using header, something like:
header('Location: result.php?user_id='.$id);
get the user record from db, with the provided id and show it to the
user.
Use this:
<script>
if(window.history.replaceState)
{
window.history.replaceState(null,null,window.location.href);
}
</script>
you may rewrite the browser history object
history.replaceState("", "", "/the/result/page");
See this

PHP is not passing my user entries

Hi I'm learning php of a tutorial and these are my two files. I browse to my apache2 server via http://myservers-ip/form2.php
fill out the forms and hit the submit button, it calls my result.php page, but all it displays is "Hi ." where it should be like "Hi (userentry)."
Please help :-(
form2.php:
<html>
<head>
<title>Form</title>
</head>
<body>
<h1>Enter your name</h1>
<form method="post" action="result.php">
<input type="text" name="username">
<input type="submit">
</form>
</body>
</html>
and my result.php
Hi <?php print $username; ?>.
Using apache2 and mysql running on my box.
I'm not sure if the source code is correct or if there might be a misconfiguration? if so which config files would you need?
Thanks
Data sent via a form with POST action will be in the superglobal $_POST array. You would want to sanitize it before trying to use it for anything, but just starting with $_POST['username'] will get you closer to your end goal.
Edit: Whatever tutorial you are using, abandon it. It's clearly waaaaay outdated.
You're sending data from form via post so you need to get them in your result.php file from POST superglobal like so:
Hi <?php print $_POST['username']; ?>.
The data is being sent to results.php via POST method.
All post params are stored in $_POST param. So to get user name you need to get it from $_POST, in results.php. E.g:
<?php
// file: results.php
if (isset($_POST['username']){
echo "Hi, {$_POST['username']}.";
} else {
echo "No user name."
}

Read input type hidden value

Is it possible to read the value right on load?
I suspect not.
<!DOCTYPE html>
<html>
<head>
<title>Some test</title>
</head>
<body>
<form method="POST">
<input type="hidden" name="token" value="<?php echo sha1("text")?>" >
</form>
<?php
echo $_POST['token'];
?>
</body>
</html>
Let's say I want to send some values generated by php like so:
<a href="url + page?options=1&token=sasadasdasda878asd7as8d7a">
Is there an option to get the token without passing it by URL (using get)?
$_POST will be populated with the data sent by the browser.
If you have a form in the page, then the data submitted by the form will only be sent by the browser when the browser submits the form, it won't be submitted when the page is initially loaded since:
The page will probably be loaded via a GET request, not a POST request
The browser won't have the data in the form until after it receives the page, so it can't submit that data in the request for the page.
You can also use $_SESSION, since it's stored on server-side.

Categories