Jump to content


Photo

problems inserting in database...


  • Please log in to reply
10 replies to this topic

#1 almightyegg

almightyegg
  • Members
  • PipPipPip
  • Advanced Member
  • 791 posts
  • LocationEngland

Posted 25 October 2006 - 04:00 PM

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:
<?
$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)
<? } ?>

the problem is that it wont put the information into the database...it did twice and then i added 2 columns, from and time.....

Visit my Game - Under Construction

#2 almightyegg

almightyegg
  • Members
  • PipPipPip
  • Advanced Member
  • 791 posts
  • LocationEngland

Posted 25 October 2006 - 04:36 PM

UPDATE on situation!!
it inserts the data when code is:
$postinto = mysql_query("INSERT INTO im (id, toid, message, time)
VALUES('$newid', '$toid', '$message', '$time')");
but not when:
$postinto = mysql_query("INSERT INTO im (id, toid, message, from, time)
VALUES('$newid', '$toid', '$message', '$mem[username]', '$time')");
i don't understand why it wont work when putting in
$mem[username]

Visit my Game - Under Construction

#3 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 25 October 2006 - 05:42 PM

Try this:

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

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#4 almightyegg

almightyegg
  • Members
  • PipPipPip
  • Advanced Member
  • 791 posts
  • LocationEngland

Posted 25 October 2006 - 06:03 PM

nope didn't work :(
nothing goes into the database....
Visit my Game - Under Construction

#5 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 25 October 2006 - 06:26 PM

ok, try this:

$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());

Let me know what the error says.

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#6 almightyegg

almightyegg
  • Members
  • PipPipPip
  • Advanced Member
  • 791 posts
  • LocationEngland

Posted 25 October 2006 - 06:29 PM

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  ???
Visit my Game - Under Construction

#7 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 25 October 2006 - 06:39 PM

'from' is a reserved word in MySQL.  Use backticks in your column names.  This should work:

$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());

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#8 almightyegg

almightyegg
  • Members
  • PipPipPip
  • Advanced Member
  • 791 posts
  • LocationEngland

Posted 25 October 2006 - 06:43 PM

yay it worked!!! thanks!!
but another little problem....
when listing the posts from people, it wont show them,.,.

<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>";
}
?>

Visit my Game - Under Construction

#9 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 25 October 2006 - 06:50 PM

It's probably the same problem, try...

$grabposts = mysql_query("SELECT * FROM im WHERE toid = '{$mem['id']}' ORDER BY `time` DESC");

Here's a list of reserved words in MySQL 4.1

Regards
Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#10 Barand

Barand
  • Moderators
  • Sen . ( ile || sei )
  • 18,021 posts

Posted 25 October 2006 - 06:52 PM

use

while($posmes = mysql_fetch_array($grabposts)){
If you are still using mysql_ functions, STOP! Use mysqli_ or PDO. The longer you leave it the more you will have to rewrite.

Donations gratefully received






moon.png

|baaGrid| easy data tables - and more
|baaChart| easy line, column and pie charts

#11 almightyegg

almightyegg
  • Members
  • PipPipPip
  • Advanced Member
  • 791 posts
  • LocationEngland

Posted 25 October 2006 - 06:54 PM

oops! i should have seen that  :-X

cheers both of you :)
Visit my Game - Under Construction




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users