Forum Moderators: coopster

Message Too Old, No Replies

Head from trying

         

l33tj0rd4n

6:50 pm on Feb 15, 2011 (gmt 0)

10+ Year Member



I've been working on this all day

Ok, heres the code, I'm trying to write to a database...
I have tried doing double quotes, angled single quotes, single quotes etc, but it's not working...


The error:
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 '::1, fgdhgshgdshsfdhdsfhdsfhsdfhsdfhdhsdf, ghdsfhsdfhdsf, 0, 15-02-2011, 18:06pm' at line 1



The code:
$i++;
$username = $_SESSION['username'];
$ip = $_SERVER['REMOTE_ADDR'];
$content = mysql_real_escape_string($_POST['postContent']);
$title = mysql_real_escape_string($_POST['title']);
$locked = 0;
$date = date("d-m-Y");
$time = date("G:ha");

mysql_query("INSERT INTO `topics` (`id`, `author`, `ip`, `content`, `title`, `locked`, `date`, `time`) VALUES ($i, $username, $ip, $content, $title, $locked, $date, $time") or die(mysql_error());

Matthew1980

7:00 pm on Feb 15, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi there l33tj0rd4n,

Welcome to WebmasterWorld!

Echo the query out so that you can see how it's being populated, then copy and paste that into your preferred sql browser (phpmyadmin or sql query browser ;)) and debug it from there to see what's what.

I have reformatted this for you as there is no need to quote numerical values when in a sql query as this will cause error's along the way.

$query = "INSERT INTO `topics` (`id`, `author`, `ip`, `content`, `title`, `locked`, `date`, `time`)
VALUES
(".$i.", '".$username."', '".$ip."', '".$content."', '".$title."', ".$locked.", '".$date."', '".$time."') ";

echo $query;
$result = mysql_query($query) or die(mysql_error());

Just to note, if the 'id' is set to auto increment there is noo need to set an incrementing value for this field, you can leave it out all together, php/sql engines will do this automatically for you.

See how that goes. Also, I think as you were missing a closing bracket from the VALUES part of the query...

Cheers,
MRb

l33tj0rd4n

7:08 pm on Feb 15, 2011 (gmt 0)

10+ Year Member



Okay.
INSERT INTO `topics` (`id`, `author`, `ip`, `content`, `title`, `locked`, `date`, `time`) VALUES (3, 'Jordan', '::1', 'Message Hi, Test', 'Title', 0, '15-02-2011', '19:07pm')

Got echo'd. Although, when I checked in my database it shows NO date was inputted...
The script I am using for my date/time checks is this:
date_default_timezone_set('Europe/London'); // Stopping the errors..
$usersZone = new usersZone;
date_default_timezone_set($usersZone->getLocalTimezone());


Then in usersZone:

<?php
class usersZone {
function getLocalTimezone(){
$iTime = time();
$arr = localtime($iTime);
$arr[5] += 1900;
$arr[4]++;
$iTztime = gmmktime($arr[2], $arr[1], $arr[0], $arr[4], $arr[3], $arr[5], $arr[8]);
$offset = doubleval(($iTztime-$iTime)/(60*60));
$zonelist = array(
'Kwajalein' => -12.00,
'Pacific/Midway' => -11.00,
'Pacific/Honolulu' => -10.00,
'America/Anchorage' => -9.00,
'America/Los_Angeles' => -8.00,
'America/Denver' => -7.00,
'America/Tegucigalpa' => -6.00,
'America/New_York' => -5.00,
'America/Caracas' => -4.30,
'America/Halifax' => -4.00,
'America/St_Johns' => -3.30,
'America/Argentina/Buenos_Aires' => -3.00,
'America/Sao_Paulo' => -3.00,
'Atlantic/South_Georgia' => -2.00,
'Atlantic/Azores' => -1.00,
'Europe/Dublin' => 0,
'Europe/Belgrade' => 1.00,
'Europe/Minsk' => 2.00,
'Asia/Kuwait' => 3.00,
'Asia/Tehran' => 3.30,
'Asia/Muscat' => 4.00,
'Asia/Yekaterinburg' => 5.00,
'Asia/Kolkata' => 5.30,
'Asia/Katmandu' => 5.45,
'Asia/Dhaka' => 6.00,
'Asia/Rangoon' => 6.30,
'Asia/Krasnoyarsk' => 7.00,
'Asia/Brunei' => 8.00,
'Asia/Seoul' => 9.00,
'Australia/Darwin' => 9.30,
'Australia/Canberra' => 10.00,
'Asia/Magadan' => 11.00,
'Pacific/Fiji' => 12.00,
'Pacific/Tongatapu' => 13.00);
$index = array_keys($zonelist, $offset);
if(sizeof($index)!=1)
return false;
return $index[0];
}
}
?>


Thanks for your fast reply,
Jordan.

l33tj0rd4n

7:09 pm on Feb 15, 2011 (gmt 0)

10+ Year Member



What was inputted was this:
3Jordan::1Message Hi, TestTitle00000-00-0019:07:00

Matthew1980

7:21 pm on Feb 15, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, that shows as it is just doing it's default time stamp format of 0000-00-00, what I am curious to is this: if you are logging the time of this being inserted, why not use the NOW() function that is built into sql, then you wouldn't need to generate something outside of the sql. Just a thought there.

Have you tried copying and pasting that echoed sql into your sql browser yet? that may yield something, other than that, this is something I haven't seen happen before. Then again, I could be missing something really simple ;)

Cheers,
MRb

l33tj0rd4n

7:22 pm on Feb 15, 2011 (gmt 0)

10+ Year Member



It is inserting it now, just not showing the date in PHPMyAdmin, and yes, I have, and it worked?

Matthew1980

7:58 pm on Feb 15, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ok, so, what's your latest piece of code look like for preparing the insert & what's the issue at present - comically I am doing this in VB.net as we speak! Though I can't promise a concise answer..

Hopefully it can be resolved though..

Cheers,
MRb