Łączenie sql'a z Android studio

0

Witam wszystkich.
Mam problem z połączeniem bazy danych z moją aplikacją na androida. Jeżeli chodzi o korzystanie z sql'a jestem nowicjuszem dlatego też piszę z prośbą o pomoc ;).

Napisałem prosty kod w php który miał mi dać możliwość połączenia się z moją bazą danych w celu rejestracji i logowania użytkownika. jak przy jednym i drugim pliku php wyskakują błędy, których nie rozumiem.

KOD PHP REJESTRACJI:

<?php

$con = mysqli_connect("my_host", "my_user", "my_password", "my_db");
    
    $name = $_POST["name"];
    $surname = $_POST["surname"];
    $username = $_POST["username"];
    $age = $_POST["age"];
    $password = $_POST["password"];
    

    $statement = mysqli_prepare($con, "INSERT INTO user (name, surname, username, age, password) VALUES  (?, ?, ?, ?)"); 
    mysqli_stmt_bind_param($statement, "siss", $name, $surname, $username, $age, $password);
    mysqli_stmt_execute($statement);

   
    $response = array();
    $response["success"] =true;
    
    echo json_encode($response);

?>

BŁĘDY:

Notice: Undefined index: name in /storage/h2/761/565761/public_html/Register.php on line 5

Notice: Undefined index: surname in /storage/h2/761/565761/public_html/Register.php on line 6

Notice: Undefined index: username in /storage/h2/761/565761/public_html/Register.php on line 7

Notice: Undefined index: age in /storage/h2/761/565761/public_html/Register.php on line 8

Notice: Undefined index: password in /storage/h2/761/565761/public_html/Register.php on line 9

Warning: mysqli_stmt_bind_param() expects parameter 1 to be mysqli_stmt, boolean given in /storage/h2/761/565761/public_html/Register.php on line 13

Warning: mysqli_stmt_execute() expects parameter 1 to be mysqli_stmt, boolean given in /storage/h2/761/565761/public_html/Register.php on line 14
{"success":true}

KOD PHP LOGOWANIA

<?php
    $con = mysqli_connect("localhost", "id565761_zbrozli, "greg0ri0", "id565761_inzynierka");
    
    $username = $_POST["username"];
    $password = $_POST["password"];
    
    $statement = mysqli_prepare($con, "SELECT * FROM user WHERE username = ? AND password = ?");
    mysqli_stmt_bind_param($statement, "ss", $username, $password);
    mysqli_stmt_execute($statement);
    
    mysqli_stmt_store_result($statement);
    mysqli_stmt_bind_result($statement, $userID, $name, $age, $username, $password);
    
    $response = array();
    $response["success"] = false;  
    
    while(mysqli_stmt_fetch($statement)){
        $response["success"] = true;  
        $response["name"] = $name;
        $response["age"] = $age;
        $response["username"] = $username;
        $response["password"] = $password;
    }
    
    echo json_encode($response);
?>

BŁĘDY:

otice: Undefined index: username in /storage/h2/761/565761/public_html/Login.php on line 4

Notice: Undefined index: password in /storage/h2/761/565761/public_html/Login.php on line 5

Warning: mysqli_stmt_bind_result(): Number of bind variables doesn't match number of fields in prepared statement in /storage/h2/761/565761/public_html/Login.php on line 12
{"success":false}
`
Z góry dziękuję za pomoc i przepraszam za forme :)
0

Problemem jest nie tylko znajomość SQLa a również PHP.

  1. BRAKUJE WALIDACJI DANYCH WEJŚCIOWYCH I OBSŁUGI BŁĘDÓW
  2. Źle formułujesz żądanie HTTP o czym świadczą "Notice: Undefined index: name in /storage/h2/761/565761/public_html/Register.php on line 5".
  3. W zapytaniu masz 5 kolumn, 4 placeholdery i przekazujesz 5 parametrów
 $statement = mysqli_prepare($con, "INSERT INTO user (name, surname, username, age, password) VALUES  (?, ?, ?, ?)"); 
mysqli_stmt_bind_param($statement, "siss", $name, $surname, $username, $age, $password);
  1. Zamiast funkcji "mysqli_" użyj PDO albo jednej z bibliotek do mapowania obiektowo relacyjnego: http://www.doctrine-project.org/ lub http://propelorm.org/ . Do prototypownia API polecam również któryś z mikroframeworków PHP np. http://silex.sensiolabs.org/ , bo dzisiaj nie pisze się aplikacji w PHP na zasadzie zbioru "luźnych" skryptów typu Register.php, Login.php.

1 użytkowników online, w tym zalogowanych: 0, gości: 1