473,473 Members | 1,874 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Problems Updating Records In MySQL with PHP

Hello,
Sorry to ask what is probably a simple answer, but I am having problems
updating a table/database from a PHP/

PHTML file. I can Read From the Table, I can Insert into Table/Database, But
not update. Here is a copy of the

script I am using. I do not know what version of MySQL my host is running
nor do I have Shell Access to it. I

would like to setup a script so my website users can update thier records
without my intervention. Thank You In

Advance.

Also you may send replies to st**********@yahoo.com

Begin Script

<?php
$db = mysql_connect("localhost", "username", "password");
mysql_select_db("databasename",$db);

if ($id) {
if ($submit) {
$sql = "UPDATE tablename SET
EMail='$EMail',Username='$Username',Password='$Pas sword',Item=

'$Item',Price='$Price',URL='$URL' WHERE Username='$Username'";
$result = mysql_query($sql);
echo "table Updated.";
} else {
// query the DB
$sql = "SELECT * FROM tablename WHERE Username='$Username'";
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
?>
<form method="post" action="<?php echo $PHP_SELF?>">
e-Mail Address:<input type="Text" name="EMail" value="<?php echo
$myrow["EMail"] ?>"><br>
Username:<input type="Text" name="Username" value="<?php echo
$myrow["Username"] ?>"><br>
Password:<input type="Text" name="Password" value="<?php echo
$myrow["Password"] ?>"><br>
Item:<input type="Text" name="Item" value="<?php echo $myrow["Item"]
?>"><br>
Price:<input type="Text" name="Price" value="<?php echo $myrow["Price"]
?>"><br>
Item URL:<input type="Text" name="URL" value="<?php echo $myrow["URL"]
?>"><br>
<input type="Submit" name="Update" value="Update table">
</form>
<?php
}
} else {
// display list of articles
$result = mysql_query("SELECT * FROM tablename",$db);
while ($myrow = mysql_fetch_array($result)) {
printf("<a href=\"%s?Username=%s\">%s %s</a><br>\n", $PHP_SELF,
$myrow["Username"], $myrow["Item"], $myrow

["URL"]);
}
}
// Close DB Connection
mysql_close()
?>

End Script
Jul 17 '05 #1
3 2941
st**********@earthlink.net wrote:

Hello,
Sorry to ask what is probably a simple answer, but I am having problems
updating a table/database from a PHP/

PHTML file. I can Read From the Table, I can Insert into Table/Database, But
not update. Here is a copy of the

script I am using. I do not know what version of MySQL my host is running
nor do I have Shell Access to it. I

would like to setup a script so my website users can update thier records
without my intervention. Thank You In

Advance.

Also you may send replies to st**********@yahoo.com

Begin Script

<?php
$db = mysql_connect("localhost", "username", "password");
mysql_select_db("databasename",$db);

if ($id) {
if ($submit) {
$sql = "UPDATE tablename SET
EMail='$EMail',Username='$Username',Password='$Pas sword',Item=

'$Item',Price='$Price',URL='$URL' WHERE Username='$Username'";
$result = mysql_query($sql);
echo "table Updated.";
} else {
// query the DB
$sql = "SELECT * FROM tablename WHERE Username='$Username'";
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
?>
<form method="post" action="<?php echo $PHP_SELF?>">
e-Mail Address:<input type="Text" name="EMail" value="<?php echo
$myrow["EMail"] ?>"><br>
Username:<input type="Text" name="Username" value="<?php echo
$myrow["Username"] ?>"><br>
Password:<input type="Text" name="Password" value="<?php echo
$myrow["Password"] ?>"><br>
Item:<input type="Text" name="Item" value="<?php echo $myrow["Item"]
?>"><br>
Price:<input type="Text" name="Price" value="<?php echo $myrow["Price"]
?>"><br>
Item URL:<input type="Text" name="URL" value="<?php echo $myrow["URL"]
?>"><br>
<input type="Submit" name="Update" value="Update table">
</form>
<?php
}
} else {
// display list of articles
$result = mysql_query("SELECT * FROM tablename",$db);
while ($myrow = mysql_fetch_array($result)) {
printf("<a href=\"%s?Username=%s\">%s %s</a><br>\n", $PHP_SELF,
$myrow["Username"], $myrow["Item"], $myrow

["URL"]);
}
}
// Close DB Connection
mysql_close()
?>

End Script


I don't see $id or $submit in your form. Where are they coming from? I suspect
this is the problem. If so, you'll have to put them in as hidden fields and/or
rename the submit button to "submit".

And you may want to consider using $_GET, $_POST, $_REQUEST, $_SESSION, etc.
If that doesn't clear up the problem:

Is register_globals on or off?
What error(s) are you getting? If none, try error_reporting(E_ALL); at the top
of the script.
Does that username have update privileges?

Regards,
Shawn
--
Shawn Wilson
sh***@glassgiant.com
http://www.glassgiant.com
Jul 17 '05 #2

On 18-Dec-2003, <st**********@earthlink.net> wrote:
Hello,
Sorry to ask what is probably a simple answer, but I am having problems
updating a table/database from a PHP/

PHTML file. I can Read From the Table, I can Insert into Table/Database,
But
not update. Here is a copy of the

script I am using. I do not know what version of MySQL my host is running
nor do I have Shell Access to it. I

would like to setup a script so my website users can update thier records
without my intervention. Thank You In

Advance.

Also you may send replies to st**********@yahoo.com

Begin Script

<?php
$db = mysql_connect("localhost", "username", "password");
mysql_select_db("databasename",$db);

if ($id) {
if ($submit) {
$sql = "UPDATE tablename SET
EMail='$EMail',Username='$Username',Password='$Pas sword',Item=

'$Item',Price='$Price',URL='$URL' WHERE Username='$Username'";
$result = mysql_query($sql);
echo "table Updated.";
} else {
// query the DB
$sql = "SELECT * FROM tablename WHERE Username='$Username'";
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
?>
<form method="post" action="<?php echo $PHP_SELF?>">
e-Mail Address:<input type="Text" name="EMail" value="<?php echo
$myrow["EMail"] ?>"><br>
Username:<input type="Text" name="Username" value="<?php echo
$myrow["Username"] ?>"><br>
Password:<input type="Text" name="Password" value="<?php echo
$myrow["Password"] ?>"><br>
Item:<input type="Text" name="Item" value="<?php echo $myrow["Item"]
?>"><br>
Price:<input type="Text" name="Price" value="<?php echo $myrow["Price"]
?>"><br>
Item URL:<input type="Text" name="URL" value="<?php echo $myrow["URL"]
?>"><br>
<input type="Submit" name="Update" value="Update table">
</form>
<?php
}
} else {
// display list of articles
$result = mysql_query("SELECT * FROM tablename",$db);
while ($myrow = mysql_fetch_array($result)) {
printf("<a href=\"%s?Username=%s\">%s %s</a><br>\n", $PHP_SELF,
$myrow["Username"], $myrow["Item"], $myrow

["URL"]);
}
}
// Close DB Connection
mysql_close()
?>

End Script


You have several problems

1) assuming you are running with register_globals off, you still need to use
addslashes() on the variables you receive from your form
2) you aren't doing any error checking. You echo "table updated" after the
query without checking the result. You should have some way of displaying
the MySQL error when the query fails-- like 'mysql_query($sql) or die($sql.'
failed because '.mysql_error());'
3) where does $id come from?

Not being able to update is usually caused by
1) where clause failed
2) no update permission
3) syntax error in SQL (bad column name, etc.)
4) not connected to DB

If you add the 'or die()' to your code you will probably get your answer.

--
Tom Thackrey
www.creative-light.com
tom (at) creative (dash) light (dot) com
do NOT send email to ja*********@willglen.net (it's reserved for spammers)
Jul 17 '05 #3
To All,
Thank You For the Help. The $id was mis named (Wrong Field/Colum) but to
answer the question that fild is a passed argument from another page. To be
honest I do little PHP/SQL Programing where is do Error Trapping, I know
this is bad but the Manual that I have (From PHP.net) does not explain very
well how to error trap.

Thanks
Steven Y.

"Shawn Wilson" <sh***@glassgiant.com> wrote in message
news:3F***************@glassgiant.com...
st**********@earthlink.net wrote:

Hello,
Sorry to ask what is probably a simple answer, but I am having problems
updating a table/database from a PHP/

PHTML file. I can Read From the Table, I can Insert into Table/Database, But not update. Here is a copy of the

script I am using. I do not know what version of MySQL my host is running nor do I have Shell Access to it. I

would like to setup a script so my website users can update thier records without my intervention. Thank You In

Advance.

Also you may send replies to st**********@yahoo.com

Begin Script

<?php
$db = mysql_connect("localhost", "username", "password");
mysql_select_db("databasename",$db);

if ($id) {
if ($submit) {
$sql = "UPDATE tablename SET
EMail='$EMail',Username='$Username',Password='$Pas sword',Item=

'$Item',Price='$Price',URL='$URL' WHERE Username='$Username'";
$result = mysql_query($sql);
echo "table Updated.";
} else {
// query the DB
$sql = "SELECT * FROM tablename WHERE Username='$Username'";
$result = mysql_query($sql);
$myrow = mysql_fetch_array($result);
?>
<form method="post" action="<?php echo $PHP_SELF?>">
e-Mail Address:<input type="Text" name="EMail" value="<?php echo
$myrow["EMail"] ?>"><br>
Username:<input type="Text" name="Username" value="<?php echo
$myrow["Username"] ?>"><br>
Password:<input type="Text" name="Password" value="<?php echo
$myrow["Password"] ?>"><br>
Item:<input type="Text" name="Item" value="<?php echo $myrow["Item"]
?>"><br>
Price:<input type="Text" name="Price" value="<?php echo $myrow["Price"]
?>"><br>
Item URL:<input type="Text" name="URL" value="<?php echo $myrow["URL"]
?>"><br>
<input type="Submit" name="Update" value="Update table">
</form>
<?php
}
} else {
// display list of articles
$result = mysql_query("SELECT * FROM tablename",$db);
while ($myrow = mysql_fetch_array($result)) {
printf("<a href=\"%s?Username=%s\">%s %s</a><br>\n", $PHP_SELF,
$myrow["Username"], $myrow["Item"], $myrow

["URL"]);
}
}
// Close DB Connection
mysql_close()
?>

End Script
I don't see $id or $submit in your form. Where are they coming from? I

suspect this is the problem. If so, you'll have to put them in as hidden fields and/or rename the submit button to "submit".

And you may want to consider using $_GET, $_POST, $_REQUEST, $_SESSION, etc.

If that doesn't clear up the problem:

Is register_globals on or off?
What error(s) are you getting? If none, try error_reporting(E_ALL); at the top of the script.
Does that username have update privileges?

Regards,
Shawn
--
Shawn Wilson
sh***@glassgiant.com
http://www.glassgiant.com

Jul 17 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: Paige | last post by:
I have a database on my local machine that I make entries and corrections on. I'd like to be able to upload that to my server and have that update the database that's on the server. What I've been...
3
by: Bert Sierra | last post by:
Hello -- I have what appears to be a simple PHP+MySQL query, but Dreamweaver consistently generates PHP code which won't parse. I've stared at the PHP code for several hours now, and I can't...
6
by: carverk | last post by:
Hello All I'm in the middle of moving a MS Access DB to a MySql backend. I have figured out about 90% of the problems I have faced, execpt for this one. I have 3 Queries, which pull records...
2
by: Phil Powell | last post by:
I am having to update records in a table where the field will contain a substitution of another value.. something like this: I've been testing this and producing 0 results(I'm using 4.0.10...
0
by: David Emme | last post by:
Win XP Pro Access 97 MySQL 4.0.x I'm attempting to convert a number of apps to MySQL backends. Having converted the tables from the MDB backend, I'm running into a number of differences in...
1
by: davidgordon | last post by:
Hi, If I am updating a list of records for a user on an asp page, is there a way to hold the page updating, even if they refresh the page, until I have updated all the records. i.e. rather...
1
by: tannenba | last post by:
Ok- I'm a dummy. I got this book "php and mysql for dummies" and decided to learn it from the book. I loaded my sarge originally with mysql 4 and php4 from the debian pkgs. I tried the test...
33
by: bill | last post by:
In an application I am writing the user can define a series of steps to be followed. I save them in a sql database using the field "order" (a smallint) as the primary key. (there are in the range...
6
by: Al Moodie | last post by:
Hi, I have a MySQL database where I want to updated multiple records. The table has two columns: product_number product_price I have a list with first entry product_price, second entry...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.