By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,950 Members | 1,005 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,950 IT Pros & Developers. It's quick & easy.

PHP/MySQL "UPDATE" question

P: n/a
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
Share this Question
Share on Google+
2 Replies


P: n/a
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

P: n/a
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.