"add.htx"
<?php
$servername = "my_server_name";
$username = "my_username";
$password = "my_pwd";
$dbname = "my_db_name";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO Drama_Gr (Date, Time, Cont, Temp, Hum, Pres, Rain_Y, Rain_M, Rain, Rain_Rate, Wind, Wind_Dir)
VALUES ('<!--date-->', '<!--time-->', '<!--ForecastStr--> frc', '<!--outsideTemp-->', '<!--outsideHumidity-->', '<!--barometer-->', '<!--totalRain-->', '<!--monthlyRain-->', '<!--dailyRain-->', '<!--rainRate-->', '<!--windSpeed-->', '<!--windDirection-->bfr')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
run this file with weatherlink
and output the result file
"add.php"
<?php
$servername = "my_server_name";
$username = "my_username";
$password = "my_pwd";
$dbname = "my_db_name";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO Drama_Gr (Date, Time, Cont, Temp, Hum, Pres, Rain_Y, Rain_M, Rain, Rain_Rate, Wind, Wind_Dir)
VALUES ('17/08/16', ' 0:11', ' Increasing clouds with little temperature change. frc', '24.6', '52', '1012.6', '291.2', '11.2', '0.0', '0.0', '1.6', 'SSEbfr')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
its ok with this way i create a database with my weather station
the problem is i want to give this code to run with weather station and other people from my area and create in to my database tables from other weather station but i don't want to include this code my database connection.
i think the correct way is to create a file like
"connect.php"
<?php
$servername = "my_server_name";
$username = "my_username";
$password = "my_pwd";
$dbname = "my_db_name";
mysql_connect($hostname, $username, $password) OR DIE("Unable to connect to database! Please try again later.");
mysql_select_db($dbname);
?>
and call this file like
<?php include("connect.php");?>
i try to add this code in "add.htx"
but get bellow error in "add.php"
Connection failed: Access denied for user ''#'localhost' (using password: NO)
why? maybe i think because the file "add.htx" start from weatherlink by local C:\WeatherLink\ but the weatherlink upload this file and convert to "add.php" at server at folder like http://my_website/weather/ ,this folder also include and the file "connect.php"
please any help
and if is easy give me the correct code at "add.htx" with the wright way to add at "add.htx"
<?php include("connect.php");?>
thanks
Was this what you did?
<?php
Include('connect.php');
$servername = "my_server_name";
$username = "my_username";
$password = "my_pwd";
$dbname = "my_db_name";
......
?>
Php is not running your include('connect. Php') file.
Bt then, I think the problem is not with the code. I think the problem is with your privileges. Compare ur server username and password with the ones you defined in your script.
// Connection failed: Access denied for user ''#'localhost' (using password: NO)// pls do check ur privileges
for other people i give the solution
first thanks to Script47 (open my eyes and see mysql_* must change to mysqli_*)
second i create i think the better way to call
"config.php"
here the fresh change at code "add.htx"
<?php
include("config.php");
include("opendb.php");
$sql = "INSERT INTO Drama_Gr (Date, Time, Cont, Temp, Hum, Pres, Rain_Y, Rain_M, Rain, Rain_Rate, Wind, Wind_Dir)
VALUES ('<!--date-->', '<!--time-->', '<!--ForecastStr--> frc', '<!--outsideTemp-->', '<!--outsideHumidity-->', '<!--barometer-->', '<!--totalRain-->', '<!--monthlyRain-->', '<!--dailyRain-->', '<!--rainRate-->', '<!--windSpeed-->', '<!--windDirection-->bfr')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
include("closedb.php");
?>
i create 3 files
to connect with db and close the db
"config.php"
<?php
$servername = "my_host_name";
$username = "my_username";
$password = "my_pwd";
$dbname = "_my_database_name";
?>
after i create a script
"opendb.php"
<?php
// Create my Weather db connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check my Weather db connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
?>
and i think is correct to create and script to close a connection when upload data to db ,name this script
"closedb.php"
<?php
// close my weather db connection
mysqli_close($conn);
?>
and finally the result to
"add.php"
New record created successfully
thanks and i hope this usefull and for others
now the last problem is with this code "add.php" the records update the db rows when refresh this page
i create a below script to auto refresh multiple files from weather stations in one page but must this page is open to auto update the new result to db and name this page
"send_db.html"
<html>
<head>
<!-- For ease i'm just using a JQuery version hosted by JQuery- you can download any version and link to it locally -->
<script src="http://code.jquery.com/jquery-latest.js"></script>
<script>
$(document).ready(function() {
$("#responsecontainer").load("http://www.dramaweather.gr/weather2/uploadwl/mysql/add.php");
var refreshId = setInterval(function() {
$("#responsecontainer").load('http://www.dramaweather.gr/weather2/uploadwl/mysql/add.php?randval='+ Math.random());
}, 900000);
$.ajaxSetup({ cache: false });
});
</script>
<script>
$(document).ready(function() {
$("#responsecontainer").load("http://www.dramaweather.gr/weather2/uploadwl/mysql/add_Serres.php");
var refreshId = setInterval(function() {
$("#responsecontainer").load('http://www.dramaweather.gr/weather2/uploadwl/mysql/add_serres.php?randval='+ Math.random());
}, 900000);
$.ajaxSetup({ cache: false });
});
</script>
</head>
<body>
<div id="responsecontainer">
</div>
</body>
</html>
is any other way to send the results of "'add.htx" to db auto every 10 minutes without a code from "send_db.html"
;
thanks again for the first problem if anyone know and my for my second problem the solution please help
Related
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();
}
?>
on line 1 of my page i required the connection file before going on to write my code. at this point i think it is supposed to work but can not figure out why it didn't. please some one who knows better help me.
Can you help me with a sample PHP code for inserting data into mysql database using PHP, mysqli?
Thank you again in advance.
You can use this code for insert data to mysql DB using php and mysqli
<?php
$servername = "localhost";
$username = "username";
$password = "password";
$dbname = "myDB";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO MyGuests (firstname, lastname, email)
VALUES ('John', 'Doe', 'john#example.com')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
I use the hosting company aPlus.net, and I can't seem to get past a connection error I'm getting when trying to process some php to write database content to a webpage, and I am curious as to if this is because my database appears to not be on the same server as the entire rest of my hosting account, and if there is a way to resolve this in my code? This is my first attempt at writing PHP, and it would be good to know if my code is wrong, or if my hosting company is messing me up. (and either way, how to fix it)
Here's the code that's failing to pull from the database:
{
$con = mysql_connect("localhost","2p5dq9vxmy240651","MY_PASSWORD");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("felineasthma_2p5dq9vxmy240651", $con);
$users_name = $_POST['name'];
$users_comment = $_POST['requests'];
$users_name = mysql_real_escape_string($users_name);
$users_comment = mysql_real_escape_string($users_comment);
$inputid = $_GET['id'];
$query = "
INSERT INTO `felineasthma_2p5dq9vxmy240651`.`submissions` (`id`,
`name`, `requests`, `inputid`) VALUES (NULL,
'$users_name', '$users_comment', '$inputid');";
mysql_query($query);
echo "<h2>Your request has been processed, reload page.</h2>";
mysql_close($con);
}
and here's some screen captures from inside my hosting account (links because I don't have enough posts here yet to upload images, sorry):
felineasthma_2p5dq9vxmy240651 doesn't appear in my hosting account
yet it clearly exists in MySQL Manager, but on a different server
I was even more confused while making the user for this database, as the control panel didn't allow me to make a username, it just randomly assigned one. Help? Advice?
I found a more modern tutorial to learn PHP with, and now everything works, I just need to add security measures now. Here's the working code snippets, if anyone ever comes here asking the same questions.
here's the form action that places the entries into the database:
<?php
$servername = "sql5c40n.carrierzone.com";
$username = "my_username";
$password = "my_password";
$dbname = "my_database";
$users_name = $_POST['name'];
$users_request = $_POST['requests'];
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "INSERT INTO submissions (name, requests)
VALUES ('$users_name', '$users_request')";
if (mysqli_query($conn, $sql)) {
header("Location: clv.php");
} else {
echo "Error: " . $sql . "<br>" . mysqli_error($conn);
}
mysqli_close($conn);
?>
here's the include that puts the database entries onto the page:
<?php
$servername = "sql5c40n.carrierzone.com";
$username = "my_username";
$password = "my_password";
$dbname = "my_database";
// Create connection
$conn = mysqli_connect($servername, $username, $password, $dbname);
// Check connection
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "SELECT id, requests, name FROM submissions";
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while($row = mysqli_fetch_assoc($result)) {
echo "" . $row["requests"]. " - by " . $row["name"]. "<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
?>
So, i got this code:
<?php
$servername = "localhost";
$username = "**";
$password = "**";
$dbname = "TestDB";
// Create connection
$conn = new mysqli($servername, $username, $password,$dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo "Connected successfully<br>";
$ip = $_SERVER['REMOTE_ADDR'];
$sql = "INSERT INTO testdata (id,address,count) VALUES (DEFAULT,'$ip',1) ON DUPLICATE KEY UPDATE count=count+1";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully<br>";
} else {
echo "Error: " ;
}
$conn->close();
?>
With this code ip-addresses of customers are saved and it should increase count by 1 every time they return to the website. When i visit database.php directly it works like it should in every browser, but when i call the php-file via the index.html-page it counts up twice (most of the times, not always) in Mozilla, other browsers no problem, the code in html-file:
<img style="display: none;" src="http://servername/database.php?">
Anyone know why? Please help, i'm really stuck here
I just made my own wamp server on my computer to handle twitch donations for a friend. The friend has a program that I made using c# that connects to the my server and reads the database. This works fine, the program reads my database just fine and lists the entries. BUT I have made a php website to send the entry (a new donation) to the database but I receive a connection timed out error just seconds after I press the button... my mysql server is set to allow every user and host with the correct password. I used the same IP user and password as on the c# program!
I can't figure out why the damn php website hosted on one.com can't connect!!
this is my code:
<?php
if (!empty($_GET['act'])) {
$servername = "81.83.95.34";
$username = "root";
$password = "mypassword";
$dbname = "tbc";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("server failed: " . $conn->connect_error);
}
$sql = "INSERT INTO donations (donations)
VALUES ('1')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
} else {
?>
can anyone help me?
thank you very much!