简体   繁体   中英

How to delete " from inserted data to MySQL database

I have got a problem.

Today, I made android application for inserting data into MySQL database.

When I am trying insert String type ( example: name ), in records show this: "name" .

数据库中的行 <---- In database it looks like this image

How to delete " from inserted String

This is my PHP script:

<?php
require "connect.php";

$firstHash = substr(str_shuffle(str_repeat("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPRSTUWYZX", 5)), 0, 5);
$secondHash = substr(str_shuffle(str_repeat("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPRSTUWYZX", 5)), 0, 20);

$file_path_URL = "";

$id = "PRD-$firstHash-$secondHash";
$name = $_POST["name"];
$description = $_POST["description"];
$price = $_POST["price"];
$state = $_POST["state"];
$category = $_POST["category"];

$file_path = "uploads/";

$response = array();

if (isset($_FILES["upload_file"]))
{
    $target_file_name = $file_path .basename("$id.png");

    if(file_exists($target_file_name))
    {
        $success = false;
        $message = "Name already exist";
    }
    else
    {
        if (move_uploaded_file($_FILES["upload_file"]["tmp_name"], $target_file_name))
        {
            $photoPath = "$file_path_URL$id.png";
            $query = "INSERT INTO `produkty`(`ID`, `Nazwa`, `Opis`, `Cena`, `Stan`, `Zdjecie`, `Kategoria`) VALUES ('$id', '$name', '$description', $price, $state, '$photoPath', '$category')";

            $result = mysqli_query($conn, $query);

            if(!$result)
            {
                $success = false;
                $message = "Error on insert data";
            }
            else
            {
                $success = true;
                $message = "Add product successfuly";
            }
        }
    else
        {
            $success = false;
            $message = "Error while uploading";
        }

    }


}
else
{
    $success = false;
    $message = "Required Field Missing";
}

$response["success"] = $success;
$response["message"] = $message;

$json = json_encode($response);

echo ($json);
?>

Method responsible for get entered data from user in Android:

    private void getValues()
{
    Log.v(TAG, "getValues(): init");

    EditText nameET = findViewById(R.id.nameET);
    EditText descriptionET = findViewById(R.id.descriptionET);
    EditText priceET = findViewById(R.id.priceET);
    EditText categoryET = findViewById(R.id.categoryET);
    CheckBox stateCHB = findViewById(R.id.checkBox);

    String name = nameET.getText().toString();
    String description = descriptionET.getText().toString();
    Float price = Float.valueOf(priceET.getText().toString());
    String category = categoryET.getText().toString();
    boolean state;

    if (stateCHB.isChecked())
    {
        state = true;
    }
    else
    {
        state = false;
    }

    sendDataToServer(name, description, price, state, category, imagePath);

    Log.v(TAG, "getValues(): end");

}

I posted it into PHP like String. This is a mistake ?

I am inserting to MySQL database from Android application first time. Please, help me !

I don't immediately see where the quote is coming from. Perhaps inside 'sendDataToServer' but one solution would be to replace

$name = $_POST["name"];

with

$name = str_replace( '"', '', $_POST["name"]);

in your PHP code to remove the quotes from the POST request when it arrives.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM