I just want to post a simple variable from my phonegap app to mysql database. But it dont work. The db show me empty results.
Mysql database looks like this, Tablename is user
Rows:
user (varchar 25),vip (varchar 1),call (varchar 1)
At top of my project i inserted:
<script type="text/javascript" src="js/tcPlugin.js"></script>
<script type="text/javascript" src="js/phoneApp.js"></script>
<script type="text/javascript" src="js/start.js"></script>
start.js is my file for the ajax request - the other two working fine so i guess there is not the problem
my start.js file looks like this:
$(document).ready( function() {
var email = "john#web.de";
$.ajax({
type: 'post',
url: 'http://www.blablabla.de/phone/action.php',
data: {
data: {"email" : email },
//data:email,
},
success: function(result) {
console.log(result);
}
});
and my action.php looks like this:
<?php
$dbhost = "bla";
$dbuser = "bla";
$dbpass = "bla";
$dbname = "bla";
$user = $_POST['data']['email'];
$conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO user (email)
VALUES ('" . $user . "')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
But everytime i start the app the db is empty. debugger concole is not giving me any errors. db connection is working, tried it with echo connection result. and the other js are working fine.
action.php
<?php
$dbhost = "bla";
$dbuser = "bla";
$dbpass = "bla";
$dbname = "bla";
print_r($_POST); // see full contents of the POST
$user = $_POST['data']['email'];
print PHP_EOL . $user . PHP_EOL; // see full contents of the $user var
$conn = new mysqli($dbhost, $dbuser, $dbpass, $dbname);
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO user (email) VALUES ('" . $user . "')";
print PHP_EOL . $sql . PHP_EOL; // see the actual sql query, you can test this later in your sql editor ex. phpmyadmin
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
make these edits, run the ajax and check the response tab in the debugger.
Related
The below code I got from the w3 schools website and adapted it a little to my db but no matter what when I click on my search button I only get the first line printed and nothing else. It doesn't even print if the connection to my db is successfully made or not.
I used chrome's dev tool to check my network traffic and I can see my POST request made successfully:
name: bahamas
submit: Search
I enabled logging for both error and general on my mysql instance, and did a grep for bahamas and got no hits. So this would seem to indicate that the script didn't even query my db?
IE this is what I get: https://imgur.com/a/PHKBmbU
<?php echo("PHP Search Page Loaded Successfully");
if(isset($_POST['submit'])){
if(preg_match("^/[A-Za-z]+/", $_POST['name'])){
$servername = "localhost";
$username = "test";
$password = "test";
$dbname = "test";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else {
echo("We are not dead");
}
$sql = "SELECT boatname, date, price FROM liveaboards";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo("<br> boatname: ". $row["boatname"]. " - date: ". $row["date"]. " " . $row["price"] . "<br>");
}
} else {
echo(" 0 results");
}
$conn->close();
}
}else{
echo("<p>Please enter a search query</p>");
}
?>
In following the theme of W3 Schools (one of my favorite resources), here is the correct way to sanitize data: https://www.w3schools.com/php/php_filter.asp
Applied to your code:
<?php echo("PHP Search Page Loaded Successfully");
// test variables.
$_POST['submit'] = true;
$_POST['name'] = "bahamas";
if(isset($_POST['submit'])){
$_POST['name'] = filter_var($_POST['name'], FILTER_SANITIZE_STRING);
$servername = "localhost";
$username = "test";
$password = "test";
$dbname = "test";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
} else {
echo("We are not dead");
}
$sql = "SELECT boatname, date, price FROM liveaboards";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo("<br> boatname: ". $row["boatname"]. " - date: ". $row["date"]. " " . $row["price"] . "<br>");
}
} else {
echo(" 0 results");
}
$conn->close();
}else{
echo("<p>Please enter a search query</p>");
}
?>
According to your POST result, you are receiving name: bahamas submit: Search , if so then Correct this
if(isset($_POST['submit'])){
TO
if(isset($_POST['Search'])){
I want to set up a website with a form in it. The form will transfer the data to the DB, but I think it is not safe to let the personal data in the DB which is external reachable.
So I thought I should transfer the data via PHP from the DB1(server1 - external reachable) to DB2(server2 - only internal reachable).
The following picture should help to know what I am searching for.
Is there any names/methods to google for?
From php you just have to create a new connection for DB2.
<?php
$servername = "localhost";
$username = "database1";
$password = "xxxxxxxx";
$dbname = "database1";
$servername2 = "localhost";
$username2 = "database2";
$password2 = "xxxxxxxx";
$dbname2 = "database2";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
$conn2 = new mysqli($servername2, $username2, $password2, $dbname2);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if ($conn2->connect_error) {
die("Connection failed: " . $conn2->connect_error);
}
//escape variables for security
$fname = mysqli_real_escape_string($conn, $_POST['fname']);
$lname = mysqli_real_escape_string($conn, $_POST['lname']);
$sql = "INSERT INTO mytable (fname,lname)
VALUES ('$fname','$lname')";
if ($conn->query($sql) === TRUE) {
echo "Successfully Saved";
} else {
echo "Error: Go back and Try Again ! " . $sql . "<br>" . $conn->error;
}
if ($conn2->query($sql) === TRUE) {
echo "Successfully Saved";
} else {
echo "Error: Go back and Try Again ! " . $sql . "<br>" . $conn2->error;
}
$conn->close();
$conn2->close();
?>
I have very strange problem. I want to run mysql query as it is shown down below, but it's not working. Connection to database is successful, INSERT query is ok too, because when I run it directly in phpmyadmin Console it works, but it's not working here in PHP code.
Could you tell me what I'm missing?
$servername = "localhost";
$username = "admin";
$password = "admin123";
$dbname = "database1";
$conn = mysqli_connect($servername, $username, $password, $dbname);
if (!$conn) {
die("Connection failed: " . mysqli_connect_error());
}
$sql = "INSERT INTO last_visit (ip, lastvisit) VALUES ('123', '123')";
You need to run your $sql, because now your $sql is only a string, it does nothing.
Add this :
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
im trying to do a post to a php and save in a mysql database
this is the ION.with command:
Ion.with(this)
.load("http_my_page/test.php")
.setBodyParameter("text", "não")
.asString(Charset.defaultCharset())//default=UTF-8
.setCallback(new FutureCallback<String>() {
#Override
public void onCompleted(Exception e, String result) {
}
});
my test.php page:
<?php
$test = $_POST["text"];
$servername = ...;
$username = "..";
$password = "...";
$dbname = "...";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$sql = "INSERT INTO test (my_test) VALUES ('$test')";
if ($conn->query($sql) === TRUE) {
echo "New record created successfully";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
$conn->close();
?>
and my MYSQL database has a column called my_test and collation is UTF8_unicode_ci
what im specting to store: "não"
what it really store: "não"
any reason? since im setting all the econdings =(
That was a PHP/MYSQL encoding issue and nothing related to Ion
$conn->set_charset("utf8");
I have wrote a script to update a MySql DB from a form.
After the DB has been updated I want the page to auto redirect to another page.
This has been working fine however since switching hosting provider non of my sites re-directs work.
Here is the code:
<?php
$servername = "localhost";
$username = "XXX";
$password = "XXX";
$dbname = "XXX";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
$id= $_POST[id];
$dob=$_POST[dob];
$sql=("update users set dob='$dob' where id='$id'")or die('Error 23 ' . mysql_error());
if ($conn->query($sql) === TRUE) {
echo "Updated successfully<br /><br />";
} else {
echo "Error: " . $sql . "<br>" . $conn->error;
}
?>
<?php
header("location:index.php?action=updated"); ?>
When I run the code the DB updates but the page just displays Updated successfully?
try using javascript to redirect like below:
if ($conn->query($sql) === TRUE) {
echo "<script>
alert('Updated successfully');
window.location.href = 'index.php?action=updated';
</script>";
}
Don't echo anything and try to redirect afterwards. Instead simply redirect without any echoing.
if ($conn->query($sql) === TRUE) {
header("location:index.php?action=updated");
exit;
} else
echo "Error: " . $sql . "<br>" . $conn->error;
try this :
<?php
ob_start();
header('Location: http://www.example.com/index.php?action=updated', true);
?>