Mysql 当从 sdcard 读取的数据有撇号时出现错误 1064 - Android,PHP,MySQL
Mysql error 1064 when data read from sdcard have apostrophe - Android,PHP,MySQL
代码有什么问题
如果数据从我的 sdcard 信息中读取 "some big or small string" 那么它的工作正常并显示 "successfully Entered" 但是当它带有像 "some big or small string's" 这样的撇号时,它就不起作用并显示 "oops! Please try again!" 来自我的 song.php 文件。
这是我的 song.php 代码
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
$date_today = $_POST['date_today'];
$data_name = $_POST['data_name'];
$data_value = $_POST['data_value'];
require_once('Connect.php');
$sql = "INSERT INTO songs VALUES('$data_name','$data_value','$date_today')";
if(mysqli_query($con,$sql)){
echo 'successfully Entered';
}else{
echo 'oops! Please try again!';
}
mysqli_close($con);
}else{
echo 'error';
}
Connect.php
<?php
define('HOST','localhost');
define('USER','root');
define('PASS','');
define('DB','dracula');
$con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect');
?>
歌曲数据class
class SongData extends AsyncTask<String,Void,String>{
@Override
protected String doInBackground(String... params) {
ContentResolver mr = getContentResolver();
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor mc = mr.query(uri,null,null,null,null);
if (mc!=null&&mc.moveToFirst()){
int titleColumn = mc.getColumnIndex(MediaStore.Audio.Media.TITLE);
int idColumn = mc.getColumnIndex(MediaStore.Audio.Media._ID);
int artist = mc.getColumnIndex(MediaStore.Audio.Media.ARTIST);
do{
long thisId = mc.getLong(idColumn);
String thisTitle = mc.getString(titleColumn);
String thisArtist = mc.getString(artist);
String mp = "TITLE "+thisTitle+"\nARTIST "+thisArtist;
song += "\n\n-------------------------\n"+mp;
}while(mc.moveToNext());
mc.close();
}
return song;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
register(date_today, device, s, "http://192.168.1.102/song.php");
}
}
这里是注册方法
private void register(String data_name, String data_value,String date_current,String url) {
class RegisterUser extends AsyncTask<String, Void, String>{
ConnectionClass cc = new ConnectionClass();
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
}
@Override
protected String doInBackground(String... params) {
HashMap<String, String> data = new HashMap<>();
data.put("data_name",params[0]);
data.put("data_value", params[1]);
data.put("date_today", params[2]);
String result = cc.sendPostRequest(params[3],data);
return result;
}
}
RegisterUser ru = new RegisterUser();
ru.execute(data_name,data_value,date_current,url);
}
这里是ConnectionClass.java
import android.util.Log;
import org.apache.http.HttpException;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import javax.net.ssl.HttpsURLConnection;
public class ConnectionClass {
public String sendPostRequest(String requestURL,
HashMap<String, String> postDataParams) {
URL url;
String response = "";
try {
url = new URL(requestURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();
int responseCode=conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream()));
response = br.readLine();
}
else {
response="Error Registering";
}
} catch (Exception e) {
e.printStackTrace();
response = "not connected";
}
return response;
}
private String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
boolean first = true;
for(Map.Entry<String, String> entry : params.entrySet()){
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
}
Log.i("result",result.toString());
return result.toString();
}
}
我得到了答案。我在我的 SongData Class onPostExecute
mehod
中编辑并放入下面一行
String newStr = s.replace("\'", "");
register(date_today, device, newStr, "http://192.168.1.102/song.php");
代码有什么问题
如果数据从我的 sdcard 信息中读取 "some big or small string" 那么它的工作正常并显示 "successfully Entered" 但是当它带有像 "some big or small string's" 这样的撇号时,它就不起作用并显示 "oops! Please try again!" 来自我的 song.php 文件。
这是我的 song.php 代码
<?php
if($_SERVER['REQUEST_METHOD']=='POST'){
$date_today = $_POST['date_today'];
$data_name = $_POST['data_name'];
$data_value = $_POST['data_value'];
require_once('Connect.php');
$sql = "INSERT INTO songs VALUES('$data_name','$data_value','$date_today')";
if(mysqli_query($con,$sql)){
echo 'successfully Entered';
}else{
echo 'oops! Please try again!';
}
mysqli_close($con);
}else{
echo 'error';
}
Connect.php
<?php
define('HOST','localhost');
define('USER','root');
define('PASS','');
define('DB','dracula');
$con = mysqli_connect(HOST,USER,PASS,DB) or die('Unable to Connect');
?>
歌曲数据class
class SongData extends AsyncTask<String,Void,String>{
@Override
protected String doInBackground(String... params) {
ContentResolver mr = getContentResolver();
Uri uri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
Cursor mc = mr.query(uri,null,null,null,null);
if (mc!=null&&mc.moveToFirst()){
int titleColumn = mc.getColumnIndex(MediaStore.Audio.Media.TITLE);
int idColumn = mc.getColumnIndex(MediaStore.Audio.Media._ID);
int artist = mc.getColumnIndex(MediaStore.Audio.Media.ARTIST);
do{
long thisId = mc.getLong(idColumn);
String thisTitle = mc.getString(titleColumn);
String thisArtist = mc.getString(artist);
String mp = "TITLE "+thisTitle+"\nARTIST "+thisArtist;
song += "\n\n-------------------------\n"+mp;
}while(mc.moveToNext());
mc.close();
}
return song;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
register(date_today, device, s, "http://192.168.1.102/song.php");
}
}
这里是注册方法
private void register(String data_name, String data_value,String date_current,String url) {
class RegisterUser extends AsyncTask<String, Void, String>{
ConnectionClass cc = new ConnectionClass();
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
}
@Override
protected String doInBackground(String... params) {
HashMap<String, String> data = new HashMap<>();
data.put("data_name",params[0]);
data.put("data_value", params[1]);
data.put("date_today", params[2]);
String result = cc.sendPostRequest(params[3],data);
return result;
}
}
RegisterUser ru = new RegisterUser();
ru.execute(data_name,data_value,date_current,url);
}
这里是ConnectionClass.java
import android.util.Log;
import org.apache.http.HttpException;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.io.UnsupportedEncodingException;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;
import javax.net.ssl.HttpsURLConnection;
public class ConnectionClass {
public String sendPostRequest(String requestURL,
HashMap<String, String> postDataParams) {
URL url;
String response = "";
try {
url = new URL(requestURL);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(15000);
conn.setConnectTimeout(15000);
conn.setRequestMethod("POST");
conn.setDoInput(true);
conn.setDoOutput(true);
OutputStream os = conn.getOutputStream();
BufferedWriter writer = new BufferedWriter(
new OutputStreamWriter(os, "UTF-8"));
writer.write(getPostDataString(postDataParams));
writer.flush();
writer.close();
os.close();
int responseCode=conn.getResponseCode();
if (responseCode == HttpsURLConnection.HTTP_OK) {
BufferedReader br=new BufferedReader(new InputStreamReader(conn.getInputStream()));
response = br.readLine();
}
else {
response="Error Registering";
}
} catch (Exception e) {
e.printStackTrace();
response = "not connected";
}
return response;
}
private String getPostDataString(HashMap<String, String> params) throws UnsupportedEncodingException {
StringBuilder result = new StringBuilder();
boolean first = true;
for(Map.Entry<String, String> entry : params.entrySet()){
if (first)
first = false;
else
result.append("&");
result.append(URLEncoder.encode(entry.getKey(), "UTF-8"));
result.append("=");
result.append(URLEncoder.encode(entry.getValue(), "UTF-8"));
}
Log.i("result",result.toString());
return result.toString();
}
}
我得到了答案。我在我的 SongData Class onPostExecute
mehod
String newStr = s.replace("\'", "");
register(date_today, device, newStr, "http://192.168.1.102/song.php");