Tycoon Talk
Become a Big fish!
The number 1 forum for online business!
Post topics, ask questions, share your knowledge.
Tycoon Talk is part of Freelancer.com - find skilled workers online at a fraction of the cost.

The Database Forum


You are currently viewing our The Database Forum as a guest. Please register to participate.
Login



Reply
Upadating data into a database
Old 03-15-2005, 04:42 PM Upadating data into a database
dan245's Avatar
Skilled Talker

Posts: 59
Location: Massachusetts, USA
Trades: 0
I have a problem updating data in a database. Yes, I do have text in the area i want to update. Here is the code...

PHP Code:
<?php

if ( isset ( $_POST['submit'] ) ) {

$contents $_POST['contents'];

include ( 
"db_settings.inc.php" );

$sql "UPDATE open_files SET contents = '{$_POST['contents']}' WHERE id = {$_POST['id']}";

$query mysql_query $sql );

if ( 
mysql_affected_rows() == );

print 
"Your file has been updated!";

} else {

print 
"Your file could not be updated...";

}

?>
dan245 is offline
Reply With Quote
View Public Profile
 
 
Register now for full access!
Old 03-15-2005, 05:42 PM
dan245's Avatar
Skilled Talker

Posts: 59
Location: Massachusetts, USA
Trades: 0
I've tried everything...It is probably a simple mistake!
dan245 is offline
Reply With Quote
View Public Profile
 
Old 03-15-2005, 06:12 PM
Super Talker

Posts: 110
Trades: 1
At what point is it failing? Do you get an error? Is a record being inserted but is blank? More evidence required!

Just scanning your code quickly it could be the line

$sql = "UPDATE open_files SET contents = '{$_POST['contents']}' WHERE id = {$_POST['id']}";

Your use of single quotes ' could be breaking the sql statement

Two quick ways to debug this are to either insert a line after that:

print $sql;

and see if PHP errors

or use mysql from the CLI and run the select from the console to see if mysql error's.
Frank Rizzo is offline
Reply With Quote
View Public Profile Visit Frank Rizzo's homepage!
 
Old 03-15-2005, 06:17 PM
Kyrnt's Avatar
The Post-Mod Years

Posts: 2,536
Location: Western Maryland
Trades: 0
One thing I am seeing right off hand is that you are checking $_POST[submit], but then building your query from $_POST[contents]. Why not check isset($_POST[contents]) as well to make sure you're not trying to submit a null or empty string. Similarly, you are not checking $_POST[id] either though it is used.

One thing I always do when I run into a SQL query: Print out the contents of your SQL string then plug it into the query window in phpMyAdmin or similar tool. You will get a much more robust error message.

Also, you are specifically assigning $contents to $_POST[contents], but then continue to use $_POST[contents]. It is not an error, but why are you doing that?
__________________
—Kyrnt
Kyrnt is offline
Reply With Quote
View Public Profile Visit Kyrnt's homepage!
 
Old 03-15-2005, 07:38 PM
Experienced Talker

Posts: 36
Trades: 0
Your script is vulnerable to SQL injection attacks. You need to use addslashes() or mysql_escape_string() on your data (this could also be cause of your problem) - on ALL data before using it a query like this. And you have syntax errors with your last if statement and are missing the last curly brace for the first if statement.

Last edited by tress; 03-15-2005 at 07:41 PM..
tress is offline
Reply With Quote
View Public Profile
 
Old 03-15-2005, 07:49 PM
dan245's Avatar
Skilled Talker

Posts: 59
Location: Massachusetts, USA
Trades: 0
Thanks for the replies! kyrnt, I accidentally deleted that variable in the SQL statment, but by accident i left it there!

But when I update it it does absolutely nothing.
dan245 is offline
Reply With Quote
View Public Profile
 
Old 03-15-2005, 08:04 PM
dan245's Avatar
Skilled Talker

Posts: 59
Location: Massachusetts, USA
Trades: 0
ok, here is the new code!

PHP Code:
<?php

include ( "db_settings.inc.php" );

$sql "UPDATE open_files SET contents = '{$_POST['contents']}' WHERE id = {$_POST['id']}";

$query mysql_query $sql );

if ( 
mysql_affected_rows() == ) {

print 
"Your file has been made!";

} else {

print 
"Your file could not be made...";

}

?>

Last edited by dan245; 03-15-2005 at 08:10 PM..
dan245 is offline
Reply With Quote
View Public Profile
 
Old 03-15-2005, 08:36 PM
Kyrnt's Avatar
The Post-Mod Years

Posts: 2,536
Location: Western Maryland
Trades: 0
So when you run this code, dan, are you at least getting the debug that the file "could not be made?" Or are you getting absolutely nothing?
__________________
—Kyrnt
Kyrnt is offline
Reply With Quote
View Public Profile Visit Kyrnt's homepage!
 
Old 03-15-2005, 08:42 PM
dan245's Avatar
Skilled Talker

Posts: 59
Location: Massachusetts, USA
Trades: 0
Now I can get the "File Could not be made."
dan245 is offline
Reply With Quote
View Public Profile
 
Old 03-16-2005, 02:17 PM
dan245's Avatar
Skilled Talker

Posts: 59
Location: Massachusetts, USA
Trades: 0
Still can't get it...Can anyone help?
dan245 is offline
Reply With Quote
View Public Profile
 
Old 03-16-2005, 02:28 PM
Super Talker

Posts: 110
Trades: 1
I'll repeat. Are you getting any errors or is just your 'Your file could not be...' being displayed.

Are you sure the select statement is correct?

Rem out your $sql line and try this:

$sql = "UPDATE open_files SET contents = 'test' WHERE id = 1";

As I said earlier, it could be your use of single quotes ... If you can test if it's that then you can get the solution!

One other point. What if the number of affected rows is greater than 1? You would therefore get a fail.

To debug this just add

print mysql_affected_rows();

I think you are asking a lot of questions when you should be just doing basic debugging techniques :-)
Frank Rizzo is offline
Reply With Quote
View Public Profile Visit Frank Rizzo's homepage!
 
Reply     « Reply to Upadating data into a database
 

Thread Tools Search this Thread
Search this Thread:

Advanced Search

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 On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off





   
RSS Feed  Feeds: RSS   JS   XML
RSS Feed  Feeds for this forum: RSS   JS   XML



Page generated in 0.41616 seconds with 12 queries