954,568 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

updating database problem

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() ;
}
}
}

phobia1
Light Poster
25 posts since Aug 2007
Reputation Points: 10
Solved Threads: 0
 

Do you get an error message ?

There is a difference between $username and $userName, did you see that ? PHP is case sensitive.

pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

There are no error messages. Apologies for the typo, thats not the problem.

phobia1
Light Poster
25 posts since Aug 2007
Reputation Points: 10
Solved Threads: 0
 

If you get rid of all your error-handling and if's, what happens then ?

pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 875
 

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.

phobia1
Light Poster
25 posts since Aug 2007
Reputation Points: 10
Solved Threads: 0
 

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.

Ezzaral
Posting Genius
Moderator
15,986 posts since May 2007
Reputation Points: 3,250
Solved Threads: 847
 

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

phobia1
Light Poster
25 posts since Aug 2007
Reputation Points: 10
Solved Threads: 0
 

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);
}

phobia1
Light Poster
25 posts since Aug 2007
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You