I'm using java to read from an excel file and put that data into variables, then send those variables to php where it will get processed and inserted into a mysql database. The database wont allow me to directly insert into the database, so I'm doing a work-around. I get this error when I run my program. "Server returned HTTP response code: 500 for URL:" This happens when it gets to the conn.getOutputStream(); If someone knows how i can fix this on the server side please let me know so that I can try to fix it
String link="http://website/directory/file.php";
String data = URLEncoder.encode("fName", "UTF-8")
+ "=" + URLEncoder.encode(fName, "UTF-8");
data += "&" + URLEncoder.encode("lName", "UTF-8")
+ "=" + URLEncoder.encode(lName, "UTF-8");
data += "&" + URLEncoder.encode("SID", "UTF-8")
+ "=" + URLEncoder.encode(SID+"", "UTF-8");
data += "&" + URLEncoder.encode("email", "UTF-8")
+ "=" + URLEncoder.encode(email, "UTF-8");
data += "&" + URLEncoder.encode("major", "UTF-8")
+ "=" + URLEncoder.encode(major, "UTF-8");
data += "&" + URLEncoder.encode("year", "UTF-8")
+ "=" + URLEncoder.encode(year, "UTF-8");
data += "&" + URLEncoder.encode("isPaid", "UTF-8")
+ "=" + URLEncoder.encode(isPaid+"", "UTF-8");
data += "&" + URLEncoder.encode("isNational", "UTF-8")
+ "=" + URLEncoder.encode(isNational+"", "UTF-8");
URL url = new URL(link);
//Did the CookieHandler just in case the server wants them
CookieHandler.setDefault(new CookieManager(null, CookiePolicy.ACCEPT_ALL));
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setDoOutput(true);
OutputStreamWriter wr = new OutputStreamWriter(conn.getOutputStream());
wr.write( data );
System.out.println("data was passed");
wr.flush();
BufferedReader reader = new BufferedReader(new InputStreamReader(conn.getInputStream()));
StringBuilder sb = new StringBuilder();
String line = null;
// Read Server Response I'm using echo . ie: echo "yes";
while((line = reader.readLine()) != null)
{
sb.append(line);
break;
}
System.out.println(sb.toString());
}
if(sb.toString().equals("yes"))
{
ttlAffected++;
}
else if(sb.toString().equals("no"))
{
System.out.println("Student ID: "+array.get(rowOn).getSID()+" already exists");
fail.add(array.get(rowOn).getSID());
}
else if(sb.toString().equals("error"))
{
System.out.println("An unknown error occured");
fail.add(array.get(rowOn).getSID());
}
else
{
System.out.println("IDk what happened "+ sb.toString());
}
Also, here is the php side
<?php
$con=mysqli_connect("host","username","password","database");
if (mysqli_connect_errno($con))
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$_SESSION['fName']=$_POST['fName'];
$_SESSION['lName']=$_POST['lName'];
$_SESSION['SID']=$_POST['SID'];
$SID=$_SESSION['SID']
$_SESSION['email']=$_POST['email'];
$_SESSION['major']=$_POST['major'];
$_SESSION['year']=$_POST['year'];
$_SESSION['isNational']=$_POST['isNational'];
$_SESSION['isPaid']=$_POST['isPaid'];
$sql ="SELECT * FROM Registered_Members15 WHERE SID='$SID'";
$result = mysqli_query($com,$sql);
if(mysqli_num_rows($result)==0)
{
$query_member = "INSERT INTO Events15(id,SID) VALUES (?, ?)";
$stmt_member = mysqli_prepare($con,$query_member);
mysqli_stmt_bind_param($stmt_member, "si" , $_SESSION['email'], $_SESSION['SID']);
mysqli_stmt_execute($stmt_member);
$affected_rows_mem = mysqli_stmt_affected_rows($stmt_member);
mysqli_stmt_close($stmt_member);
$query = "INSERT INTO Registered_Members15(ID,first_name,last_name,SID,email,paid_chapter,paid_national,major,year) VALUES (NULL,?, ?, ?, ?, ?, ?, ?, ?)";
$stmt = mysqli_prepare($con, $query);
mysqli_stmt_bind_param($stmt, "ssisiiss", $_SESSION['fName'], $_SESSION['lName'], $_SESSION['SID'], $_SESSION['email'], $_SESSION['isPaid'], $_SESSION['isNational'], $_SESSION['major'], $_SESSION['year']);
mysqli_stmt_execute($stmt);
$affected_rows = mysqli_stmt_affected_rows($stmt);
if($affected_rows == 1 && $affected_rows_mem == 1)
{
echo "yes";
}
else
{
echo "error";
}
}else{
echo "no";
}
mysqli_close($con);
?>
I believe this is all right but I do want to ask if someone could check if i did something wrong.
via Chebli Mohamed
Aucun commentaire:
Enregistrer un commentaire