Weird error while uploading to database - php

I'm creating app with connection to MySQL Database, I have some working PHP files on my domain already, but now I cannot normally use 1 of PHP files.
The PHP file looks like that :
<?php
$response = array();
require_once __DIR__ . '/db_connect.php';
$db = new DB_CONNECT();
if(isset($_POST['title']) && isset($_POST['dish']) && isset($_POST['review']) && isset($_POST['rating']) && isset($_POST['database_name']) && isset($_POST['UserKeyID']) && isset($_POST['dishphoto']) && isset($_POST['dishsmallphoto'])) {
$title = $_POST['title'];
$dish = $_POST['dish'];
$review = $_POST['review'];
$rating = $_POST['rating'];
$database_name = $_POST['database_name'];
$UserKeyID = $_POST['UserKeyID'];
$dishphoto = $_POST['dishphoto'];
$dishsmallphoto = $_POST['dishsmallphoto'];
$result = mysql_query("INSERT INTO " . $database_name . "(title, review, rating, dish, dishphoto, dishsmallphoto, UserKeyID) VALUES('$title', '$review', '$rating', '$dish', '$dishphoto', '$dishsmallphoto', '$UserKeyID')");
if ($result) {
$response["success"] = 1;
$response["message"] = "Product successfully created.";
// echoing JSON response
echo json_encode($response);
} else {
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
// echoing JSON response
echo json_encode($response);
}
} else {
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
?>
In logcat there is something like that :
[socket][/] connected
[CDS]rx timeout:0
[CDS]SO_SND_TIMEOUT:0
>doSendRequest
<doSendRequest

Related

JsonObjectRequest doesn't send or recive the PHP parameters

When I read the response, I always receive from PHP the error message: missing parameters in the query.
I know the PHP and query is well 100% because works. Only occurs in this way.
Volley.class
JSONObject params = new JSONObject();
params.put("consulta", "UPDATE Usuarios SET Nombre='AAA' WHERE IdUsuario=1");
//All right
RequestFuture<JSONObject> future = RequestFuture.newFuture();
JsonObjectRequest request = new JsonObjectRequest(Request.Method.POST, URL", params, future, future); //request contains params with de sql sentence. Its well
VolleyS.getInstance(context).mRequestQueue.add(request);
JSONObject response = future.get(10, TimeUnit.SECONDS); //response contains the wrong error and the database isn't changed
if (response.getInt("success") == 0) correctoExterna = false;
else correctoExterna = true;
update.php
if (isset($_REQUEST['consulta'])){
$vConsulta= $_REQUEST['consulta'];
$result = mysql_query($vConsulta);
// mysqli_errno($db) != 0
if ($result)
{
$response["success"] = 1;
$response["message"] = "OK.";
echo json_encode($response);
myqli_commit($db);
mysqli_close($db);
}
else{
$response["success"] = 0;
$response["message"] = "ERROR :".mysqli_error($db);
echo json_encode($response);
mysqli_rollback($db);
mysqli_close($db);
}
}
else {
$response["success"] = 0;
$response["message"] = "ERROR, missing fields to insert";
echo json_encode($response);
mysqli_close($db);
}

How do i add register validation into this PHP?

How do i add register validation so that user wont be able to use the same user id as others? I'm stuck here as I have tried every code and nothing would work. And it will crash my app.
I'm using Eclipse to do my app.
My php file
<?php
/*
* Following code will create a new product row
* All product details are read from HTTP Post Request
*/
// array for JSON response
$response = array();
// check for required fields
if (isset($_POST['user_name']) && isset($_POST['user_pwd'])){
$user_name = $_POST['user_name'];
$user_pwd = $_POST['user_pwd'];
// include db connect class
require_once __DIR__ . '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// mysql inserting a new row
$result = mysql_query("INSERT INTO doc_user (user_name, user_pwd) VALUES('$user_name', '$user_pwd')");
// check if row inserted or not
if ($result) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "Product successfully created.";
// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
// echoing JSON response
echo json_encode($response);
}
} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
change this part in your code
// connecting to db
$db = new DB_CONNECT();
// chack database
$result = mysql_query("select user_name from doc_user where user_name = '$user_name'");
if(mysql_num_rows($result)){
$response["success"] = 0;
$response["message"] = "username not available.";
// echoing JSON response
echo json_encode($response);
exit;
}
// mysql inserting a new row
$result = mysql_query("INSERT INTO doc_user (user_name, user_pwd) VALUES('$user_name', '$user_pwd')");

Cannot POST data into the database

I'm trying to add some data (strings) from my android app using POST. I'm currently testing the code with the Advanced Rest Client however so the imput's from there.
EDIT: To make it more clear what my question is - what's wrong with the code that it doesn't take the values and assign them? why does it skip the first if?
EDIT 2: I am using chrome.google.com/webstore/detail/advanced-rest-client/… to test this POST.
NOTES:
echo #name; -> it does nothing, doesn't echo a thing
Right now, it automatically enters the third else saying that required field(s) are missing.
I've tried using var_dump on $name and it states the following: Unexpected token s => that didn't make any sense.
Thanks in advance for the answers, if you need more info I'll gladly provide it.
This is the code:
$app->post('/users', function() use ($app, $mysql) {
$name = $app->request->post('name');
$email = $app->request->post('email');
$beer_type = $app->request->post('beer_type');
$favorite_beer = $app->request->post('favorite_beer');
$favorite_drink = $app->request->post('favorite_drink');
echo $name;
if (!empty($name) and !empty($email) and !empty($beer_type) and !empty($favorite_beer) and !empty($favorite_drink)) {
$insert = "INSERT INTO clients(name, email) VALUES({$name}, {$email})";
$getid = "SELECT id_client FROM clients WHERE email = {$email}";
$insert2 = "INSERT INTO preferences(client_id, beer_type, favorite_beer, favorite_drink) VALUES({$idvalue}, {$beer_type}, {$favorite_beer}, {$favorite_drink})";
$request = $mysql->query($insert);
$test = false;
$id = 0;
$test2 = false;
if ($request !== false) {
$test = true;
}
if ($test === true) {
$request = $mysql->query($getid);
if ($request !== false) {
$id = $request->fetch_all(MYSQLI_ASSOC);
}
}
if ($id !== 0) {
$request = $mysql->query($insert2);
if ($request !== false) {
$test2 = true;
}
}
if ($test2 === true) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "Product successfully created.";
// echoing JSON response
echo json_encode($response);
} else {
// failed to insert row
$response["success"] = 0;
$response["message"] = "Oops! An error occurred.";
// echoing JSON response
echo json_encode($response);
}
}else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
// echoing JSON response
echo json_encode($response);
}
});
I've solved my issue by changing the java code. I used the Ion Library for the POST and it worked perfectly fine.

php mysql how to fix this error :Undefined variable :id in C:\

how to fix the error:
Undefined variable : id in c:\wamp\www\android_connect\status.php in 38
what i am doing wrong ???
status.php
<?php
ob_start();
session_start();
//array for JSON response
$response = array();
if(isset($_SESSION['id']))
{
$id= $_SESSION['id'];
}
//var_dump ($id);
// include db connect class
require_once '/db_connect.php';
// connecting to db
$db = new DB_CONNECT();
// check for required fields
if(empty($_POST['status'])){
$response["success"] = 0;
$response["message"] = "Your Text Field is empty";
// echoing JSON response
die (json_encode($response));
}
else if (isset($_POST['status'])) {
$status = $_POST['status'];
$sql = mysql_query("INSERT INTO status (status_text, uid)VALUES('$status', '$id')")or die(mysql_error());
if ($sql) {
// successfully inserted into database
$response["success"] = 1;
$response["message"] = "Status Saved.";
// echoing JSON response
die (json_encode($response));
} else {
// failed to insert row
$response["success"] = 0;
$response["message"] = "Error in saving the status.";
// echoing JSON response
die (json_encode($response));
}
}
ob_end_flush();
?>
the error is in the insert query but i do not know how to fix it.
it have something with the $_SESSION can anyone help me ??
The error you get means $id is unset. In your code there isn't a check for the $id variable.
Change else if (isset($_POST['status'])) { to else if (isset($_POST['status']) && isset($id)) {
and add an else statement, something like this:
else {
// failed to insert row because of missing $id
$response["success"] = 0;
$response["message"] = "Error in saving the status, session variable $id missing";
// echoing JSON response
die (json_encode($response));
}
Change the insert query like this,
$sql = mysql_query("INSERT INTO status (status_text, uid)VALUES('".$status."', '".$id."')")or die(mysql_error());

mysql query and encoding it in json format

I'm using this simple php code to encode the MySql query result in json format.
But Don't know why it is not giving me the desirable output.
I'm actually trying to get the employee details by entering their 'employee_number'.
<?php
require('DB_Connect.php');
require('config.php');
// check for post data
/*
if (isset($_POST["employee_number"])) {
$employee_name = $_POST['employee_number'];
*/
//get a employee from employee_info table
$result = mysql_query("SELECT *FROM employee_info WHERE employee_number =9876543210");
if (!empty($result)) {
// check for empty result
if (mysql_num_rows($result) > 0) {
$result = mysql_fetch_array($result);
echo("Success !! Yoo");
$employee = array();
$employee["employee_number"] = $result["employee_number"];
$employee["employee_name"] = $result["employee_name"];
$employee["flag"]=$result["flag"];
// success
$response["success"] = 1;
// user node
$response["employee"] = array();
array_push($response["employee"], $employee);
// echoing JSON response
echo json_encode($response);
} else {
// no employee found
$response["success"] = 0;
$response["message"] = "No employee found";
// echo no users JSON
echo json_encode($response);
}
}else {
// no employee found
$response["success"] = 0;
$response["message"] = "No employee found";
// echo no users JSON
echo json_encode($response);
}
/*} else {
// required field is missing
$response["success"] = 0;
$response["message"] = "Required field(s) is missing";
*/
// echoing JSON response
echo json_encode($response);
?>
mysql_query is deprecated. Use mysqli or PDO instead.
make sure you format your query properly *note the spaces
$result = mysql_query("SELECT * FROM employee_info WHERE employee_number = 9876543210");
you could also just echo the $result as your employee array doesn't seem to do anything.
echo json_encode($result);

Categories