我正在尝试将我的android应用程序与本地服务器上的数据库连接起来。它实际上正在连接,但当它试图执行插入数据库时,我收到了错误:
Notice: Undefined index: user_login in C:wampwwwwebappregister.php on line 4
Notice: Undefined index: user_password in C:wampwwwwebappregister.php on line 5
Notice: Undefined index: user_phonenumber in C:wampwwwwebappregister.php on line 6
Notice: Undefined index: user_email in C:wampwwwwebappregister.php on line 7
在我的register.php上,我有
require "init.php";
$user_login = $_POST["user_login"];
$user_password = $_POST["user_password"];
$user_phonenumber = $_POST["user_phonenumber"];
$user_email = $_POST["user_email"];
$sql_query = "insert into user_db values('$user_login','$user_password','$user_phonenumber','$user_email');";
安卓代码:
URL url = new URL(reg_url);
HttpURLConnection httpURLConnection = (HttpURLConnection)url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
OutputStream OS = httpURLConnection.getOutputStream();
Log.d("in background", " connected");
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(OS,"UTF-8"));
String data = URLEncoder.encode("user_login","UTF-8") + "="+URLEncoder.encode(Loginstr,"UTF-8")+"&"+
URLEncoder.encode("user_password","UTF-8") + "="+URLEncoder.encode(Passwordstr,"UTF-8")+"&"+
URLEncoder.encode("user_phonenumber","UTF-8") + "="+URLEncoder.encode(Numberstr,"UTF-8")+"&"+
URLEncoder.encode("user_email","UTF-8") + "="+URLEncoder.encode( Emailstr,"UTF-8");
bufferedWriter.write(data);
bufferedWriter.flush();
bufferedWriter.close();
OS.close();
InputStream IS = httpURLConnection.getInputStream();
IS.close();
我还会问如何制作一件东西。在我的数据库中,我创建了一个由新用户填写的字段,并创建了一个子字段来保存用户的唯一id。我的问题是:在创建新用户后,这个id字段会与与用户相关的新uniqe id一起存档吗?(已标记AUTO_INCREMENT
选项)。如果没有,我该怎么做才能做出这样的东西。
我对SQL和PHP真的很陌生,所以我希望我已经正确地解释了一切。提前感谢
首先,您不会收到"错误",但会收到"通知"。
您可以使用以下行禁用通知显示:
ini_set('error_reporting', E_ALL ^ E_NOTICE);
对于开发用途,最好将错误报告设置为"在页面上"显示错误:
ini_set('display_errors', true);
但对于生产,这应该设置为false,并在其他地方记录错误。
其次,如果您的MySQL表有一个带有AUTO_INCREMENT
的主键,那么您的插入将自动生成一个Id。您可以使用LAST_INSERT_ID()
MySQL函数(或等效的PHP函数,取决于您使用的是mysqli还是PDO)来访问它。