How to update database on wamp server using arduino ?

i have created a database on wamp server and i can insert data into this database using this php code (find in the attachment) how can i insert data into this database using arduino code ?? if anyone can help me with sketch or some library or anything i will be thankfull
note: i have arduino mega and ethernet ENC25J60

thanks alot :slight_smile:

index1.php (666 Bytes)

how can i insert data into this database using arduino code

You can't. You have to have your client make a GET request to the PHP script. It makes the changes to the database.

OK how to write this get request ?

OK how to write this get request ?

Like any other GET request. What have you tried?

am using web client example on the library of ethernet ENC28J60 ?

#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 192, 168, 42, 42 };
byte server[] = { 192, 168, 42, 1 };

Client client(server, 80);

void setup() {
Ethernet.begin(mac, ip);
Serial.begin(9600);

delay(1000);
Serial.println("connecting...");

if (client.connect()) {
Serial.println("connected");
client.println("GET /search?q=arduino HTTP/1.0");
client.println();
}
else {
Serial.println("connection failed");
}
}

void loop() {
if (client.available()) {
char c = client.read();
Serial.print(c);
}

if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
for(;:wink:
;
}
}

i don't think you're using the right lib, Ethernet.h is used on the Wiznet shields, you should use something like this lib, although i don't think there's a "client mode" for the ENC28J60.

the ethernet shield ENC28J60 has its own library an i have downloaded it and from its examples WebClient example so it seems to be it can act like a client.
my question is how to write get request in the sketch for accessing PHP code that control a database created on wamp server ?

i need some examples please

thanks in advance

my question is how to write get request in the sketch

Well, first you have to HAVE a sketch. Second, you need to quit being so greedy, and share that sketch. Then, we might be able to help you.

Not sure if these might help:

http://forum.arduino.cc/index.php?topic=173240.0

https://launchpad.net/mysql-arduino

Well, first you have to HAVE a sketch. Second, you need to quit being so greedy, and share that sketch. Then, we might be able to help you.

the sketch is in the third comment and if you are seeing me greedy don't reply on my questions.

Not sure if these might help:

Need Help: Retrieve data from IR Sensor and Update MySQL Database (WAMP) - Project Guidance - Arduino Forum

MySQL Connector/Arduino in Launchpad

Thanks alot EdwardE

    client.println("GET /search?q=arduino HTTP/1.0");

You can't see how to change this to call YOUR script, instead?

Quit being so thin skinned.

No one can tell you how to change that call to call your script, since you haven't told us where the script lives on you server (wherever it is replaces the / in that GET statement) or what the script is called (whatever it is replaces search in that GET statement) or what arguments it expects (whatever they are replaces the q=arduino in that GET statement).

ok Paul thanks for ur reply :slight_smile:

my php file located in c/wamp/www/test/php_db.php

and it is a localhost server
sorry i am newbie in these project and don't have sufficient information
thanks in advance

and the argument is temp1=25 and in this case i have to change the php script so what i have to write in the script ?

sorry for bad english and inconvenience :slight_smile:

my php file located in c/wamp/www/test/php_db.php

The real question is how you access it from a browser. That is, where is the httpd daemon on the server going to look for the script, relative to some location. Most likely, it will look in the C:\wamp\www directory for named files, so the GET request would be:

"GET /test/php_db.php?someName=someValue HTTP/1.0"

and it is a localhost server

No. What pointing a browser to localhost means is that the IP address is 127.0.01 (that is, the computer the browser is running on). No other computer can access that server using the localhost name, because localhost means me. If I point my browser at localhost/test/php_db.php, the browser is going to look for php_db.php on MY computer.

and the argument is temp1=25 and in this case i have to change the php script so what i have to write in the script ?

That's what goes in place of q=arduino in the sketch you posted, or someName=someValue in the GET statement I showed above.

sorry for bad english and inconvenience

No problem. We all have to start somewhere.

Thanks a Lot PaulS

what about this php script

<?php //Connect to database $con = mysql_connect("localhost", "root", ""); if(!$con) die('Could not connect: ' .mysql_error()); mysql_select_db("smart", $con); mysql_query("INSERT INTO counter(counting) VALUES (test1)"); mysql_close($con); ?>

it doesn't work with this script any suggestions please ?

Thanks in advance :slight_smile:

connecting...
connection failed

disconnecting.

and i have this in serial monitor :frowning:

$link = mysql_connect('localhost', 'mysql_user', 'mysql_password');

The password.

@Paul Sorry, I am careless not notice the question is for you. Anyhow you could take weekend break. :stuck_out_tongue:

mysql_query("INSERT INTO counter(counting) VALUES (test1)");

This query inserts hardcoded values to the database. To insert data passed to the script, you need to access the data using the $_GET variable (which contains name=value pairs) to get the value corresponding to the name you used in the GET request (temp1 in the example we were discussing earlier).

This has really moved beyond an Arduino issue, to a PHP, MySQL, client/server issue, and really does not belong in this forum anymore.

and i have this in serial monitor

Well, that's a different issue. The server that you are trying to access, 192, 168, 42, 1, doesn't look right. Typically, on a home network (which the 192.168 part implies), the third field is either 0 or 1. The last field is the specific device. Typically, 1 is the router. Device addresses start from 2 and go up.

thanks Paul

this IP when i write this ip 192.168.1.2 in the web browser URL it direct to my local host so it seems to be the IP for the wamp server and i put it into sketch and the serial monitor gave me the same result

and here you are the sketch an php file

#include <Ethernet.h>

byte mac[] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xFE, 0xED };
byte ip[] = { 169, 254, 209, 175 }; //for ethernt device
byte server[] = { 192, 168, 1, 2 }; // for wamp server

Client client(server, 80);

void setup() {
Ethernet.begin(mac, ip);
Serial.begin(9600);

delay(1000);
Serial.println("connecting...");

if (client.connect()) {
Serial.println("connected");
client.println("GET /php_db.php?test1=234 HTTP/1.0");
client.println();
}
else {
Serial.println("connection failed");
}
}

void loop() {
if (client.available()) {
char c = client.read();
Serial.print(c);
}

if (!client.connected()) {
Serial.println();
Serial.println("disconnecting.");
client.stop();
for(;:wink:
;
}
}

PHP Script

<?php //Connect to database $con = mysql_connect("localhost", "root", ""); if(!$con) die('Could not connect: ' .mysql_error()); mysql_select_db("smart", $con); mysql_query("INSERT INTO counter(counting) VALUES (test1)"); mysql_close($con); ?>

i don't know where is the problem ? :frowning: