473,225 Members | 1,311 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,225 software developers and data experts.

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 = '$txtArtistSearch'";
$result = mysql_query($sql) or die("SQL Select failed");
$row = mysql_fetch_array($result,MYSQL_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='$txtNotes' WHERE Artist =
'$txtArtistSearch'" ;
$result = mysql_query($sql) 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;&nbsp;
<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&nbsp;
<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;&nbsp;
<input type="submit" value="Update" name="cmdUpdate"></p>

</form>

</body>

</html>

Thanks!!
Mark Jones
Jul 17 '05 #1
2 10529
hi***********@yahoo.com (Mark) wrote in message news:<5e**************************@posting.google. 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 = '$txtArtistSearch'";
$result = mysql_query($sql) or die("SQL Select failed");
$row = mysql_fetch_array($result,MYSQL_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='$txtNotes' WHERE Artist =
'$txtArtistSearch'" ;
$result = mysql_query($sql) 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;&nbsp;
<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&nbsp;
<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;&nbsp;
<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='$txtNotes' WHERE Artist =
'$txtArtistSearch'" ;
$result = mysql_query($sql) or die("SQL Update failed");
}
// Fecth from database

$sql = "select * from cdcollection where Artist = '$txtArtistSearch'";
$result = mysql_query($sql) or die("SQL Select failed");
$row = mysql_fetch_array($result,MYSQL_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;&nbsp;
<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&nbsp;
<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;&nbsp;
<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.google. com>...
hi***********@yahoo.com (Mark) wrote in message news:<5e**************************@posting.google. 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 = '$txtArtistSearch'";
$result = mysql_query($sql) or die("SQL Select failed");
$row = mysql_fetch_array($result,MYSQL_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='$txtNotes' WHERE Artist =
'$txtArtistSearch'" ;
$result = mysql_query($sql) 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;&nbsp;
<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&nbsp;
<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;&nbsp;
<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='$txtNotes' WHERE Artist =
'$txtArtistSearch'" ;
$result = mysql_query($sql) or die("SQL Update failed");
}
// Fecth from database

$sql = "select * from cdcollection where Artist = '$txtArtistSearch'";
$result = mysql_query($sql) or die("SQL Select failed");
$row = mysql_fetch_array($result,MYSQL_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;&nbsp;
<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&nbsp;
<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;&nbsp;
<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
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...
1
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...
5
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'...
3
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
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...
5
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,...
5
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
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
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...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: veera ravala | last post by:
ServiceNow is a powerful cloud-based platform that offers a wide range of services to help organizations manage their workflows, operations, and IT services more efficiently. At its core, ServiceNow...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: jianzs | last post by:
Introduction Cloud-native applications are conventionally identified as those designed and nurtured on cloud infrastructure. Such applications, rooted in cloud technologies, skillfully benefit from...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
Git
by: egorbl4 | last post by:
Скачал я git, хотел начать настройку, а там вылезло вот это Что это? Что мне с этим делать? ...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.