Current location: Hot Scripts Forums » Programming Languages » PHP » Problem inserting into database


Problem inserting into database

Reply
  #1 (permalink)  
Old 02-03-07, 02:00 PM
sn0wy sn0wy is offline
Newbie Coder
 
Join Date: Jan 2006
Posts: 57
Thanks: 0
Thanked 0 Times in 0 Posts
Problem inserting into database

Hey Guys,

Trying to make a script where i just fill a textarea in and sends the text into a database, but the problem i have is when there is any ' in the text i got 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 're firmly established at the very heart of British life, providing the freshest ' at line 1

but if i dont use ' it goes in fine? Can anyone help?? my text is below

PHP Code:

<?PHP

include ("includes/db.php");

$id2 $_POST['id2'];

$free_text $_POST['free_text'];
$awards_text $_POST['awards_text'];




$table_name "baccount";
      
          
$connection = @mysql_connect($dbserver$dbuser$dbpass) or die ("Couldn't Connect to database");
          
$db = @mysql_select_db($dbname$connection) or die(mysql_error());
          
$sql "UPDATE $table_name set free_text= '$free_text', awards_text= '$awards_text' WHERE id = '$id2'";
          
$result = @mysql_query($sql,$connection) or die(mysql_error()); 
        
          
?>
Can someone have a look at my code and see what i need to do?

Thank you in advanced
__________________
Power is in my programming fingers

Last edited by Nico; 02-03-07 at 02:04 PM. Reason: Fixed [/php] wrapper.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #2 (permalink)  
Old 02-03-07, 09:11 PM
grafman grafman is offline
Coding Addict
 
Join Date: Dec 2006
Posts: 278
Thanks: 0
Thanked 0 Times in 0 Posts
Do you have an apostrophe or single quote of some sort in your text? If you do then you need to use addslashes otherwise sql complains.

My guess is that you have something like "they're" right there and the string is trying to close at the apostrophe rather than at the end of the string where you expect it to.
__________________
"Things are difficult only while you don't understand them."
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #3 (permalink)  
Old 02-04-07, 06:20 AM
sn0wy sn0wy is offline
Newbie Coder
 
Join Date: Jan 2006
Posts: 57
Thanks: 0
Thanked 0 Times in 0 Posts
Yea thats the problem i am having, But because its a textarea and want users to enter ' and any other special characters, what do i do?
__________________
Power is in my programming fingers
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #4 (permalink)  
Old 02-04-07, 07:05 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,074
Thanks: 11
Thanked 88 Times in 83 Posts
PHP Code:



$free_text 
mysql_real_escape_string($free_text); 
http://us2.php.net/mysql_real_escape_string
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #5 (permalink)  
Old 02-04-07, 07:14 AM
sn0wy sn0wy is offline
Newbie Coder
 
Join Date: Jan 2006
Posts: 57
Thanks: 0
Thanked 0 Times in 0 Posts
Is this correct?

PHP Code:

<?PHP

include ("includes/db.php");

$id2 $_POST['id2'];

$free_text $_POST['free_text'] ;

$free_text mysql_real_escape_string($free_text);  

$awards_text $_POST['awards_text'];

$table_name "baccount";
      
          
$connection = @mysql_connect($dbserver$dbuser$dbpass) or die ("Couldn't Connect to database");
          
$db = @mysql_select_db($dbname$connection) or die(mysql_error());
          
$sql "UPDATE $table_name set free_text= '$free_text', awards_text= '$awards_text' WHERE id = '$id2'";
          
$result = @mysql_query($sql,$connection) or die(mysql_error()); 
        
          
?>
__________________
Power is in my programming fingers
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #6 (permalink)  
Old 02-04-07, 07:27 AM
Nico's Avatar
Nico Nico is offline
Community Leader
 
Join Date: Sep 2005
Location: Spain
Posts: 8,074
Thanks: 11
Thanked 88 Times in 83 Posts
Half. Use mysql_real_escape_string() on all user defined variables you're going to insert.

And you can do it directly like this.
PHP Code:

$free_text mysql_real_escape_string($_POST['free_text']);

$awards_text mysql_real_escape_string($_POST['awards_text']); 
And if $id2 is supposed to be a numeric value, I'd use intval() on it.

PHP Code:

$id2 intval($_POST['id2']); 

www.php.net/intval
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #7 (permalink)  
Old 02-04-07, 11:12 AM
stormshadow's Avatar
stormshadow stormshadow is offline
Coding Addict
 
Join Date: Mar 2005
Posts: 355
Thanks: 0
Thanked 0 Times in 0 Posts
also, i find it that

sometimes in my code, the output changes but sometimes this wont word:
PHP Code:

 $sql "UPDATE $table_name set free_text= '$free_text', awards_text= '$awards_text' WHERE id = '$id2'"
and you need to do this:
PHP Code:

 $sql "UPDATE `$table_name` set free_text= '$free_text', awards_text= '$awards_text' WHERE id = '$id2'"
but like i said my output usually varies
__________________
Lost-On-Earth.net - *BETA* Text Based RPG
LiquidSqueeze.com - *Under Development* Scripts, Articles, Tutorials. Help
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #8 (permalink)  
Old 02-04-07, 02:14 PM
HoTDaWg's Avatar
HoTDaWg HoTDaWg is offline
Newbie Coder
 
Join Date: Aug 2005
Posts: 43
Thanks: 0
Thanked 0 Times in 0 Posts
could you export your mysql table for us?

Last edited by HoTDaWg; 02-04-07 at 02:14 PM. Reason: incorrect english
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #9 (permalink)  
Old 02-05-07, 02:11 PM
sn0wy sn0wy is offline
Newbie Coder
 
Join Date: Jan 2006
Posts: 57
Thanks: 0
Thanked 0 Times in 0 Posts
Ok for some reason this is not inserting into a database

PHP Code:

<?PHP

include ("includes/db.php");

$id2 $_POST['id2'];

$free_text mysql_real_escape_string($_POST['free_text']); 
$awards_text mysql_real_escape_string($_POST['awards_text']);  

$table_name "baccount";
      
          
$connection = @mysql_connect($dbserver$dbuser$dbpass) or die ("Couldn't Connect to database");
          
$db = @mysql_select_db($dbname$connection) or die(mysql_error());
          
$sql "UPDATE $table_name set free_text= '$free_text', awards_text= '$awards_text' WHERE id = '$id2'";
          
$result = @mysql_query($sql,$connection) or die(mysql_error()); 
        
          
?>
          
          <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>foodplanet infonet</title>
<link href="includes/default.css" rel="stylesheet" type="text/css" />
</head><body>
<div id="extra">Classifications    (<?php echo ''.$id2.''?>)</div><div id="extra1">
Classification update completed<br />
<?php echo ''.$free_text.''?>
<a href="Javascript: self.close()">Close Window</a>
</div>   
</div>
</body>
</html>
The error i get is this

PHP Warning: mysql_real_escape_string(): A link to the server could not be established in /home/f/o/foodplanet/public_html/info/extratext2.php on line 7
__________________
Power is in my programming fingers
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
  #10 (permalink)  
Old 02-05-07, 02:11 PM
sn0wy sn0wy is offline
Newbie Coder
 
Join Date: Jan 2006
Posts: 57
Thanks: 0
Thanked 0 Times in 0 Posts
if you need it this is the page with the form in

PHP Code:

<?php 


include ("includes/db.php");

$id $_GET['id'];

$process $_POST['process'];


        
$table_name2 "baccount";
        
$connection = @mysql_connect($localhost$dbuser$dbpass) or die ("Couldn't Connect to database");
        
$db = @mysql_select_db($dbname$connection) or die("Couldn't select database");
        
$sql "select * from $table_name2 WHERE id = '$id'";
        
$result = @mysql_query($sql,$connection) or die ("Couldn't execute query");

        while (
$row mysql_fetch_array($result)) {   
        
        
                        
                
$awards_text $row["awards_text"];
                
$free_text $row["free_text"];
                
                }
                
                
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>foodplanet infonet</title>
<link href="includes/default.css" rel="stylesheet" type="text/css" />
</head>
<script> 
self.resizeTo(700,600);
self.moveTo(700,350);
</script>
<body>
<div id="extra">Classifications    (<?php echo ''.$id.''?>)</div>
<div id="extra1"><form id="form1" name="form1" method="post" action="extratext2.php">
  <table width="100%" border="0">
    <tr>
      <td><strong>More Information</strong></td>
      </tr>
    <tr>
      <td><label>
        <textarea name="free_text" cols="60" rows="5" wrap="physical" id="free_text"><?php echo ''.$free_text.''?>
</textarea>
      </label></td>
      </tr>
    <tr>
      <td><strong>Award Text </strong></td>
    </tr>
    <tr>
      <td><label>
        <textarea name="awards_text" cols="60" rows="5" wrap="physical" id="awards_text"><?php echo ''.$awards_text.''?>
</textarea>
      </label></td>
    </tr>
    <tr>
      <td><label>
        <div align="center">
          <input type="submit" name="Submit" value="Save" />
          <input name="id2" type="hidden" id="id2" value="<?php echo ''.$id.'';?>" />
          <input name="process" type="hidden" id="process" value="1" />
</div>
      </label></td>
    </tr>
  </table>
</form><br /><a href="Javascript: self.close()">Close Window</a>
</div>
</body>
</html>
__________________
Power is in my programming fingers
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiShare on FacebookShare on Stumble UponShare on Twitter
Reply With Quote
Reply

Bookmarks


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

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
apostrophes error while inserting variables containing them in database zitwep PHP 2 06-30-06 11:16 PM
MySQL Database Problem shamerox PHP 6 12-03-05 04:53 PM
A Language filter problem, and a problem with a for loop querying my database. . . Spreegem PHP 6 05-08-05 12:03 PM
i have a problem regarding updating database online GENIUSAdnan PHP 1 06-23-04 04:53 AM
tracking down a database problem ... bjmcintosh ASP 1 08-29-03 08:52 AM


All times are GMT -5. The time now is 04:39 PM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.