我一直在做这个数据库一整晚,当我从Android应用程序下订单时,我无法弄清楚为什么我在表格购物车中为订单ID保持0。插入到两个表中的东西没有问题,只是对于cart表中的orderid,它总是在表中返回0。
车
<?php
require "connection.php";
session_start();
$foodname=$_POST["food_name"];
$cartquantity=$_POST["cart_quantity"];
$id=$_POST["id"];
$pickuptime=$_POST["pick_up_time"];
$orderid= $_SESSION["last_id"];
$mysqli_qry="insert into cart (orderid,foodname,cartquantity,id,pickuptime)
values ('$orderid','$foodname','$cartquantity','$id','$pickuptime')";
if ($conn->query($mysqli_qry,$mysqli_qry1)==TRUE){
echo "Insert Successful";
}
else {
echo "Error:".$mysqli_qry."<br>".$conn->error;
}
?>
顺序
<?php
$hostname="localhost";
$username="root";
$password="";
$database="";
$conn = new PDO("mysql:host=$hostname;dbname=$database", $username,
$password);
$conn->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
session_start();
$id=$_POST["id"];
$pickuptime=$_POST["pick_up_time"];
$pickupdate=$_POST["pick_up_date"];
$amount=$_POST["amount"];
$mysqli_qry="insert into `order` (`id`,`pickuptime`,`pickupdate`,`amount`)
values ($id,$pickuptime,$pickupdate,$amount)";
$conn->exec($mysqli_qry);
$_SESSION["last_id"] == $conn->lastInsertId();
?>
更新 对于我的Android部分
public class BackgroundWorker extends AsyncTask<String,Void,String> {
Context context;
AlertDialog alertDialog;
BackgroundWorker (Context ctx){
context=ctx;
}
@Override
protected String doInBackground(String...params){
String type=params[0];
String order_url="http://192.168.28.1/restaurant/orderdb.php";
String cart_url="http://192.168.28.1/restaurant/cartdb.php";
if (type.equals("order")){
try{
String id=params[1];
String pick_up_time=params[2];
String pick_up_date=params[3];
String amount=params[4];
URL url=new URL(order_url);
HttpURLConnection httpURLConnection=(HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream=httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
String post_data= URLEncoder.encode("id","UTF-8")+"="+URLEncoder.encode(id,"UTF-8")+"&"+URLEncoder.encode("pick_up_time","UTF-8")+"="+URLEncoder.encode(pick_up_time,"UTF-8")+"&"+URLEncoder.encode("pick_up_date","UTF-8")+"="+URLEncoder.encode(pick_up_date,"UTF-8")+"&"+URLEncoder.encode("amount","UTF-8")+"="+URLEncoder.encode(amount,"UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream=httpURLConnection.getInputStream();
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
String result="";
String line="";
while((line=bufferedReader.readLine())!=null){
result+=line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
}catch(MalformedURLException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
}
else if (type.equals("cart")){
try{
String food_name=params[1];
String cart_quantity=params[2];
String id=params[3];
String pick_up_time=params[4];
URL url=new URL(cart_url);
HttpURLConnection httpURLConnection=(HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream=httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter=new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
String post_data= URLEncoder.encode("food_name","UTF-8")+"="+URLEncoder.encode(food_name,"UTF-8")+"&"+URLEncoder.encode("cart_quantity","UTF-8")+"="+URLEncoder.encode(cart_quantity,"UTF-8")+"&"+URLEncoder.encode("id","UTF-8")+"="+URLEncoder.encode(id,"UTF-8")+"&"+URLEncoder.encode("pick_up_time","UTF-8")+"="+URLEncoder.encode(pick_up_time,"UTF-8");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream=httpURLConnection.getInputStream();
BufferedReader bufferedReader=new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
String result="";
String line="";
while((line=bufferedReader.readLine())!=null){
result+=line;
}
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
}catch(MalformedURLException e){
e.printStackTrace();
}catch (IOException e){
e.printStackTrace();
}
}
return null;
}
答案 0 :(得分:0)
您未在orderid
中定义params
变量,post_data
中缺少该变量。