Skip to content Skip to sidebar Skip to footer

Mysql Insert Statement And Passing Parameters To Php

I am trying to execute various statements/queries in my PHP file, depending on what variables are received (moreover, is there a way to send variables and choose what kind of query

Solution 1:

Since this PHP script does an INSERT statement, it's strongly recommended that you use POST parameters.

Java code:

public String connect() {

    try {
    /*URL url = new URL("phpfilelocation.php");*/Stringdata="&" + URLEncoder.encode("un", "UTF-8") + "=" + URLEncoder.encode(un, "UTF-8");
        data += "&" + URLEncoder.encode("pw", "UTF-8") + "=" + URLEncoder.encode(pw, "UTF-8");
        data += "&" + URLEncoder.encode("uid", "UTF-8") + "=" + URLEncoder.encode(Integer.toString(uid), "UTF-8");

        URLurl=newURL("phpfilelocation.php");
        HttpURLConnectionconn= (HttpURLConnection) url.openConnection();

        conn.setDoOutput(true);
        conn.setRequestMethod("POST");
        conn.connect();

        OutputStreamWriterwr=newOutputStreamWriter(conn.getOutputStream());
        wr.write(data);
        wr.flush();

        BufferedReaderreader=newBufferedReader(newInputStreamReader(conn.getInputStream()));

        StringBuildersb=newStringBuilder();
        Stringline=null;

        // Read Server Responsewhile((line = reader.readLine()) != null)
        {
            sb.append(line);
            break;
        }
        return sb.toString();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return"fail";
}

PHP script:

<?php$con=mysqli_connect("logindetailsgohere");

if (mysqli_connect_errno($con))
{
echo"Failed to connect to MySQL: " . mysqli_connect_error();
}

if (isset($_POST['un']) && isset($_POST['pw']) && isset($_POST['uid'])){
$uname = mysql_real_escape_string($_POST['un']);
$pass = mysql_real_escape_string($_POST['pw']);
$uid = intval($_POST['uid']);
$sql = "INSERT IGNORE INTO Users (id, Username, Password) VALUES ('$uid', '$uname', '$pass')";
if(mysqli_query($con, $sql){
    echo"Values have been inserted";
}
}

mysqli_close($con);
?>

As an aside, for a GET request, you just append the parameters to the end of the url when you create the URL object.

public String connect() {

    try {
    /*URL url = new URL("phpfilelocation.php");*/Stringdata= URLEncoder.encode("un", "UTF-8") + "=" + URLEncoder.encode(un, "UTF-8");
        data += "&" + URLEncoder.encode("pw", "UTF-8") + "=" + URLEncoder.encode(pw, "UTF-8");
        data += "&" + URLEncoder.encode("uid", "UTF-8") + "=" + URLEncoder.encode(Integer.toString(uid), "UTF-8");

        StringurlString="phpfilelocation.php" + "?" + data;
        URLurl=newURL(urlString);
        HttpURLConnectionconn= (HttpURLConnection) url.openConnection();

        conn.setDoOutput(false);
        conn.setRequestMethod("GET");
        conn.connect();

        //remove://OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());//wr.write(data);//wr.flush();BufferedReaderreader=newBufferedReader(newInputStreamReader(conn.getInputStream()));

        StringBuildersb=newStringBuilder();
        Stringline=null;

        // Read Server Responsewhile((line = reader.readLine()) != null)
        {
            sb.append(line);
            break;
        }
        return sb.toString();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
    return"fail";
}

Solution 2:

PHP :

mysql_query("INSERT INTO Users(id, UserName, Password)
VALUES ('$uid', '$uname', '$pass')");

JAVA :

String driver="com.mysql.jdbc.Driver";
String url="jdbc:mysql://localhost:3306/dbname";
String unameDB="username";
String passDB="password";
Class.forName(driver);
Connection c=(Connection) DriverManager.getConnection(url,unameDB,passDB);
Statement s=c.createStatement();
s.executeUpdate("INSERT INTO `Users`(ID,UserName,Password) VALUE ('"+uid+"','"+uname+"','"+pass+"')");

Post a Comment for "Mysql Insert Statement And Passing Parameters To Php"