$from = $_POST['from'];
$to = $_POST['to'];
$message = $_POST['message'];
$query = "SELECT * FROM Users WHERE `user_name` = '$from' LIMIT 1";
$result = mysql_query($query);
while($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
$fromID = $row['user_id'];
}
I'm trying to have $formID be the user_id for a user in my database. Each row in the Users table is like:
user_id | user_name | user_type
1 | Hristo | Agent
So I want $from = 1
but the above开发者_运维百科 code isn't working. Any ideas why?
Try this:
$from = mysql_real_escape_string($_POST['from']);
$to = mysql_real_escape_string($_POST['to']);
$message = mysql_real_escape_string($_POST['message']);
$query = "SELECT * FROM Users WHERE user_name = '$from' LIMIT 1";
$result = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_assoc($result)) {
$fromID = $row['user_id'];
}
Also, make sure that:
- You have connected to the database
- You do get data from the post, try
var_dump
with your vars egvar_dump($from)
Use mysql_fetch_assoc instead
while($row =
mysql_fetch_assoc($result)){ $fromID = $row['user_id'];
}
though it should. try this code
$from = mysql_real_escape_string($_POST['from']);
$to = mysql_real_escape_string($_POST['to']);
$message = mysql_real_escape_string($_POST['message']);
$query = "SELECT * FROM Users WHERE `user_name` = '$from' LIMIT 1";
$result = mysql_query($query) or trigger_error(mysql_error().$query);
$row = mysql_fetch_array($result, MYSQL_ASSOC);
$fromID = $row['user_id'];
echo $fromID;
if it will throw no errors but still print no id, add this line
var_dump($row);
and post here it's output
not that you shouldn't use a user name but user id to address particular user.
精彩评论