Today's Posts Follow Us On Twitter! TFL Members on Twitter  
Forum search: Advanced Search  
Navigation
Marketplace
  Members Login:
Lost password?
  Forum Statistics:
Forum Members: 24,254
Total Threads: 80,792
Total Posts: 566,472
There are 1842 users currently browsing (tf).
 
  Our Partners:
 
  TalkFreelance     Design and Development     Programming     PHP and MySQL :

mysql error when inserting data

Thread title: mysql error when inserting data
Closed Thread  
Page 1 of 2 1 2 >
    Thread tools Search this thread Display Modes  
12-08-2007, 08:23 PM
#1
Nightscream is offline Nightscream
Status: Junior Member
Join date: Aug 2006
Location:
Expertise:
Software:
 
Posts: 58
iTrader: 0 / 0%
 

Nightscream is on a distinguished road

  Old  mysql error when inserting data

When I try to add some data from a form to my database I get this 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,Admin,1,qsdfqsdf,1197145012,qsdfqsdf,sqdfqsdfsd ,qsdf,qsdf,qsf,qsdf,qsdf' at line 1
this were my insert lines, second one is displayed on error

INSERT INTO contestthread VALUES ,qsdfqsdf,qsdfqsdf,1,1,1197145012,1,,,,Admin,1,Adm in,1180130400,300,1
INSERT INTO contestpost VALUES ,1,Admin,1,qsdfqsdf,1197145012,qsdfqsdf,sqdfqsdfsd ,qsdf,qsdf,qsf,qsdf,qsdf
my php
PHP Code:
foreach($_POST as $key => $value) {
        $
$key $value;
    }
    
    
// Thread variables
    
$username "Admin";
    
$uid 1;
    
$firstpostid get_lastPostID();
    
    
// Post variables
    
$threadID get_threadID();
    
    
// Add slashes to have no mistakes with php functions.
    
AddSlashes($Title);
    
AddSlashes($Sub);
    
AddSlashes($Description);
    
AddSlashes($Website);
    
AddSlashes($Tagline);
    
AddSlashes($Color);
    
AddSlashes($Style);
    
AddSlashes($Formats);
    
    
$cvalues = array( ''// thread id
        
$Title// Title
        
$Sub// sub-title
        
$firstpostid//id of first post in topic
        
$firstpostid//id of last post
        
strtotime('now'), //date in sec of last post
        
$CatID// category id
        
''// open(0 = yes | no = 1)
        
''// comments
        
''// entries
        
$username// username of starter
        
$uid// userid of starter
        
$username// lastposter name
        
strtotime($Length), // Length of contest
        
$Prize// prize
        
$Payment // payment
    
);
    
    
$pvalues = array( ''//post id
        
$threadID// thread id
        
$username// username
        
$uid// user id
        
$Title// title of thread
        
strtotime('now'), //time of post in sec
        
$Summary// summary
        
$Description// message
        
$Website// website
        
$Tagline// tagline
        
$Color// color
        
$Style// style
        
$Formats // formats
    
);
    
    
$contest "INSERT INTO contestthread VALUES ".implode(','$cvalues);
    
    
$post "INSERT INTO contestpost VALUES ".implode(','$pvalues);
    
    
$result1 mysql_query($contest);
    
$result2 mysql_query($post);
    
    if(
$result1 && mysql_num_rows($result1) != || $result2 && mysql_num_rows($result2) != 0) {
        echo 
'Uploaded';
    }else {
        echo 
"Sorry there has been an error, try again.<br />";
        echo 
mysql_error()."<br />".$contest."<br />".$post;
    } 
I'm not getting my problem, I looked at my database and everything looked fine too me.

12-08-2007, 08:55 PM
#2
Salathe is offline Salathe
Salathe's Avatar
Status: Community Archaeologist
Join date: Jul 2004
Location: Scotland
Expertise: Software Development
Software: vim, PHP
 
Posts: 3,820
iTrader: 25 / 100%
 

Salathe will become famous soon enough

Send a message via MSN to Salathe

  Old

Your SQL queries are not formatted properly.

Incorrect
Code:
INSERT INTO contestpost VALUES
 ,1,Admin,1,qsdfqsdf,1197145012,qsdfqsdf,sqdfqsdfsd ,qsdf,qsdf,qsf,qsdf,qsdf
Corrected
Code:
INSERT INTO 
    contestpost
    (
        thread_id, username, user_id, thread_title,
        time_post, summary, message, website,
        tagline, color, style, formats
    )
VALUES 
(
    1, 'Admin', 1, 'qsdfqsdf', 
    1197145012, 'qsdfqsdf', 'sqdfqsdfsd', 'qsdf', 
    'qsdf', 'qsf', 'qsdf', 'qsdf'
);
The above is just an example, and the column names might not be what are in your table. For detailed information on how to write INSERT queries, please read INSERT in the MySQL Manual.

Correctly formatting your queries (lines beginning "$contest = ..." and "$post = ...") will prevent the errors from occurring but the way in which you go about this is prone to all manner of problems -- in terms of security -- in the long run. But that's outwith the scope of your question here.

12-09-2007, 12:35 AM
#3
Nightscream is offline Nightscream
Status: Junior Member
Join date: Aug 2006
Location:
Expertise:
Software:
 
Posts: 58
iTrader: 0 / 0%
 

Nightscream is on a distinguished road

  Old

could you tell me more about how it should? because I don't want any security leeks.

12-17-2007, 04:30 PM
#4
Xeoncross is offline Xeoncross
Status: I'm new around here
Join date: Dec 2007
Location:
Expertise:
Software:
 
Posts: 19
iTrader: 0 / 0%
 

Xeoncross is on a distinguished road

  Old

Don't use AddSlashes - use http://us.php.net/manual/en/function...ape-string.php if you are working with mySQL as it is safer. AddSlashes allows certain hex and stuff to make it through un-harmed.

Also, you might need to strip_slashes if your data is messed with by your server.

PHP Code:
//undo slashes for poorly configured servers
$_POST['text'] = (get_magic_quotes_gpc()) ? stripslashes($_POST['text']) : $_POST['text']; 

12-21-2007, 08:53 PM
#5
Nightscream is offline Nightscream
Status: Junior Member
Join date: Aug 2006
Location:
Expertise:
Software:
 
Posts: 58
iTrader: 0 / 0%
 

Nightscream is on a distinguished road

  Old

So use the code below for inserting in the database?
PHP Code:
$Title = (get_magic_quotes_gpc()) ? stripslashes($Title) : $Title
    
$Sub = (get_magic_quotes_gpc()) ? stripslashes($Sub) : $Sub
    
$Description = (get_magic_quotes_gpc()) ? stripslashes($Description) : $Description;
    
$Website = (get_magic_quotes_gpc()) ? stripslashes($Website) : $Website;
    
$Tagline = (get_magic_quotes_gpc()) ? stripslashes($Tagline) : $Tagline;  
    
$Color = (get_magic_quotes_gpc()) ? stripslashes($Color) : $Color
    
$Style = (get_magic_quotes_gpc()) ? stripslashes($Style) : $Style;  
    
$Formats = (get_magic_quotes_gpc()) ? stripslashes($Formats) : $Formats

12-31-2007, 04:26 PM
#6
phpintheusa is offline phpintheusa
phpintheusa's Avatar
Status: I'm new around here
Join date: Dec 2007
Location: Tennessee
Expertise:
Software:
 
Posts: 20
iTrader: 0 / 0%
 

phpintheusa is on a distinguished road

Send a message via MSN to phpintheusa

  Old

I just want to recommend that you use a function like this instead of using addslashes(). Addslashes is a security blanket full of holes.

Code:
function validateit($value) {
	$value = str_replace('javascript:', '_', $value);
	$value = str_replace('document.location', '_', $value);
	$value = str_replace('vbscript:', '_', $value);
	$value = str_replace('<marquee', '_', $value);
	$value = str_replace('<script', '_', $value);
	$value = str_replace('?php', '_', $value);
	$value = mysql_real_escape_string(strip_tags(htmlentities(trim($value))));
	return $value;
}
This will help against sql injections, cross site scripting, and all that jazz.

12-31-2007, 04:45 PM
#7
Village Genius is offline Village Genius
Village Genius's Avatar
Status: Geek
Join date: Apr 2006
Location: Denver, CO
Expertise: Software
Software: Chrome, Notepad++
 
Posts: 6,894
iTrader: 18 / 100%
 

Village Genius will become famous soon enough

  Old

Originally Posted by phpintheusa View Post
I just want to recommend that you use a function like this instead of using addslashes(). Addslashes is a security blanket full of holes.

Code:
function validateit($value) {
	$value = str_replace('javascript:', '_', $value);
	$value = str_replace('document.location', '_', $value);
	$value = str_replace('vbscript:', '_', $value);
	$value = str_replace('<marquee', '_', $value);
	$value = str_replace('<script', '_', $value);
	$value = str_replace('?php', '_', $value);
	$value = mysql_real_escape_string(strip_tags(htmlentities(trim($value))));
	return $value;
}
This will help against sql injections, cross site scripting, and all that jazz.
Dont manually escape all the HTML. Use htmlspecialchars and unescape the values that you want in (<b><i>, ect.)

12-31-2007, 05:09 PM
#8
phpintheusa is offline phpintheusa
phpintheusa's Avatar
Status: I'm new around here
Join date: Dec 2007
Location: Tennessee
Expertise:
Software:
 
Posts: 20
iTrader: 0 / 0%
 

phpintheusa is on a distinguished road

Send a message via MSN to phpintheusa

  Old

When you want html to be allowed, you can do this:

Code:
	$value = mysql_real_escape_string(trim($value));
Nice blog, VI, good reading!

01-01-2008, 07:33 PM
#9
Nightscream is offline Nightscream
Status: Junior Member
Join date: Aug 2006
Location:
Expertise:
Software:
 
Posts: 58
iTrader: 0 / 0%
 

Nightscream is on a distinguished road

  Old

Ok thanks

I've got another problem with css and php, I've attached a css file.
But It won't use it, I change the color of a class but it won't do it when viewing the file.

Code:
dd .amount {
	color: #580;
	font-weight: bold;
}
Code:
<dd class="amount">$<? echo $prize; ?></dd>

01-01-2008, 08:13 PM
#10
Village Genius is offline Village Genius
Village Genius's Avatar
Status: Geek
Join date: Apr 2006
Location: Denver, CO
Expertise: Software
Software: Chrome, Notepad++
 
Posts: 6,894
iTrader: 18 / 100%
 

Village Genius will become famous soon enough

  Old

Thats not how html and css works

Try
Code:
.amount {
	color: #580;
	font-weight: bold;
}
Code:
<dd class="amount">$<? echo $prize; ?></dd>
[/QUOTE]

Closed Thread  
Page 1 of 2 1 2 >


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 

  Posting Rules  
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump:
 
  Contains New Posts Forum Contains New Posts   Contains No New Posts Forum Contains No New Posts   A Closed Forum Forum is Closed