Jump to content

Archived

This topic is now archived and is closed to further replies.

almightyegg

problems inserting in database...

Recommended Posts

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.....

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Try this:

[code=php:0]
$postinto = mysql_query("INSERT INTO im (id, toid, message, from, time)
VALUES('$newid', '$toid', '$message', '{$mem['username']}', '$time')");
[/code]

Regards
Huggie

Share this post


Link to post
Share on other sites
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.

Regards
Huggie

Share this post


Link to post
Share on other sites
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 1

the funny thing is that it is recognising the $mem[username] = Lord of the Abyss  ???

Share this post


Link to post
Share on other sites
'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]

Regards
Huggie

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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]

Regards
Huggie

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.