I have a PHP script to receive a html/txt data and store it into a MySQL database. The PHP is like this and is in the following path XXX.XXX.XX.158/myphp.php
<?php
$text = $_POST['data'];
$con = mysqli_connect('localhost','root',''); //DB connection
mysqli_select_db($con,'db1'); //DB selection
$sql = "INSERT INTO pages (page) VALUES ($text)";
mysqli_query($con,$sql);
?>
On the other hand, I need a BASH-CGI in other local IP (XXX.XXX.XX.157) to send the html/txt file. However, curl is not working for me and I dont know how to do this in pure bash.
#!/bin/bash
dir='Location: ../http/index.html'
#POST($dir) send(data)??
Is there any command or method so I can do this?
Thank you in advance
Related
I am building a simple mobile app with Framework7 and Phonegap that submits form data to a database. I have created a form POST according to the F7 docs to a POST test server as follows:
<form action="https://ptsv2.com/t/c1i9p-1580217375/post" method="POST" class="form-ajax-submit">
I can see in the console that the form data looks correct and the test server is also receiving the data:
I am now trying to create a connection to phpMyAdmin running locally so that I can POST the data somewhere. My HTML has changed to:
<form action="/php/form-data.php" method="POST" class="form-ajax-submit">
And the php file is as follows:
<?php
$servername = "localhost";
$database = "test";
$username = "root";
$password = "";
//Create connection
$conn = mysqli_connect($servername, $username, $password, $database);
//Insert generic variables into table for testing
$sql = "INSERT INTO meeting (first_name, surname, email)
VALUES ('John', 'Doe', 'johndoe#gmail.com')";
mysqli_close($conn);
The POST now fails and I am getting the following error in the console:
POST http://myIP:55/php/form-data.php 404 (Not Found)
If I click on the above link the php file in question downloads to my PC, so I don't think it's actually an issue of not being able to find the file and rather something wrong with the php or database? It is my first time ever doing a POST request so please excuse any silly mistakes.
you need to install server for PHP e.g. Apache
I'm trying to export a csv file from a linux machine to a local PC via a PHP script on a browser.
I've done a good bit of research, but I can't find what I'm looking for (maybe it doesn't exist).
My script connects to the database via php and uses localhost as do my other php scripts that just display data from the database but don't export the data.
Here is my futile attempt:
$user = 'postgres';
$password = 'XXXXX';
$db = 'ltg';
$host = 'localhost';
$conn=pg_connect('dbname ='.$db.' user = '.$user.' password = '.$password.' host = '.$host) or die ("Could not connect: ");
//query for copying/exporting to csv
echo "system(\copy (select lat, lon from ltg_data order by time desc limit 1000) To '~/Downloads/export.csv' CSV HEADER);"
There are two issues, at least, that I'm confused about. The connection to the db works fine for accessing/display data from db. Do I need to use something other than "localhost" to export data?
Second, running the \copy command is problematic. I somehow need to run a psql meta-command to export the data, and I really don't know how to do that via php.
Thanks very much for your time.
The problem is that system runs a shell command (not sure why you're echoing it?), so it doesn't go through $conn at all. And of course there is no shell command called \copy.
If you want you can send a \copy ... to psql with something like system("echo '\copy ....' | psql"), but you'll have to be careful with escaping the parameters etc.
Since your database is on localhost and you're already connecting with the postgres user, you could just use COPY instead of \copy, and send the command via $conn.
Here is documentation about COPY.
im trying to write a query to insert data to my remote database table from a CRON file in my remote server. db connection happens through webservice and all that is working fine but the database does not get filled. following is the code that i have written, will anyone of you be able to find why it does not get write to the database table
$link = DbConnection::getInstance('TDC')->connectMysql()
$attributeKey = "12345";
$query_new = "INSERT INTO redtable (message) VALUES ('$attributeKey')";
$result = mysql_query($query_new, $link);
I am been trying to connect my android application with my 000Webhost account's MySql Database using PHP.
My Webhost server details are:
saravananoct14.web44.net
username: xxxx
HomeRoot: /home/xxxx
ServerName: server10.000webhost.com
IPAdress: "ipaddress"
MySQL database name:xxxx_sara
MySQL user name:xxxx_sarav
password for mysql:xxxx!4
my table name is ms.
column names:EMAIL_ID,Q1,Q2,Q3.
I have saved my .php files under public_html folder in phpmnMyAdmin.
config.php file is:
<?php
define ('DB_USER',"xxxx_sarav");
define('DB_PASSWORD',"xxxxx!4");
define('DB_DATABASE',"xxxx_sara");
define('DB_SERVER',"mysql1.000webhost.com");
?>
add.php file:
<?php
$res=array();
if(isset($_POST['EMAIL_ID']) && isset($_POST['Q1']) && isset($_POST['Q2']) && isset($_POST['Q3'])){
$EMAIL_ID=$_POST['EMAIL_ID'];
$Q1=$_POST['Q1'];
$Q2=$_POST['Q2'];
$Q3=$_POST['Q3'];
require_once __DIR__.'connect.php';
$db=new DB_CONNECT();
$result=mysql_query("INSERT into ms (EMAIL_ID,Q1,Q2,Q3) values ('$EMAIL_ID','$Q1','$Q2','$Q3')");
if($result){
$res["success"]=1;
$res["message"]="success";
echo json_encode($res);
}else{
}
}
else
{
$res["success"]=0;
$res["message"]="Required field(s) missing";
echo json_encode($res);
}
?>
I have used the same android program to connect it with my local wamp server and it worked.
I have given the following string as Url to parsed using JSON parser is:
http://"ip address"/add.php .
I am getting errors as Error Parsing the data. Cannot be converted into !Doctype...
I am pretty sure that I must make a change in the url. But could not find what changes I need to implement so that I can connect with the database.
Help me pls....
So after we discussed your Problem a long way I will write an Answer:
Please do not call your webservice via IP, always take the URL (especially when you have a webhoster)
Always check your PHP Script via a Browser on your PC, you will see the errors directly.
Debug the errors shown by your php script
SO, I'm trying to insert some registers to PostgreSQL table using PHP.
I have a career table and registers are into school_careers.sql. So I've tried this:
$host = "localhost";
$user = "user";
$pass = "passw0";
$db = "school";
$con = pg_connect("host=$host dbname=$db user=$user password=$pass")
or die ("Could not connect to server $server\n");
$query = "\\i ./school_classes.sql";
$rs = pg_query($con, $query) or die("Cannot execute query: $query\n");
echo $rs . " done\n";
pg_close($con);
But I just get:
Cannot execute query: \i ./school_classes.sql
You're getting an error because pg_query expects a literal query, not a file name.
This post discusses loading and running sql from a file in PHP: Loading .sql files from within PHP
The accepted answer by Jeremy Privett seems to recommend that you "build out a PHP file that contains your queries in a variable [from which you] can just run them".
This doesn't work because "\i" is a psql client program command, not a valid query. There's no way for this command as-is to work in PHP as a pg_query. If you want to do it this way, you can invoke a shell and run this command:
psql -c "\i ./school_classes.sql" -U <username> -h <hostname> <dbname>
This has a drawback in that I do not believe that you can simply pass the password to the psql client, it will prompt for one. You can set up a .pgpass file containing the username/password pair that it can then use to connect without prompting (this is detailed in the PostgreSQL documentation as "libpq-pgpass").
Also, I'm not a PHP person so I don't know how to run a shell command, but it's a common enough operation that it should be in the documentation. You'll also need to determine how to capture the output from the shell command in order to validate that your script can correctly.