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

Edited 3 Years Ago by happygeek: fixed formatting

Do you get an error message ?

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

There are no error messages. Apologies for the typo, thats not the 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.

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.

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

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

Edited 3 Years Ago by happygeek: fixed formatting

This article has been dead for over six months. Start a new discussion instead.