473,546 Members | 2,239 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

PHP/MySQL "UPDATE" question

A beginner in this area, I have been able to read a record from a
MySQL database and populate an HTML form (wow!). Now, my goal is to
allow the user to edit the contents of the form and then update the
record in MySQL.

The problem is, as soon as the "Update" button (type="submit") is
pressed, all of the data disappear from the form. How can that be
prevented?

Here is my code:

<body>
<?php

// Select the Music database
mysql_select_db ("music") or die("Could not connect to Music
database");

// Perform SQL query
$sql = "select * from cdcollection where Artist = '$txtArtistSear ch'";
$result = mysql_query($sq l) or die("SQL Select failed");
$row = mysql_fetch_arr ay($result,MYSQ L_ASSOC);

?>

<?php

if ($cmdUpdate) {
// Select the Music database
mysql_select_db ("music") or die("Could not connect to Music
database");

$sql = "UPDATE cdcollection SET Notes='$txtNote s' WHERE Artist =
'$txtArtistSear ch'" ;
$result = mysql_query($sq l) or die("SQL Update failed");
}

?>

<form method="post" action="<?php echo $PHP_SELF;?>">

<p>Artist&nbsp; &nbsp;&nbsp;
<input type="text" name="txtArtist " size="19" value="<?php echo
$row['Artist'] ?>"> </p>
<p>Title&nbsp;& nbsp;&nbsp;&nbs p;
<input type="text" name="txtTitle" size="39" value="<?php echo
$row['Title'] ?>"></p>
<p>Genre&nbsp;& nbsp;
<input type="text" name="txtGenre" size="13" value="<?php echo
$row['Genre'] ?>"></p>
<p>Rating&nbs p;
<input type="text" name="txtRating " size="3" value="<?php echo
$row['Rating'] ?>"></p>
<p>Notes&nbsp;& nbsp;
<input type="text" name="txtNotes" size="84" value="<?php echo
$row['Notes'] ?>"></p>
<p>

<input type="button" value="Search" name="cmdSearch ">&nbsp;&nb sp;
<input type="submit" value="Update" name="cmdUpdate "></p>

</form>

</body>

</html>

Thanks!!
Mark Jones
Jul 17 '05 #1
2 10545
hi***********@y ahoo.com (Mark) wrote in message news:<5e******* *************** ****@posting.go ogle.com>...
A beginner in this area, I have been able to read a record from a
MySQL database and populate an HTML form (wow!). Now, my goal is to
allow the user to edit the contents of the form and then update the
record in MySQL.

The problem is, as soon as the "Update" button (type="submit") is
pressed, all of the data disappear from the form. How can that be
prevented?

Here is my code:

<body>
<?php

// Select the Music database
mysql_select_db ("music") or die("Could not connect to Music
database");

// Perform SQL query
$sql = "select * from cdcollection where Artist = '$txtArtistSear ch'";
$result = mysql_query($sq l) or die("SQL Select failed");
$row = mysql_fetch_arr ay($result,MYSQ L_ASSOC);

?>

<?php

if ($cmdUpdate) {
// Select the Music database
mysql_select_db ("music") or die("Could not connect to Music
database");

$sql = "UPDATE cdcollection SET Notes='$txtNote s' WHERE Artist =
'$txtArtistSear ch'" ;
$result = mysql_query($sq l) or die("SQL Update failed");
}

?>

<form method="post" action="<?php echo $PHP_SELF;?>">

<p>Artist&nbsp; &nbsp;&nbsp;
<input type="text" name="txtArtist " size="19" value="<?php echo
$row['Artist'] ?>"> </p>
<p>Title&nbsp;& nbsp;&nbsp;&nbs p;
<input type="text" name="txtTitle" size="39" value="<?php echo
$row['Title'] ?>"></p>
<p>Genre&nbsp;& nbsp;
<input type="text" name="txtGenre" size="13" value="<?php echo
$row['Genre'] ?>"></p>
<p>Rating&nbs p;
<input type="text" name="txtRating " size="3" value="<?php echo
$row['Rating'] ?>"></p>
<p>Notes&nbsp;& nbsp;
<input type="text" name="txtNotes" size="84" value="<?php echo
$row['Notes'] ?>"></p>
<p>

<input type="button" value="Search" name="cmdSearch ">&nbsp;&nb sp;
<input type="submit" value="Update" name="cmdUpdate "></p>

</form>

</body>

</html>

Thanks!!
Mark Jones


Hi,

You should update the record before fetching from database.
your code should be: -

// Start of script
<?php

// Select the Music database
mysql_select_db ("music") or die("Could not connect to Music
database");

// Update the database if form posted
if (!empty($_POST['cmdUpdate'])
{
$sql = "UPDATE cdcollection SET Notes='$txtNote s' WHERE Artist =
'$txtArtistSear ch'" ;
$result = mysql_query($sq l) or die("SQL Update failed");
}
// Fecth from database

$sql = "select * from cdcollection where Artist = '$txtArtistSear ch'";
$result = mysql_query($sq l) or die("SQL Select failed");
$row = mysql_fetch_arr ay($result,MYSQ L_ASSOC);

?>

<html>
<body>
<form method="post" action="<?php echo $PHP_SELF;?>">

<p>Artist&nbsp; &nbsp;&nbsp;
<input type="text" name="txtArtist " size="19" value="<?php echo
$row['Artist'] ?>"> </p>
<p>Title&nbsp;& nbsp;&nbsp;&nbs p;
<input type="text" name="txtTitle" size="39" value="<?php echo
$row['Title'] ?>"></p>
<p>Genre&nbsp;& nbsp;
<input type="text" name="txtGenre" size="13" value="<?php echo
$row['Genre'] ?>"></p>
<p>Rating&nbs p;
<input type="text" name="txtRating " size="3" value="<?php echo
$row['Rating'] ?>"></p>
<p>Notes&nbsp;& nbsp;
<input type="text" name="txtNotes" size="84" value="<?php echo
$row['Notes'] ?>"></p>

<p><input type="button" value="Search" name="cmdSearch ">&nbsp;&nb sp;
<input type="submit" value="Update" name="cmdUpdate "></p>

</form>

</body>
</html>

// End of Script

regards,

rahul
Jul 17 '05 #2
I'm still stumped on this one. I forwarded my code to you, Rahul.

As soon as I click the "Update" button (type="submit") , ALL the
variables are cleared, including those on the form, the ones on the
calling HTML form and any that I declare inside the script. This is
true regardless of the order of the fetch and update logic within the
code.

How can I execute an SQL "Update" without losing everything? I assume
the data loss is caused by the button type="submit".

Thanks again!
ra************@ rediffmail.com (Rahul Anand) wrote in message news:<62******* *************** ****@posting.go ogle.com>...
hi***********@y ahoo.com (Mark) wrote in message news:<5e******* *************** ****@posting.go ogle.com>...
A beginner in this area, I have been able to read a record from a
MySQL database and populate an HTML form (wow!). Now, my goal is to
allow the user to edit the contents of the form and then update the
record in MySQL.

The problem is, as soon as the "Update" button (type="submit") is
pressed, all of the data disappear from the form. How can that be
prevented?

Here is my code:

<body>
<?php

// Select the Music database
mysql_select_db ("music") or die("Could not connect to Music
database");

// Perform SQL query
$sql = "select * from cdcollection where Artist = '$txtArtistSear ch'";
$result = mysql_query($sq l) or die("SQL Select failed");
$row = mysql_fetch_arr ay($result,MYSQ L_ASSOC);

?>

<?php

if ($cmdUpdate) {
// Select the Music database
mysql_select_db ("music") or die("Could not connect to Music
database");

$sql = "UPDATE cdcollection SET Notes='$txtNote s' WHERE Artist =
'$txtArtistSear ch'" ;
$result = mysql_query($sq l) or die("SQL Update failed");
}

?>

<form method="post" action="<?php echo $PHP_SELF;?>">

<p>Artist&nbsp; &nbsp;&nbsp;
<input type="text" name="txtArtist " size="19" value="<?php echo
$row['Artist'] ?>"> </p>
<p>Title&nbsp;& nbsp;&nbsp;&nbs p;
<input type="text" name="txtTitle" size="39" value="<?php echo
$row['Title'] ?>"></p>
<p>Genre&nbsp;& nbsp;
<input type="text" name="txtGenre" size="13" value="<?php echo
$row['Genre'] ?>"></p>
<p>Rating&nbs p;
<input type="text" name="txtRating " size="3" value="<?php echo
$row['Rating'] ?>"></p>
<p>Notes&nbsp;& nbsp;
<input type="text" name="txtNotes" size="84" value="<?php echo
$row['Notes'] ?>"></p>
<p>

<input type="button" value="Search" name="cmdSearch ">&nbsp;&nb sp;
<input type="submit" value="Update" name="cmdUpdate "></p>

</form>

</body>

</html>

Thanks!!
Mark Jones


Hi,

You should update the record before fetching from database.
your code should be: -

// Start of script
<?php

// Select the Music database
mysql_select_db ("music") or die("Could not connect to Music
database");

// Update the database if form posted
if (!empty($_POST['cmdUpdate'])
{
$sql = "UPDATE cdcollection SET Notes='$txtNote s' WHERE Artist =
'$txtArtistSear ch'" ;
$result = mysql_query($sq l) or die("SQL Update failed");
}
// Fecth from database

$sql = "select * from cdcollection where Artist = '$txtArtistSear ch'";
$result = mysql_query($sq l) or die("SQL Select failed");
$row = mysql_fetch_arr ay($result,MYSQ L_ASSOC);

?>

<html>
<body>
<form method="post" action="<?php echo $PHP_SELF;?>">

<p>Artist&nbsp; &nbsp;&nbsp;
<input type="text" name="txtArtist " size="19" value="<?php echo
$row['Artist'] ?>"> </p>
<p>Title&nbsp;& nbsp;&nbsp;&nbs p;
<input type="text" name="txtTitle" size="39" value="<?php echo
$row['Title'] ?>"></p>
<p>Genre&nbsp;& nbsp;
<input type="text" name="txtGenre" size="13" value="<?php echo
$row['Genre'] ?>"></p>
<p>Rating&nbs p;
<input type="text" name="txtRating " size="3" value="<?php echo
$row['Rating'] ?>"></p>
<p>Notes&nbsp;& nbsp;
<input type="text" name="txtNotes" size="84" value="<?php echo
$row['Notes'] ?>"></p>

<p><input type="button" value="Search" name="cmdSearch ">&nbsp;&nb sp;
<input type="submit" value="Update" name="cmdUpdate "></p>

</form>

</body>
</html>

// End of Script

regards,

rahul

Jul 17 '05 #3

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

Similar topics

19
8676
by: Westcoast Sheri | last post by:
To keep track of how many fruits my visitors buy, I use a mySQL database (2 columns: "fruit" and "quantity")....so can we make these following mySQL queries work somehow? (visitor buys 5 apples): replace into fruit_database set fruit = 'apple' , quantity = quantity + 5; (visitor buys 7 apples): replace into fruit_database set fruit =...
1
1699
by: Paul | last post by:
Hello, I know mysql update queries can only utilize one index, making them slow in some cases. My update are taking about 3 hours, joining 2 tables on their primary keys. (table sizes about 1 mil in 1 table, 2 mill in the other). My real question though is, mysql is only using about 10% of the cpu whilst running this update query (on a...
5
3823
by: jayson_13 | last post by:
Hi, I need to implement a counter and i face problem of locking so hope that u guys can help me. I try to do test like this : 1st connection SELECT * FROM nextkey WHERE tblname = 'PLCN' FOR Update; (when i execute this statement and i guess that this will lock the
3
1667
by: Epetruk | last post by:
Hi, I have a mySql question here on updates to multiple tables. Here's a simple schema to clarify things: Structure Table A
8
11211
by: RC | last post by:
In my Access 2002 form, I have a combo box and on the AfterUpdate event I use DoCmd.RunSQL ("UPDATE .... to update records in a table. When it starts to run I get a message "You are about to update 3 row(s)." Is there a way to prevent the message from popping up?
5
4161
by: HydroSan | last post by:
Having a bit of a problem getting UPDATE working. The project in question is a simple MySQL VB.NET frontend, allowing Insertion, Selection, and others. Well, I've gotten Drop and Insert working, but to edit a table row, I'd like to use Update. I have the following code in a class: Private Function SQL_CustomerUpdate()
5
7604
by: Lennart | last post by:
I really like the construction: select * from new table (update ....) X but I noticed that it cant be used as: insert into T select * from new table (update ....) X because of:
1
1697
by: test | last post by:
Using a DetailsView to update data in SQL 2005, How do I change the default "Update" hyperlink to a more user friendly button? Thanks
2
2873
osward
by: osward | last post by:
Hello there, I am using phpnuke 8.0 to build my website, knowing little on php programing. I am assembling a module for my member which is basically cut and paste existing code section of various module that I found it useful. Here is the 1st problem I encounter: I had a function to edit a event row form the database which is fine with...
0
7504
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
1
7461
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7792
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6026
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
5080
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3491
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3470
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1046
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
747
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.