almightyegg Posted October 25, 2006 Share Posted October 25, 2006 i want to have an instant message system, but i cannot figure out why this bit isnt working....i have a form:<form id="sendmess" name="sendmess" method="post" action="post.php"><p>Account ID: <input name="toid" type="text"><br>Message:<br><textarea name="message" rows=7 cols=40></textarea><input type="submit" name="Submit" value="Find Account"></p></form>and main code on post.php:[code=php:0]<?$pos = mysql_query("SELECT * FROM im WHERE id='$id'") or die(mysql_error());$posmes = mysql_fetch_array($pos);$message = $_POST['message'];$toid = $_POST['toid'];$time = date("Y-m-d H:i:s");$mem[username] = $username;if($mem[id] == $toid){echo "You can't send a message to yourself!";}else{$postinto = mysql_query("INSERT INTO im (id, toid, message, from, time)VALUES('$newid', '$toid', '$message', '$username', '$time')");?>Your message was sent to...(will add later)<? } ?>[/code]the problem is that it wont put the information into the database...it did twice and then i added 2 columns, from and time..... Quote Link to comment https://forums.phpfreaks.com/topic/25071-problems-inserting-in-database/ Share on other sites More sharing options...
almightyegg Posted October 25, 2006 Author Share Posted October 25, 2006 UPDATE on situation!!it inserts the data when code is:[code=php:0]$postinto = mysql_query("INSERT INTO im (id, toid, message, time)VALUES('$newid', '$toid', '$message', '$time')");[/code]but not when:[code=php:0]$postinto = mysql_query("INSERT INTO im (id, toid, message, from, time)VALUES('$newid', '$toid', '$message', '$mem[username]', '$time')");[/code]i don't understand why it wont work when putting in [code=php:0]$mem[username][/code] Quote Link to comment https://forums.phpfreaks.com/topic/25071-problems-inserting-in-database/#findComment-114281 Share on other sites More sharing options...
HuggieBear Posted October 25, 2006 Share Posted October 25, 2006 Try this:[code=php:0]$postinto = mysql_query("INSERT INTO im (id, toid, message, from, time)VALUES('$newid', '$toid', '$message', '{$mem['username']}', '$time')");[/code]RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/25071-problems-inserting-in-database/#findComment-114326 Share on other sites More sharing options...
almightyegg Posted October 25, 2006 Author Share Posted October 25, 2006 nope didn't work :(nothing goes into the database.... Quote Link to comment https://forums.phpfreaks.com/topic/25071-problems-inserting-in-database/#findComment-114337 Share on other sites More sharing options...
HuggieBear Posted October 25, 2006 Share Posted October 25, 2006 ok, try this:[code=php:0]$postinto = mysql_query("INSERT INTO im (id, toid, message, from, time)VALUES('$newid', '$toid', '$message', '{$mem['username']}', '$time')") or die ("Can't insert: " . mysql_error());[/code]Let me know what the error says.RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/25071-problems-inserting-in-database/#findComment-114355 Share on other sites More sharing options...
almightyegg Posted October 25, 2006 Author Share Posted October 25, 2006 how odd...Can't insert: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'from, time) VALUES('', '17', 'hbegf', 'Lord of the Abyss', '2006-10-25 13:32:15'' at line 1the funny thing is that it is recognising the $mem[username] = Lord of the Abyss ??? Quote Link to comment https://forums.phpfreaks.com/topic/25071-problems-inserting-in-database/#findComment-114358 Share on other sites More sharing options...
HuggieBear Posted October 25, 2006 Share Posted October 25, 2006 'from' is a [url=http://dev.mysql.com/doc/refman/4.1/en/reserved-words.html]reserved word[/url] in MySQL. Use backticks in your column names. This should work:[code=php:0]$postinto = mysql_query("INSERT INTO im (`id`, `toid`, `message`, `from`, `time`)VALUES('$newid', '$toid', '$message', '{$mem['username']}', '$time')") or die ("Can't insert: " . mysql_error());[/code]RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/25071-problems-inserting-in-database/#findComment-114362 Share on other sites More sharing options...
almightyegg Posted October 25, 2006 Author Share Posted October 25, 2006 yay it worked!!! thanks!!but another little problem....when listing the posts from people, it wont show them,.,.[code=php:0]<table border=0><tr bgcolor=#101010><td width=100>From</td><td width=150>Preview</td><td width=150>Time Sent</td></tr><?$grabposts = mysql_query("SELECT * FROM im WHERE toid = '{$mem['id']}' order by time desc");while($messages = mysql_fetch_array($grabposts)){echo "<tr bgcolor=#222222><td>$posmes[from]</td><td>$posmes[message]</td><td>$posmes[time]</td></tr>";}?>[/code] Quote Link to comment https://forums.phpfreaks.com/topic/25071-problems-inserting-in-database/#findComment-114364 Share on other sites More sharing options...
HuggieBear Posted October 25, 2006 Share Posted October 25, 2006 It's probably the same problem, try...[code=php:0]$grabposts = mysql_query("SELECT * FROM im WHERE toid = '{$mem['id']}' ORDER BY `time` DESC");[/code]Here's a list of [url=http://dev.mysql.com/doc/refman/4.1/en/reserved-words.html]reserved words in MySQL 4.1[/url]RegardsHuggie Quote Link to comment https://forums.phpfreaks.com/topic/25071-problems-inserting-in-database/#findComment-114368 Share on other sites More sharing options...
Barand Posted October 25, 2006 Share Posted October 25, 2006 usewhile($posmes = mysql_fetch_array($grabposts)){ Quote Link to comment https://forums.phpfreaks.com/topic/25071-problems-inserting-in-database/#findComment-114369 Share on other sites More sharing options...
almightyegg Posted October 25, 2006 Author Share Posted October 25, 2006 oops! i should have seen that :-Xcheers both of you :) Quote Link to comment https://forums.phpfreaks.com/topic/25071-problems-inserting-in-database/#findComment-114370 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.