944,122 Members | Top Members by Rank

Ad:
  • PHP Discussion Thread
  • Unsolved
  • Views: 1220
  • PHP RSS
Aug 23rd, 2007
0

updating database problem

Expand Post »
Hi Guys
First apologies since no doubt this type of question has been asked before.
Thanks for any help in advance..

I have a form to modify data... it works just fine as I echo the modified array. However the database is not updated. Could anone tell me why?

function sql_update()
{
global $conn;
global $_POST;
$location="Localhost";
$userName="username";
$password="password";
$dbname="database";

$sql = "update `DTable` set
`Town`= " .sqlvalue(@$_POST["Town"], true) .",
`id`=" .sqlvalue(@$_POST["id"], true) .",
`Age`=" .sqlvalue(@$_POST["Age"], false) .",
`pool`=" .sqlvalue(@$_POST["pool"], true) ."
where " ."
(`Town`=" .sqlvalue(@$_POST["xTown"], true) .") and
(`id`=" .sqlvalue(@$_POST["xid"], true) .") and
(`Age`=" .sqlvalue(@$_POST["xAge"], false) .") and
(`pool`=" .sqlvalue(@$_POST["xpool"], true) .")";


echo $sql; //OK to here, but does not update record

// Connect to the Database
if (!($conn=mysql_pconnect($location,$username,$password))) {
DisplayErrMsg(sprintf("error connecting to host %s, by user %s",
$location, $username)) ;
exit() ;
}

// Select the Database
if (!mysql_select_db($dbname, $conn)) {
DisplayErrMsg(sprintf("Error in selecting %s database", $dbname)) ;
DisplayErrMsg(sprintf("error:%d %s", mysql_errno($link), mysql_error($link))) ;
exit() ;
}

// Execute the Statement
if (!mysql_query($sql, $conn)) {
DisplayErrMsg(sprintf("Error in executing %s stmt", $sql)) ;
DisplayErrMsg(sprintf("error:%d %s", mysql_errno($link), mysql_error($link))) ;
exit() ;
}
}
}
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
phobia1 is offline Offline
25 posts
since Aug 2007
Aug 23rd, 2007
0

Re: updating database problem

Do you get an error message ?

There is a difference between $username and $userName, did you see that ? PHP is case sensitive.
Sponsor
Featured Poster
Reputation Points: 557
Solved Threads: 735
Bite my shiny metal ass!
pritaeas is offline Offline
4,204 posts
since Jul 2006
Aug 23rd, 2007
0

Re: updating database problem

There are no error messages. Apologies for the typo, thats not the problem.
Reputation Points: 10
Solved Threads: 0
Light Poster
phobia1 is offline Offline
25 posts
since Aug 2007
Aug 23rd, 2007
0

Re: updating database problem

If you get rid of all your error-handling and if's, what happens then ?
Sponsor
Featured Poster
Reputation Points: 557
Solved Threads: 735
Bite my shiny metal ass!
pritaeas is offline Offline
4,204 posts
since Jul 2006
Aug 23rd, 2007
0

Re: updating database problem

I tried that too, but result is the same I thought at first that just
mysql_query($sql, $conn) or die(mysql_error());
would be required as the &conn (my $link value is valid. I am trying a different approach at the moment and will see if I can get anything into the table.
Reputation Points: 10
Solved Threads: 0
Light Poster
phobia1 is offline Offline
25 posts
since Aug 2007
Aug 23rd, 2007
0

Re: updating database problem

Have you tried copying the $sql string to phpMyAdmin and running the query directly? If you are getting no errors, perhaps your WHERE criteria are not selecting the row you intend.
Moderator
Featured Poster
Reputation Points: 3239
Solved Threads: 839
Posting Genius
Ezzaral is offline Offline
6,761 posts
since May 2007
Aug 23rd, 2007
0

Re: updating database problem

Hi, yes I did just that and it works. The problem is I think that the Table does not know which record to update. However I tried with WHERE id = '$recid' but still no joy. In fact I have too problems. One form that DOES update the Database but sometimes does not read all the text. And another form that reads the text perfectly, but does NOT update. I am trying to combine the two at this time. Thanks for your reply. Best FJW
Reputation Points: 10
Solved Threads: 0
Light Poster
phobia1 is offline Offline
25 posts
since Aug 2007
Aug 25th, 2007
0

Re: updating database problem

I found the problem but can't make it work as yet. The sql-query does not get the $recid so it fails. How to pass a variable from the database to the query?
function sql_update()
{
$conn = mysql_connect("Localhost", "user", "thepass");
mysql_select_db("user");
global $_POST;
$location="Localhost";
$userName="user";
$password="thepass";
$dbname="areabulnet";

$sql = "update `Garant` set
`Town`= " .sqlvalue(@$_POST["Town"], true) .",
`Age`=" .sqlvalue(@$_POST["Age"], true) ."
WHERE id = '$recid' ";

echo $sql; //OK to here

mysql_query($sql, $conn)or die (mysql_error());
mysql_close($conn);
}
Reputation Points: 10
Solved Threads: 0
Light Poster
phobia1 is offline Offline
25 posts
since Aug 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in PHP Forum Timeline: Mail to a list of people
Next Thread in PHP Forum Timeline: Any free PHP resources out there.





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC