Advertisement
Guest User

Untitled

a guest
May 9th, 2016
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 9.38 KB | None | 0 0
  1. Trying to run query to get user_id based on logged in username.
  2. users.php....
  3.  
  4.  
  5. <?php
  6. session_start();
  7. include_once 'error.php';
  8.  
  9. class User{
  10.  
  11. private $db;
  12. private $db_table = "users";
  13.  
  14. public function __construct()
  15. {
  16. $this->db = new DbConnect();
  17. }
  18.  
  19.  
  20. public function isLoginExist($username, $password)
  21. {
  22.  
  23. $query = "select * from " . $this->db_table . " where username =
  24. '$username' AND password = '$password' Limit 1";
  25. $result = mysqli_query($this->db->getDb(), $query);
  26. if(mysqli_num_rows($result) > 0){
  27. mysqli_close($this->db->getDb());
  28. return true;
  29. }
  30. mysqli_close($this->db->getDb());
  31. return false;
  32. }
  33.  
  34. public function createNewRegisterUser($username, $password, $email)
  35. {
  36.  
  37. $query = "insert into users (username, password, email, created_at,
  38. updated_at) values ('$username', '$password', '$email', NOW(), NOW())";
  39. $inserted = mysqli_query($this->db->getDb(), $query);
  40. if($inserted == 1){
  41. $json['success'] = 1;
  42. }else{
  43. $json['success'] = 0;
  44. }
  45. mysqli_close($this->db->getDb());
  46. return $json;
  47. }
  48.  
  49.  
  50.  
  51. public function loginUsers($username, $password){
  52.  
  53. $json = array();
  54. $canUserLogin = $this->isLoginExist($username, $password);
  55. if($canUserLogin){
  56. $json['success'] = 1;
  57. }else{
  58. $json['success'] = 0;
  59. }
  60. return $json;
  61. }
  62. }
  63.  
  64.  
  65. ?>
  66.  
  67. index.php
  68.  
  69. <?php
  70. session_start();
  71. require_once 'users.php';
  72.  
  73. $username = $_SESSION["username"];
  74. $password = "";
  75. $email = "";
  76.  
  77.  
  78.  
  79. if(isset($_POST['username'])){
  80. $username = $_POST['username'];
  81. }
  82. if(isset($_POST['password'])){
  83. $password = $_POST['password'];
  84. }
  85. if(isset($_POST['email'])){
  86. $email = $_POST['email'];
  87. }
  88.  
  89. // Instance of a User class
  90. $userObject = new User();
  91.  
  92.  
  93. // Registration of new user
  94. if(!empty($username) && !empty($password) && !empty($email)){
  95. $hashed_password = md5($password);
  96. $json_registration = $userObject->createNewRegisterUser($username,
  97. $hashed_password, $email);
  98.  
  99. echo json_encode($json_registration);
  100. }
  101.  
  102. // User Login
  103. if(!empty($username) && !empty($password)){
  104. $hashed_password = md5($password);
  105. $json_array = $userObject->loginUsers($username, $hashed_password);
  106.  
  107. echo json_encode($json_array);
  108. }
  109.  
  110. ?>
  111.  
  112. <?php
  113. session_start();
  114. include_once 'error.php';
  115.  
  116.  
  117. class Topic{
  118.  
  119. private $db;
  120. private $db_table = "topics";
  121. private $db_table1 = "created_topics";
  122.  
  123.  
  124.  
  125. public function __construct()
  126. {
  127. $this->db = new DbConnect();
  128. }
  129.  
  130. public function createNewTopic($topic_name, $content)
  131. {
  132. //query to get current logged in user_id
  133. $un = "SELECT user_id FROM users WHERE username = '$username' LIMIT 1";
  134. //running query
  135. $unResults = mysqli_query($this->db->getDb(), $un);
  136.  
  137. //insert into db topic_name and content
  138. $query = "INSERT INTO topics (topic_name, content, created_at,
  139. updated_at) values ('$topic_name', '$content', NOW(), NOW())";
  140.  
  141. //query to insert into created_topics table with user_id and topic_id
  142. $q = "insert into created_topics(user_id,topic_id,created_at) values
  143. ($unResults, LAST_INSERT_ID(),NOW())";
  144.  
  145.  
  146. $inserted = mysqli_query($this->db->getDb(), $query);
  147.  
  148. mysqli_query($this->db->getDb(), $q);
  149.  
  150. if($inserted == 1){
  151. $json['success'] = 1;
  152. }else{
  153. $json['success'] = 0;
  154. }
  155.  
  156. mysqli_close($this->db->getDb());
  157. return $json;
  158.  
  159. }
  160. }
  161.  
  162.  
  163. ?>
  164.  
  165. created_topic.php
  166.  
  167. <?php
  168. session_start();
  169. require_once 'topics.php';
  170.  
  171. $topic_name = "";
  172. $content = "";
  173. $username = $_SESSION['username'];
  174.  
  175.  
  176. if(isset($_POST['topic_name']))
  177. {
  178. $topic_name = $_POST['topic_name'];
  179. }
  180. if(isset($_POST['content']))
  181. {
  182. $content = $_POST['content'];
  183. }
  184.  
  185.  
  186.  
  187. // Instance of a Topic class
  188. $topicObject = new Topic();
  189.  
  190. // Registration of new topic
  191. if(!empty($topic_name) && !empty($content))
  192. {
  193.  
  194. $json_registration = $topicObject->createNewTopic($topic_name, $content);
  195.  
  196. echo json_encode($json_registration);
  197. }
  198.  
  199. ?>
  200.  
  201. package com.example.mrbuknahsty.annovoteexdb;
  202.  
  203. import android.content.Intent;
  204. import android.os.AsyncTask;
  205. import android.os.Bundle;
  206. import android.support.v7.app.AppCompatActivity;
  207. import android.view.View;
  208. import android.widget.Button;
  209. import android.widget.EditText;
  210. import android.widget.Toast;
  211.  
  212. import org.apache.http.HttpResponse;
  213. import org.apache.http.NameValuePair;
  214. import org.apache.http.client.ClientProtocolException;
  215. import org.apache.http.client.HttpClient;
  216. import org.apache.http.client.entity.UrlEncodedFormEntity;
  217. import org.apache.http.client.methods.HttpPost;
  218. import org.apache.http.impl.client.DefaultHttpClient;
  219. import org.apache.http.message.BasicNameValuePair;
  220. import org.apache.http.params.BasicHttpParams;
  221. import org.apache.http.params.HttpConnectionParams;
  222. import org.apache.http.params.HttpParams;
  223. import org.json.JSONException;
  224. import org.json.JSONObject;
  225.  
  226. import java.io.BufferedReader;
  227. import java.io.IOException;
  228. import java.io.InputStream;
  229. import java.io.InputStreamReader;
  230. import java.util.ArrayList;
  231. import java.util.List;
  232.  
  233. public class createTopic extends AppCompatActivity
  234. {
  235. protected EditText enteredTopicName,enteredContent;
  236.  
  237. Button create;
  238.  
  239. protected String topic_name;
  240.  
  241. private final String serverUrl1 =
  242. "http://lkirkpatrick.btcwsd.com/anno/create_topic.php";
  243.  
  244.  
  245.  
  246. @Override
  247. protected void onCreate(Bundle savedInstanceState)
  248. {
  249. super.onCreate(savedInstanceState);
  250. setContentView(R.layout.activity_create_topic);
  251.  
  252. enteredTopicName = (EditText) findViewById(R.id.topicNameET);
  253. enteredContent = (EditText) findViewById(R.id.contentEdit);
  254.  
  255. create = (Button)findViewById(R.id.createBtn);
  256.  
  257. create.setOnClickListener(new View.OnClickListener() {
  258.  
  259. @Override
  260.  
  261. public void onClick(View v) {
  262.  
  263. topic_name = enteredTopicName.getText().toString();
  264.  
  265. String content = enteredContent.getText().toString();
  266.  
  267. if(topic_name.equals("") || content.equals("")){
  268.  
  269. Toast.makeText(createTopic.this, "Topic Name or Content must
  270. be filled", Toast.LENGTH_LONG).show();
  271.  
  272. return;
  273.  
  274. }
  275.  
  276. if(topic_name.length() <= 1 || content.length() <= 1){
  277.  
  278. Toast.makeText(createTopic.this, "Topic Name or Content
  279. length must be greater than one", Toast.LENGTH_LONG).show();
  280.  
  281. return;
  282.  
  283. }
  284.  
  285. // request authentication with remote server4
  286.  
  287. AsyncDataClass asyncRequestObject = new AsyncDataClass();
  288.  
  289. asyncRequestObject.execute(serverUrl1, topic_name, content);
  290.  
  291. }
  292.  
  293. });
  294. }
  295.  
  296. private class AsyncDataClass extends AsyncTask<String, Void, String> {
  297.  
  298. @Override
  299.  
  300. protected String doInBackground(String... params) {
  301.  
  302. HttpParams httpParameters = new BasicHttpParams();
  303.  
  304. HttpConnectionParams.setConnectionTimeout(httpParameters, 5000);
  305.  
  306. HttpConnectionParams.setSoTimeout(httpParameters, 5000);
  307.  
  308. HttpClient httpClient = new DefaultHttpClient(httpParameters);
  309.  
  310. HttpPost httpPost = new HttpPost(params[0]);
  311.  
  312. String jsonResult = "";
  313.  
  314. try {
  315.  
  316. List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>
  317. (2);
  318.  
  319. nameValuePairs.add(new BasicNameValuePair("topic_name",
  320. params[1]));
  321.  
  322. nameValuePairs.add(new BasicNameValuePair("content", params[2]));
  323.  
  324. nameValuePairs.add(new BasicNameValuePair("content", params[2]));
  325.  
  326. httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
  327.  
  328. HttpResponse response = httpClient.execute(httpPost);
  329.  
  330. jsonResult =
  331. inputStreamToString(response.getEntity().getContent()).toString();
  332.  
  333. } catch (ClientProtocolException e) {
  334.  
  335. e.printStackTrace();
  336.  
  337. } catch (IOException e) {
  338.  
  339. e.printStackTrace();
  340.  
  341. }
  342.  
  343. return jsonResult;
  344.  
  345. }
  346.  
  347. @Override
  348.  
  349. protected void onPreExecute() {
  350.  
  351. super.onPreExecute();
  352.  
  353. }
  354.  
  355. @Override
  356.  
  357. protected void onPostExecute(String result) {
  358.  
  359. super.onPostExecute(result);
  360.  
  361. System.out.println("Resulted Value: " + result);
  362.  
  363. if(result.equals("") || result == null){
  364.  
  365. Toast.makeText(createTopic.this, "Server connection failed",
  366. Toast.LENGTH_LONG).show();
  367.  
  368. return;
  369.  
  370. }
  371.  
  372. int jsonResult = returnParsedJsonObject(result);
  373.  
  374. if(jsonResult == 0){
  375.  
  376. Toast.makeText(createTopic.this, "Something Went Wrong",
  377. Toast.LENGTH_LONG).show();
  378.  
  379. return;
  380.  
  381. }
  382.  
  383. if(jsonResult == 1){
  384.  
  385. Intent intent = new Intent(createTopic.this, login.class);
  386.  
  387. intent.putExtra("USERNAME", topic_name);
  388.  
  389. intent.putExtra("MESSAGE", "Topic successfully created!");
  390.  
  391. startActivity(intent);
  392.  
  393. }
  394.  
  395. }
  396.  
  397. private StringBuilder inputStreamToString(InputStream is) {
  398.  
  399. String rLine = "";
  400.  
  401. StringBuilder answer = new StringBuilder();
  402.  
  403. BufferedReader br = new BufferedReader(new InputStreamReader(is));
  404.  
  405. try {
  406.  
  407. while ((rLine = br.readLine()) != null) {
  408.  
  409. answer.append(rLine);
  410.  
  411. }
  412.  
  413. } catch (IOException e) {
  414.  
  415. e.printStackTrace();
  416.  
  417. }
  418.  
  419. return answer;
  420.  
  421. }
  422.  
  423. }
  424.  
  425. private int returnParsedJsonObject(String result){
  426.  
  427. JSONObject resultObject = null;
  428.  
  429. int returnedResult = 0;
  430.  
  431. try {
  432.  
  433. resultObject = new JSONObject(result);
  434.  
  435. returnedResult = resultObject.getInt("success");
  436.  
  437. } catch (JSONException e) {
  438.  
  439. e.printStackTrace();
  440.  
  441. }
  442.  
  443. return returnedResult;
  444.  
  445. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement