Posting data to index,php file on a server ip - php

i am trying to capture some data from a IOT device. The problem isto capture the data you have to feed in the ip to the device so that data is [posted to that ip address.
To process the data,i came up with this script and aptly named it index.php
<?php
$servername = "94.049.947.776";
$username = "droid";
$password = "!#nord";
$dbname = "atree";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$data = $_POST;
$sql = "INSERT INTO gps (data)
VALUES ('$data')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
To test it out,i have this html page
<form method="post" action="972.245.119.017">
<input type="text" name="ed" value="jsonstring" />
<input type="submit" value="submit" />
</form>
However,no data is inserted to the database. What could be wrong with my script?.

$_POST is php variable in form of an Array, maybe try to :
replace :
$data = $_POST;
with :
$data = $_POST['ed']; // the value from the form
or some other value that you posted to the index.php like :
$data = $_POST['VALUE_NAME'];
consider working with PDO (http://php.net/manual/en/book.pdo.php) for the sql part

You should replace :
$data = $_POST;
by :
$data=$_POST['ed'];

Related

How to ask a user input and use it as a parameter for a stored procedure in a PHP script?

Sorry for the newbie question, but I have this task which kind of got me stuck.
So, I made a database in PhpMyAdmin, created a table with data : Products(id, name, city) and created a stored procedure that will actually do a query on the table to find out the product with a certain name (which will be input-ed by the user on the web page). My stored procedure is: proc_test and takes one VARCHAR paramter.
So, how can I do this in a php script? How can i ask the user for some data, (on the site he should have like a box to type it) then click a search button, and get redirected to the page with the query results. This is my code so far:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "practice";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "CALL proc_test('pencil');";
if ($result = mysqli_query($conn, $sql)) {
while ($row = mysqli_fetch_assoc($result)) {
echo $row['numec'] . "<br/>";
}
}
$conn->close();
?>
Here, of course, I manually give the parameter in the script. But I don't know how to change this and ask for a user input instead. Any help is welcome!
You can use ajax to send the data from your website to the php file where you can search for it in the database and send that data back to the user. In my example, the data is being displayed on the same webpage. You could echo the link to the website and redirect the user on the webpage using JavaScript if you really wanted to but my example is just a proof of concept.
index.html
<html>
<head>
<title>Test</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.4.1/jquery.min.js"></script>
</head>
<body>
<input required type="text" id="user_input">
<button onclick="sendData()">Search</button>
<p id="result"></p>
<script>
function sendData() {
var var_params = $('#user_input').val();
$.post('test.php', {params: var_params}, function(data) {
document.getElementById('result').innerHTML = "Your search result: " + data;
});
}
</script>
</body>
</html>
test.php
<?php
if(isset($_POST['params'])) {
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "practice";
$conn = new mysqli($servername, $username, $password, $dbname); // Create connection
if ($conn->connect_error) { // Check connection
die("Connection failed: " . $conn->connect_error);
}
$param = mysqli_real_escape_string($conn, $_POST['params']);
$sql = "CALL proc_test('$param');";
if($result = mysqli_query($conn, $sql)) {
while($row=mysqli_fetch_assoc($result)) {
echo $row['numec']. "<br/>";
}
}
$conn->close();
}
?>

Submitting HTML form to database using PHP and it brings back php page instead of submitting

I'm trying to connect HTML form to SQL database using PHP but when I hit submit, it is giving me PHP page.
This is HTML code
<form method="post" action="connect.php">
Username : <input type="text" name="username"><br><br>
Password : <input type="password" name="password"><br><br>
<input type="submit" value="Submit">
</form>
Here is PHP code
$username = filter_input(INPUT_POST, 'username');
$password = filter_input(INPUT_POST, 'password');
if (!empty($username)){
if (!empty($password)){
$host = "localhost";
$dbusername = "root";
$dbpassword = "";
$dbname = "youtube";
// Create connection
$conn = new mysqli ($host, $dbusername, $dbpassword, $dbname);
if (mysqli_connect_error()){
die('Connect Error ('. mysqli_connect_errno() .') '
. mysqli_connect_error());
}
else{
$sql = "INSERT INTO account (username, password)
values ('$username','$password')";
if ($conn->query($sql)){
echo "New record is inserted sucessfully";
}
else{
echo "Error: ". $sql ."
". $conn->error;
}
$conn->close();
}
}
else{
echo "Password should not be empty";
die();
}
}
else{
echo "Username should not be empty";
die();
}
I expect to get 'New record is inserted successfully' or 'error'
In php, once you done with the various processes in a page, you need to write a redirect otherwise it will just die waiting for the next action. At the moment, after its done executing, you it will just echo if successful and die() is failed as per your conditions.
Add:
if ($conn->query($sql)){
header("Location: /path_of_page.html");
}
The same can be applied in the }else (){} statements.
Edit: I forgot to mention, avoid using die and echo in intermediate (processing) pages since they will output on the raw php during executions. Rather, put them in an array and pass them to the UI as parameter.

php Data not stored in mySQL database using XAMPP

I have this code and I am using the latest version of XAMPP:
filename: store.html
<!DOCTYPE html>
<html>
<body>
<form action="store.php" method="post">
User input: <input type="text" name="userinput"><br>
<input type="submit">
</form>
</body>
</html>
filename: store.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "database";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully";
$input = $_POST["userinput"];
$sql = "INSERT INTO table_1 (s_num)
VALUES ('$input')";
?>
Whatever I do, no data is added to the database. Please help. Thank you.
The problem here is that you never executed the query.
$sql = mysqli_query($conn,"INSERT INTO table_1 (s_num)
VALUES ('$input')");
if(!sql){
echo "Error: " . mysqli_error($conn);
}
else{
echo "Success";
}
Reference:
http://php.net/manual/en/mysqli.query.php
Your present code is open to SQL injection if user-input (other than yourself) ever gets involved.
Use prepared statements, or PDO with prepared statements, they're much safer.
Plus, seeing you are running this from your own machine, make sure you are accessing as http://localhost/file.php as opposed to file:///file.php.
You are not executing your sql insertcode.
After this line:
$sql = "INSERT INTO table_1 (s_num)
VALUES ('$input')";
Add these line:
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();

Mysql I want user to upload there Url on My database

This is my form:
<form action="add.php" method="post" enctype="multipart/form-data">
<input type="url" name="url"><br>
<input type="submit" value="Upload">
</form>
Here is my add.php:
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "addimage";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "INSERT INTO addimage (url)
VALUES ('')";
if (mysqli_query($conn, $sql)) {
echo "New txt added successfully";
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
Now when I upload text, it shows
txt added successfully
BUT IN PHP MY ADMIN it shows no text in Url column.
I have created 2 columns in my data base Id and Url.
It shows id number(1,2,3....) but not inserted text in php my admin.
Now when I set the value in add.php as http:\
It shows http:\ in all the fields of Url
$sql = "INSERT INTO addimage (url) VALUES ('')";
You insert an empty string into your database...
Please add $_POST['url'], and sanitize it.

Connect my form to SQL

I was trying to connect a form to my but can't seem to make it work. Any help, please?
Here is the HTML
<form action="info.php" method="post">
<label for"email">Vaša e-mail adresa</label>
<input type="email" name="EMAIL" required>
<textarea name="recenzija" class="recenzija" rows="10" columns:"1" name="TEKST" required>Otkucajte ili kopirajte vaš tekst ovde (do oko 400 reči).</textarea>
<input type="submit" class="submit" name="submit" value="Pošalji">
</form
And the PHP
<?php
$servername = "mysql";
$username = "podkupol_filmski";
$password = "123";
$dbname = "podkupol_konkurs";
// Create connection
$conn = new mysqli($mysql, $podkupol_filmski, $123, $podkupol_konkurs);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO konkurs (EMAIL, TEKST)
VALUES ('EMAIL', 'TEKST')";
if ($conn->query($sql) === TRUE) {
echo "Hvala!";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
I keep getting this message:
Connection failed: Can't connect to local MySQL server through socket
'/var/lib/mysql/mysql.sock' (111)
This:
$conn = new mysqli($mysql, $podkupol_filmski, $123, $podkupol_konkurs);
should be either your variables or strings, you are setting variables which you don't use but pass unset variables in you connection string.
try it like this:
$conn = new mysqli($servername, $username, $password, $dbname);
SIDENOTE:
As #developerwjk has mentioned in his comment, 'mysql' should be changed to localhost, or the actual hosts ip address.

Categories