I am posting certain values from front end (android) to back end (php) and this webservice code works well in server one, but for some unknown reason its not working in server 2.
What I found is mysql_query is not working and data is not inputing to the table.
$result = mysql_query("INSERT INTO tbl_booking(booking_name,booking_address,booking_date,booking_phno,description)
Here is the full code
<?php
$con = $con = mysql_connect("localhost","touchlive","touchlive");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("raora", $con);
$response ="FALSE";
$booking_name = $_POST['name'];
$booking_address =$_POST['address'];
$booking_date = $_POST['date'];
$booking_phno =$_POST['phno'];
$description =$_POST['description'];
$result = mysql_query("INSERT INTO tbl_booking(booking_name,booking_address,booking_date,booking_phno,description)
VALUES ('$booking_name','$booking_address','$booking_date', '$booking_phno', '$description')");
if(result ==0)
$response ="TRUE";
echo $response;
mysql_close($con);
?>
your code has
if(result == 0)
where is ' $ '
if($result == 0) like this
and give ur db connection like this
$host="your host";
$port=1234;
$socket="/tmp/mysql.sock";
$user="root";
$password="123456789";
$dbname="dbname";
$con = mysqli_connect($host, $user, $password, $dbname, $port, $socket);
change all above variables as per your details
socket can be empty .
Related
I am creating a messaging website, which uses a MySQL database to store the message data. However, when there are only 5 users online at the same time, it starts failing with "MySQLi: Cannot Connect: Too many MySQL connections".
I have heard that my host, HelioHost, limits the maximum MySQL connections to four, which explains my error (see this for details).
I want to know how I can change my scripts (which currently connect when the message form is submitted, add it to the database, then disconnect) can only use a maximum of four connections over the server.
Here's some code:
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("MySQL server connection failed: " . $conn->connect_error);
}
//Process data
if ($_SERVER["REQUEST_METHOD"] == "POST") {
$name = $_SESSION["username"];
$parent = $_POST["thread"];
$content = test_content($_POST["content"]);
$sql = "INSERT INTO `threads_replies`(`Parent`, `Author`, `Content`) VALUES ('" . $parent . "','" . $name . "','" . $content . "')";
$conn->query($sql) or die ("Failed MySQL query to save reply");
echo "Reply saved. <span class=\"fa fa-chevron-circle-left\"></span> Back to Thread";
}
function test_input($data) {
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
function test_content($data) {
$data = str_replace("'", "'", $data);
return $data;
}
Any ideas?
Right from the docs:
http://php.net/manual/en/function.mysqli-connect.php
$link = mysqli_connect("127.0.0.1", "my_user", "my_password", "my_db");
if (!$link) {
$error = mysqli_connect_error();
echo "Failed to connect to MySQL: $error";
echo 'Sorry, pal, 5 already playing around...';
exit;// or location('wait.php');
}
# ....
mysqli_close($link);
For the 5th user - he should reload the page, you can redirect him to something like wait.php:
<!-- htm, head, please add.. -->
<?php
// no need in PHP, actually
?>
<h3>Sorry, having too much users, will reload automatically...</h3>
<script>
setTimeout(
()=> location.href =
location.href + '?t=' + (new Date()).getTime(),
5000 // hope 5 seconds will be enough
);
So your code:
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
location('wait.php');
die("MySQL server connection failed: " . $conn->connect_error);
}
You can build a cache system which will be checked before accessing the database (and build up a connection). As an example, you don't need to read the news on the front page every time from the database, they don't change that often. So you read the news from the cache system (like some file in the file system) instead and display the data from it. And when the news entries has been changed you delete/invalidate the cache so the cache gets rebuild (once) on the next visit again.
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);
?>
I have been developing a CRUD application using PHP & MySQL database.
I was succeeded by creating, displaying, updation parts. But I stuck at the deletion part of a row from a database table.
I tried my best solving all the PHP shown errors but now in final it is now showing a message which I wrote to echo in case of failure.
I request someone to please help me with this problem.
Thankyou in advance.
Code I wrote for deletion:
//include database connection
include 'db_connect.php';
//$mysqli->real_escape_string() function helps us prevent attacks such as SQL injection
$query = "DELETE
FROM `grocery`
WHERE `GrocerID` ='".$mysqli->real_escape_string($_GET['id'])."'
limit 0,1";
//execute query
if( $mysqli->query($query) ){
//if successful deletion
echo "User was deleted.";
}else{
//if there's a database problem
echo "Database Error: Unable to delete record.";
}
$mysqli->close();
?>
Code I wrote for delete link in display table:
//just preparing the delete link to delete the record
echo "<a href='delete.php?id={$GrocerID}'>Delete</a>";
Code I wrote for db config:
<?php
//set connection variables
$host = "localhost";
$username = "root";
$password = "secret";
$db_name = "crud"; //database name
//connect to mysql server
$mysqli = new mysqli($host, $username, $password, $db_name);
//check if any connection error was encountered
if(mysqli_connect_errno()) {
die("Connection failed: " . $conn->connect_error);
exit;
}
?>
I tried this and got working, can you update the code and see if this works?
$host = "localhost";
$username = "root";
$password = "secret";
$db_name = "crud"; //database name
//connect to mysql server
$mysqli = new mysqli($host, $username, $password, $db_name);
//check if any connection error was encountered
if(mysqli_connect_errno()) {
die("Connection failed: " . $conn->connect_error);
exit;
}
// Delete row
if ($mysqli->query (sprintf( "DELETE FROM grocery WHERE email = '".$mysqli->real_escape_string($_GET['id'])."' LIMIT 1") )) {
printf ( "Affected Rows %d rows.\n", $mysqli->affected_rows );
}
I hope this helps.
Provide a connection :
if( $mysqli->query($con, $query) ){
Based on suggestions and a better understanding of what I need I have changed my question:
My site is protected with .htaccess. Im trying to get the current user and a few other variables and save them to a MySQL database. Everything works except the current user part.
Heres my code:
<?php
$_SERVER["PHP_AUTH_USER"] = "rep";
$hostname = "hostname";
$username = "username";
$dbname = "dbname";
$password = "password";
$usertable = "usageStats";
$yourfield = "your_field";
$con = mysql_connect($hostname, $username, $password);
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db($dbname, $con);
$sql="INSERT INTO usageStats (Bid, PRid, User)
VALUES
('$_POST[BID]','$_POST[branding]','$_POST[rep]')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con);
?>
I know that the below are both wrong. How should the current user be passed instead of $_POST[rep]?
Wong:
$_SERVER["PHP_AUTH_USER"] = "rep";
and
'$_POST[rep]'
If you are using Basic Authentication you do not need to send the username explicitly. The browser will send it and you can access it using the PHP_AUTH_USER server variable:
$_SERVER['PHP_AUTH_USER']
Also checkout the HTTP Authentication article in the documentation.
I am a PHP newbie and have been trying for sometime now to connect to MySQL database using PHP so I can insert data into a table I have created but I am unable to do this.
I suspect the problem is coming from my PHP .ini file,but that's just me.
Would be grateful if anyone can help me configure my PHP .ini file so I can connect to MySQL and insert data into my table. Here is my PHP script in case you are wondering.
Any help will be gratefully appreciated.
<?php
$host ="localhost";
$username = "username";
$password = "password";
$database = "database1";
$table ="users";
$con = mysql_connect("localhost","username","password");
if (!$con)
{
die('Could not connect:'.mysql_error());
}
mysql_select_db("database1",$con);
$mysql = "INSERT INTO $table(name,email,password)
VALUES('$_POST[name]','$_POST[email]','$_POST[password]";
if(mysql_query($mysql)) die(mysql_error());
echo"Data inserted";
mysql_close();
?>
I revised some of your code this should work. You had a bunch of little errors. I suggest you read a couple tutorials on just connecting and the syntax of php.
Here is some really basic examples of connecting to a database:
http://www.w3schools.com/php/php_mysql_connect.asp
Also once you get the hang of it here is a really good tutorial to teach you the OOP way of creating a class for a database:
http://net.tutsplus.com/tutorials/php/real-world-oop-with-php-and-mysql/
As far as I see this is not an ini issue. I hope this helps.
<?php
//Set your variables
$host = "127.0.0.1";
$username = "username";
$password = "password";
$database = "database1";
$table = "users";
//Make your connection to database
$con = mysql_connect($host,$username,$password);
//Check your connection
if (!$con) {
die("Could not connect: " . mysql_error());
}
//Select your database
$db_selected = mysql_select_db($database, $con);
//Check to make sure the database is there
if (!$db_selected) {
die ('Can\'t use the db : ' . mysql_error());
}
//Run query
$result = mysql_query("INSERT INTO $table(name,email,password) VALUES('$_POST[name]','$_POST[email]','$_POST[password]'");
//Check Query
if (!$result) {
die("lid query: " . mysql_error());
}
echo "Data inserted";
mysql_close($con);
?>
First, why do you have <br/> in your PHP statements? Remove all those.
Also, you have to use PDO or mysqli_ instead of the mysql_ library, mysql_ is deprecated.