Jump to content

Archived

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

StirCrazy

Insert into database

Recommended Posts

Again sorry my code isn't exactly neat and tidy (still learning) *sorry*.

Got a problem that I can't work out ~

Trying to allow users of our website to directly email each other without disclosing their email addresses.

I'm trying to store all outgoing messages into a mysql database...

....can't work out why none of the data will insert into the table.

Any ideas?


Below is the relevent data (btw the sql function works fine: the whole site uses it)

[code]function contact2() {

  global $site_prefix;
  global $site_title; 
 
  $send_to_user = $_POST['sendto'];
  $msg = $_POST['msg'];     

  $r = my_query("select username,email from central_users where id='" .
  mysql_real_escape_string($_SESSION['user']) . "'");
  list($username,$from) = mysql_fetch_row($r);
 
  $r = my_query("select email from central_users where username='" .
  mysql_real_escape_string($send_to_user) . "'");
  list($to) = mysql_fetch_row($r); 


  $r = my_query("insert into email_reports (site,send_to_user,from,message,email,timedate) values(
    '" . mysql_real_escape_string($site_prefix) . "',
    '" . mysql_real_escape_string($send_to_user) . "',
    '" . mysql_real_escape_string($username) . "',
    '" . mysql_real_escape_string($msg) . "',
    '" . mysql_real_escape_string($to) . "',
    '" . get_time() . "')");


  $subject = "New Private Message has arrived from " . $username;
  $headers = "From: $from\nReply-To: $from\nContent-Type: text/plain\nContent-Transfer-Encoding: 8bit";
  $body = stripslashes($msg);

  @mail($to,$subject,$body,$headers);

  ?>
 
<br><br>
<span class="PlainText">Your message has been sent!</span>
<br><br>

  <?php

  return(0);
}[/code]


[quote]CREATE TABLE `email_reports` (
  `id` int(11) unsigned NOT NULL auto_increment,
  `site` varchar(255) default NULL,
  `send_to_user` varchar(255) default NULL,
  `from` varchar(255) default NULL,
  `message` text,
  `email` varchar(255) default NULL,
  `timedate` int(11) NOT NULL default '0',
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;[/quote]


Thanks for your help guys - dunno what i'd do with you lot.

S.C>

Share this post


Link to post
Share on other sites
Just at a glance, shouldn't you be using mysql_query and not my_query?  I searched on php.net and my_query didn't show up

Share this post


Link to post
Share on other sites
nah my_query is one of my functions ~ is actually included when this is run (sorry just forgot to add the include just above function contact2() .

function my_query($sql) {
  global $link;
  $result = @mysql_query($sql, $link);
  if ($result) return $result;
  if (DEBUG_MODE) { error_report(10); };

  return false;
}

Share this post


Link to post
Share on other sites
I'm guessing that the error lies here:-
[code]  $r = my_query("insert into email_reports (site,send_to_user,from,message,email,timedate) values(
    '" . mysql_real_escape_string($site_prefix) . "',
    '" . mysql_real_escape_string($send_to_user) . "',
    '" . mysql_real_escape_string($username) . "',
    '" . mysql_real_escape_string($msg) . "',
    '" . mysql_real_escape_string($to) . "',
    '" . get_time() . "')"); [/code]
and not in the way the database table is set up?

S.C>

Share this post


Link to post
Share on other sites
instead of this:
[code]
$r = my_query("insert into email_reports (site,send_to_user,from,message,email,timedate) values(
    '" . mysql_real_escape_string($site_prefix) . "',
    '" . mysql_real_escape_string($send_to_user) . "',
    '" . mysql_real_escape_string($username) . "',
    '" . mysql_real_escape_string($msg) . "',
    '" . mysql_real_escape_string($to) . "',
    '" . get_time() . "')");
[/code]
do this:
[code]
$sql = "insert into email_reports (site,send_to_user,from,message,email,timedate) values(
    '" . mysql_real_escape_string($site_prefix) . "',
    '" . mysql_real_escape_string($send_to_user) . "',
    '" . mysql_real_escape_string($username) . "',
    '" . mysql_real_escape_string($msg) . "',
    '" . mysql_real_escape_string($to) . "',
    '" . get_time() . "')";
$r = my_query($sql);
[/code]
now, echo $sql and examine it. insert it directly into your db through phpmyadmin or whatever. see if it works there. If it does, then the problem is your code, not the query. post what you find out, including the query string.

Share this post


Link to post
Share on other sites
GENIUS # Thanks Crayon Violent: was because I used FROM!!! DOH!

Thanks for the nudge in the right direction

S.C>


Error
SQL query:

INSERT INTO email_reports( site, send_to_user,
[b][color=red]FROM[/color][/b] , message, email, timedate )
VALUES (

'site1', 'sendto-user', 'stircrazy', 'Hiya,\r\n\r\nPut your message here...\r\n\r\nRegards,\r\n\r\n', 'user@domain.com', '1157586163'
)

MySQL said: 

#1064 - 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,message,email,timedate) values( 'site1', 'sendto-user', 'stircrazy', 'Hiya,\r\n\r\nP' at line 1

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.