Jump to content


Photo

Insert into database


  • Please log in to reply
5 replies to this topic

#1 StirCrazy

StirCrazy
  • Members
  • PipPipPip
  • Advanced Member
  • 75 posts

Posted 06 September 2006 - 04:31 PM

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)

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);
}


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 ;



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

S.C>


#2 Wintergreen

Wintergreen
  • Members
  • PipPipPip
  • Advanced Member
  • 107 posts

Posted 06 September 2006 - 04:35 PM

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

#3 StirCrazy

StirCrazy
  • Members
  • PipPipPip
  • Advanced Member
  • 75 posts

Posted 06 September 2006 - 04:40 PM

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

#4 StirCrazy

StirCrazy
  • Members
  • PipPipPip
  • Advanced Member
  • 75 posts

Posted 06 September 2006 - 05:23 PM

I'm guessing that the error lies here:-
  $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() . "')");
and not in the way the database table is set up?

S.C>

#5 .josh

.josh
  • Staff Alumni
  • .josh
  • 14,871 posts

Posted 06 September 2006 - 05:33 PM

instead of this:
$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() . "')");
do this:
$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);
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.
Did I help you? Feeling generous? Buy me lunch! 
Please, take the time and do some research and find out how much it would have cost you to get your help from a decent paid-for source. A "roll-of-the-dice" freelancer will charge you $5-$15/hr. A decent entry level freelancer will charge you around $15-30/hr. A professional will charge you anywhere from $50-$100/hr. An agency will charge anywhere from $100-$250/hr. Think about all this when soliciting for help here. Think about how much money you are making from the work you are asking for help on. No, we do not expect you to pay for the help given here, but donating a few bucks is a fraction of the cost of what you would have paid, shows your appreciation, helps motivate people to keep offering help without the pricetag, and helps make this a higher quality free-help community :)

#6 StirCrazy

StirCrazy
  • Members
  • PipPipPip
  • Advanced Member
  • 75 posts

Posted 06 September 2006 - 06:15 PM

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,
FROM , 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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users