我是PHP的新手,只使用它作为我的Android应用程序的后端。
我有三个字符串,我从我的Android应用程序发送到PHP。我想查询一个名为'users'的表,找到从我的Android App发送的用户名的用户ID,然后将数据插入一个名为'msg'的单独表中。
我已经尝试过我的生活,我无法让它工作,而且我还没有完成。
谢谢并帮助我会非常惊人,因为我是PHP的新手并且无法完成剩下的代码。PHP:
<?php
$username = $_POST['username'];
$msg = $_POST['msg'];
$frienduser = $_POST ['frienduser'];
/*mysql data below */
$dbc = mysql_connect('localhost', 'removemypasswords', 'again');
if(!dbc) {
die("Something went wrong! Try again...");
}
/* select database */
$db_select = mysql_select_db("andagain, $dbc");
if (!db_select){
die("Can't connect :" .mysql_error);
}
$query = mysql_query("SELECT FROM users WHERE usernames ='$usernames'");
$query1 = mysql_query(INSERT INTO `gtanews1_zips54`.`msg` (
`id` ,
`friendid` ,
`msg`
)
VALUES (
'$query', '$frienduser', 'msg'
);
echo ($msg);
?>
答案 0 :(得分:0)
如何在$ query1周围加上引号,如
$query1 = mysql_query("INSERT INTO gtanews1_zips54.msg (`id` ,`friendid` ,`msg`)
VALUES ('$query', '$frienduser', 'msg')");
答案 1 :(得分:0)
应该是
$query = mysql_query("SELECT * FROM users WHERE usernames ='$username'");
$result = mysql_fetch_array($query);
$query1 = mysql_query("INSERT INTO gtanews1_zips54.msg (id,friendid,msg) VALUES ('" . $result['yourField'] . "', '$frienduser','$msg')");
答案 2 :(得分:0)
你的mysql选择db代码是错误的。你需要在逗号之前加上引号
mysql_select_db(“andagain”,$ dbc);
也会在查询末尾加上引号
$query = mysql_query("SELECT FROM users WHERE usernames ='$usernames'"); $query1 = mysql_query(INSERT INTO `gtanews1_zips54`.`msg` ( `id` , `friendid` , `msg` ) VALUES ( '$query', '$frienduser', 'msg' )");
答案 3 :(得分:0)
这里出现了很多问题:
<?php
$username = $_POST['username'];
$msg = $_POST['msg'];
$frienduser = $_POST ['frienduser'];
/*mysql data below */
$dbc = mysql_connect('localhost', 'removemypasswords', 'again');
if(!$dbc) { //- You forgot the dollar $ sign on $dbc
die("Something went wrong! Try again...");
}
/* select database */
$db_select = mysql_select_db("andagain", $dbc); //- You had the entire thing quoted, quotes are just around "andagain"
if (!db_select){
die("Can't connect :" .mysql_error()); //- You forgot the parentheses after mysql_error
}
$query = mysql_query("SELECT FROM users WHERE usernames ='$usernames'");
//- You need to actually get the results out of the query object
$row = mysql_fetch_assoc($query);
if (!$row) {
die('User not found');
}
$user_id = $row['id']; //- Or whatever the column is called
$query1 = mysql_query("INSERT INTO `gtanews1_zips54`.`msg` (
`id` ,
`friendid` ,
`msg`
)
VALUES (
'$user_id', '$frienduser', 'msg'
"); //- You forgot to put quotes around this query
echo ($msg);
?>
而这只是为了开始,可能还有其他问题,具体取决于您的数据库架构/数据传输格式。
此外,您对SQL注入非常开放。
答案 4 :(得分:0)
您的代码有很多错误。
- $ db_select = mysql_select_db(andagain,$ dbc);
- $ query = mysql_query('SELECT FROM users WHERE usernames =“$ usernames”');
醇>
因为Stackoverflow不是修复代码错误的社区..所以我将离开这份工作。
以下是一些可以帮助您修复所有错误的要点?