| | |
updating database problem
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Aug 2007
Posts: 25
Reputation:
Solved Threads: 0
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() ;
}
}
}
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() ;
}
}
}
•
•
Join Date: Aug 2007
Posts: 25
Reputation:
Solved Threads: 0
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
•
•
Join Date: Aug 2007
Posts: 25
Reputation:
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);
}
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);
}
![]() |
Similar Threads
- database problem (C#)
- Writing to an Access Database (Visual Basic 4 / 5 / 6)
- Updating database with related tables and queries?? (VB.NET)
- Error connecting to database (ASP.NET)
- database access is getting slow day by day (MySQL)
- Database not being populated.... (Java)
- Problem printing from IE6 (Web Browsers)
- clustering (Windows NT / 2000 / XP)
Other Threads in the PHP Forum
- Previous Thread: Mail to a list of people
- Next Thread: Any free PHP resources out there.
| Thread Tools | Search this Thread |
Tag cloud for PHP
.htaccess access ajax apache api array beginner binary broken cakephp checkbox class cms code cron curl database date directory display download dynamic echo email encode error fcc file files folder form forms function functions google howtowriteathesis href htaccess html image include insert integration ip java javascript joomla limit link login loop mail menu methods mlm mod_rewrite multiple multipletables mysql oop open parse paypal pdf php problem provider query radio random recursion regex remote script search select server sessions sms soap source space speed sql structure syntax system table template tutorial update upload url validation validator variable video web xml youtube






