471,305 Members | 1,272 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,305 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 10399
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 discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

19 posts views Thread by Westcoast Sheri | last post: by
1 post views Thread by Paul | last post: by
5 posts views Thread by jayson_13 | last post: by
3 posts views Thread by Epetruk | last post: by
5 posts views Thread by HydroSan | last post: by
reply views Thread by rosydwin | last post: by

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.