Jump to content

problems inserting in database...


almightyegg

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.....
Link to comment
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]
Link to comment
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  ???
Link to comment
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
Link to comment
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]
Link to comment
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.