Reputation:
I am building a blog site and am having trouble with updating fields in my MYSQL database. Whenever I hit the update button on the form that uses PHP, it adds extra space before the text string in the MYSQL text field. Here is the PHP code:
//UPDATE TEXT
$updated_text = $_POST['text'.$the_post['ID'][$n]];
if ($updated_text != "") {
$sql = "UPDATE posts SET TEXT = '".addslashes($updated_text)."' WHERE ID = '".$the_post['ID'][$n]."'";
$text_result = mysql_query($sql) or die(mysql_error());
}
Thanks
Upvotes: 1
Views: 4390
Reputation: 33
If you have a space or line break (in your code editor) in between and then remove them. I had the same issue earlier, but is now fixed.
Upvotes: 0
Reputation:
I found that the empty mySQL field was inserting " " into my html form value, so I used:
$variable = trim($variable," ");
to trim the unwanted space.
Upvotes: 0
Reputation: 401002
Not sure why you have this problem, but you could first try using trim
to remove white-characters at the beginning and end of your string :
$updated_text = trim($_POST['text'.$the_post['ID'][$n]]);
If this solves the problem, it's because you are receiving those whitespaces from the form -- else... Well, strange ^^
A couple of other notes :
mysql_*
function, which means you should use mysql_real_escape_string
instead of addslashes
.TEXT
; but, to avoid SQL injections, you should protect the data use in the where
clause too.
ID
is a char/varchar in DB, it means using mysql_real_escape_string
on $the_post['ID'][$n]
tooID
is an integer in database :
intval($the_post['ID'][$n])
Upvotes: 4
Reputation: 3140
Perhaps its an issue of the text-area tag of your html - for example if its indented or so..
Upvotes: 2