Post to MySQL without the use of a form

I have a C# program that needs to pass specific values to our remote server nightly.

I already have a database “counter” with just a few tables: ClientID, ClientName, BoxesMoved, and ReportDate.

I need to pass the values through a simple post command WITHOUT THE USE OF A FORM, but things aren’t working properly. Here’s what I was starting with.

I was trying to pass the values like this: ( test.php?clientid=01?clientname=theclient?boxesmoved=185443?reportdate=07-17-2015). Is this possible, and where can I find any information to aid me in my process.

[php]<?php
$host=“localhost”; // Host name
$username=“root”; // Mysql username
$password=“password”; // Mysql password
$db_name=“counter”; // Database name
$tbl_name=“boxcounter”; // Table name

// Connect to server and select database.
mysql_connect("$host", “$username”, “$password”)or die(“cannot connect”);
mysql_select_db("$db_name")or die(“cannot select DB”);

// Get values from form
$clientid=$_POST[‘ClientID’];
$clientname=$_POST[‘ClientName’];
$boxesmoved=$_POST[‘BoxesMoved’];
$reportdate=$_POST[‘ReportDate’];

// Insert data into mysql
$sql=“INSERT INTO $tbl_name(reportdate, clientid, boxesmoved, clientname)VALUES(’$ClientID’, ‘$ClientName’, ‘$BoxesMoved’, ‘$ReportDate’, )”;
$result=mysql_query($sql);

// if successfully insert data into database, displays message “Successful”.
if($result){
echo “Successful”;
}

else {
echo “ERROR”;
}
?>

<?php // close connection mysql_close(); ?>[/php]

If you want to use something like the url you showed then change the $_POST to $_GET. You’ll need to make sure you match the spelling and case structure of your vars to the $_GET vars. The example url you showed was all in lowercase but in the $_POST vars you have uppercase letters, they won’t match, so they won’t work.

I changed what you said, but it’s still not working correctly. Here’s my call: test.php?clientid=01?clientname=TheGap?boxesmoved=185443?reportdate=07-17-2015

Any Idea why?

[php]

<?php $host="localhost"; $username="root"; $password="password"; $db_name="counter"; $tbl_name="boxcounter"; mysql_connect("$host", "$username", "$password")or die("cannot connect"); mysql_select_db("$db_name")or die("cannot select DB"); $clientid=$_GET['clientid']; $clientname=$_GET['clientname']; $boxesmoved=$_GET['boxesmoved']; $reportdate=$_GET['reportdate']; $sql="INSERT INTO $tbl_name(clientid, clientname, boxesmoved, reportdate )VALUES('$clientid', '$clientname', '$boxesmoved', '$reportdate', )"; $result=mysql_query($sql); if($result){ echo "Successful"; } else { echo "ERROR"; } ?> <?php // close connection mysql_close(); ?>

[/php]

Sorry I didn’t notice the error in your url, you only need the first ? and the rest should be & like this
test.php?clientid=01&clientname=TheGap&boxesmoved=185443&reportdate=07-17-2015

FASTOL

You Rock. What I noticed was I had an extra comma after my last DB insert so it kept failing, and I have debugging off, so I got nothing.

You helped me immensely :slight_smile: , and thank you!

Sponsor our Newsletter | Privacy Policy | Terms of Service