updating database problem

Reply

Join Date: Aug 2007
Posts: 25
Reputation: phobia1 is an unknown quantity at this point 
Solved Threads: 0
phobia1 phobia1 is offline Offline
Light Poster

updating database problem

 
0
  #1
Aug 23rd, 2007
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() ;
}
}
}
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 883
Reputation: pritaeas will become famous soon enough pritaeas will become famous soon enough 
Solved Threads: 142
Sponsor
pritaeas's Avatar
pritaeas pritaeas is offline Offline
Practically a Posting Shark

Re: updating database problem

 
0
  #2
Aug 23rd, 2007
Do you get an error message ?

There is a difference between $username and $userName, did you see that ? PHP is case sensitive.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 25
Reputation: phobia1 is an unknown quantity at this point 
Solved Threads: 0
phobia1 phobia1 is offline Offline
Light Poster

Re: updating database problem

 
0
  #3
Aug 23rd, 2007
There are no error messages. Apologies for the typo, thats not the problem.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 883
Reputation: pritaeas will become famous soon enough pritaeas will become famous soon enough 
Solved Threads: 142
Sponsor
pritaeas's Avatar
pritaeas pritaeas is offline Offline
Practically a Posting Shark

Re: updating database problem

 
0
  #4
Aug 23rd, 2007
If you get rid of all your error-handling and if's, what happens then ?
"If it is NOT source, it is NOT software."
-- NASA
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 25
Reputation: phobia1 is an unknown quantity at this point 
Solved Threads: 0
phobia1 phobia1 is offline Offline
Light Poster

Re: updating database problem

 
0
  #5
Aug 23rd, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: May 2007
Posts: 4,508
Reputation: Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future Ezzaral has a brilliant future 
Solved Threads: 522
Moderator
Featured Poster
Ezzaral's Avatar
Ezzaral Ezzaral is offline Offline
Industrious Poster

Re: updating database problem

 
0
  #6
Aug 23rd, 2007
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.
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 25
Reputation: phobia1 is an unknown quantity at this point 
Solved Threads: 0
phobia1 phobia1 is offline Offline
Light Poster

Re: updating database problem

 
0
  #7
Aug 23rd, 2007
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
Reply With Quote Quick reply to this message  
Join Date: Aug 2007
Posts: 25
Reputation: phobia1 is an unknown quantity at this point 
Solved Threads: 0
phobia1 phobia1 is offline Offline
Light Poster

Re: updating database problem

 
0
  #8
Aug 25th, 2007
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);
}
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



Tag cloud for PHP
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC