473,387 Members | 1,569 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,387 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 10534
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

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.