Jump to content

Updating data problem


mcgold652

Recommended Posts

Hi,

 

The following script I adapted from a script I had that worked fine. What happens is that I'm able to query and display the data just fine, but when I alter any of the information in the form and submit it, the data I updated disappears from the table. Any help in finding the problem would be greatly appreciated.

 

Thanks

 

Sean

 

 

 

<?php # Script 8.4 - edit_tracks.php

 

// This page edits a track.

// This page is accessed through view_tracks.php.

 

$page_title = 'Edit a Track';

include ('header.html');

 

// Check for a valid user ID, through GET or POST.

if ( (isset($_GET['id'])) && (is_numeric($_GET['id'])) ) { // Accessed through view_tracks.php

$id = $_GET['id'];

} elseif ( (isset($_POST['id'])) && (is_numeric($_POST['id'])) ) { // Form has been submitted.

$id = $_POST['id'];

} else { // No valid ID, kill the script.

echo '<h1 id="mainhead">Page Error</h1>

<p class="error">This page has been accessed in error.</p><p><br /><br /></p>';

include ('footer.html');

exit();

}

 

require_once ('mysql_connect.php'); // Connect to the db.

 

// Check if the form has been submitted.

if (isset($_POST['submitted'])) {

 

$errors = array(); // Initialize error array.

 

// Check for a artist name.

if (empty($_POST['artistName'])) {

$errors[] = 'You forgot to enter the Artist name.';

} else {

$an = ($_POST['artistName']);

}

 

// Check for a track #.

if (empty($_POST['SeqNum'])) {

$errors[] = 'You forgot to enter the Track #.';

} else {

$t = ($_POST['SeqNum']);

}

// Check for an track name.

if (empty($_POST['trackname'])) {

$errors[] = 'You forgot to enter the Track name.';

} else {

$tr = ($_POST['trackname']);

}

// Check for an ISRC.

if (empty($_POST['ISRC'])) {

$errors[] = 'You forgot to enter the ISRC #.';

} else {

$tr = ($_POST['ISRC']);

}

// Check for an time.

if (empty($_POST['duration'])) {

$errors[] = 'You forgot to enter the Track time.';

} else {

$tr = ($_POST['duration']);

}

// Check for an composer.

if (empty($_POST['composer'])) {

$errors[] = 'You forgot to enter the Composer.';

} else {

$tr = ($_POST['composer']);

}

 

if (empty($_POST['publisher'])) {

$errors[] = 'You forgot to enter the Publisher.';

} else {

$tr = ($_POST['publisher']);

}

 

if (empty($errors)) { // If everything's OK.

 

//  Test for unique email address.

$query = "SELECT ISRC FROM tracks WHERE trackname='$tn' AND ISRC != $id";

$result = mysql_query($query);

if (mysql_num_rows($result) == 0) {

 

// Make the query.

$query = "UPDATE tracks SET artistName='$an', SeqNum='$sn', trackname='$tn', ISRC='$is', duration='$du', composer='$co', publisher='$pu' WHERE ISRC=$id";

$result = @mysql_query ($query); // Run the query.

if (mysql_affected_rows() == 1) { // If it ran OK.

 

// Print a message.

echo '<h1 id="mainhead"></h1>

<p><strong><font size="+1"><em>The track has been edited.</em></font></strong></p><p><br /><br /></p>';

 

} else { // If it did not run OK.

echo '<h1 id="mainhead">System Error</h1>

<p class="error">The user could not be edited due to a system error. We apologize for any inconvenience.</p>'; // Public message.

echo '<p>' . mysql_error() . '<br /><br />Query: ' . $query . '</p>'; // Debugging message.

include ('footer.html');

exit();

}

 

} else { // Already registered.

//echo '<h1 id="mainhead">Error!</h1>

//<p class="error">The email address has already been registered.</p>';

}

 

} else { // Report the errors.

 

echo '<h1 id="mainhead">Error!</h1>

<p class="error">The following error(s) occurred:<br />';

foreach ($errors as $msg) { // Print each error.

echo " - $msg<br />\n";

}

echo '</p><p>Please try again.</p><p><br /></p>';

 

} // End of if (empty($errors)) IF.

 

} // End of submit conditional.

 

// Always show the form.

 

// Retrieve the user's information.

$query = "SELECT artistName, SeqNum, trackname, ISRC, duration, composer, publisher FROM tracks WHERE ISRC=$id";

//

$result = @mysql_query ($query); // Run the query.

 

if (mysql_num_rows($result) == 1) { // Valid user ID, show the form.

 

// Get the user's information.

$row = mysql_fetch_array ($result, MYSQL_NUM);

 

// Create the form.

echo '<h2>Edit a Track</h2>

<form action="edit_tracks.php" method="post">

<table width="200" border="0">

  <tr>

    <td>Artist:</td>

    <td> </td>

    <td><input type="text" name="artistName" size="45" maxlength="45" value="' . $row[0] . '" /></td>

  </tr>

  <tr>

    <td>Track #:</td>

    <td> </td>

    <td><input type="text" name="SeqNum" size="2" maxlength="2" value="' . $row[1] . '" /></td>

  </tr>

  <tr>

    <td>Track:</td>

    <td> </td>

    <td><input type="text" name="trackname" size="45" maxlength="45" value="' . $row[2] . '"  /></td>

  </tr>

  <tr> 

<td>ISRC:</td>

    <td> </td>

    <td><input type="text" name="ISRC" size="15" maxlength="15" value="' . $row[3] . '" /></td>

  </tr>

  <tr>

    <td>Time:</td>

    <td> </td>

    <td><input type="text" name="duration" size="6" maxlength="6" value="' . $row[4] . '" /></td>

  </tr>

  <tr>

    <td>Composer:</td>

    <td> </td>

    <td><input type="text" name="composer" size="45" maxlength="45" value="' . $row[5] . '" /></td>

  <tr>

  </tr> 

<td>Publisher:</td>

    <td> </td>

<td><input type="text" name="publisher" size="45" maxlength="45" value="' . $row[6] . '" /></td>

  </tr>

<tr></tr>

<tr>

    <td> </td>

    <td> </td>

<td><input type="submit" name="submit" value="Submit" /></td>

    <td><input type="hidden" name="submitted" value="TRUE" /></td>

    <td><input type="hidden" name="id" value="' . $id . '" /></td>

  </tr>

</table>

</form>';

 

} else { // Not a valid user ID.

//echo '<h1 id="mainhead">Page Error</h1>

//<p class="error">This page has been accessed in error.</p><p><br /><br /></p>';

}

 

mysql_close(); // Close the database connection.

 

include ('footer.html');

?>

Link to comment
https://forums.phpfreaks.com/topic/73774-updating-data-problem/
Share on other sites

Before anybody tries answering the question do you mind posting your code in the code or php tags?

 

But, try using error reporting.

 

$query = "UPDATE tracks SET artistName='$an', SeqNum='$sn', trackname='$tn', ISRC='$is', duration='$du', composer='$co', publisher='$pu' WHERE ISRC='$id'";
$result = mysql_query($query) or die(mysql_error());

Link to comment
https://forums.phpfreaks.com/topic/73774-updating-data-problem/#findComment-372228
Share on other sites

Sorry about that. Just tried the code, doesn't delete anything, but then it doesn't update the data either....

<?php # Script 8.4 - edit_tracks.php

// This page edits a track.
// This page is accessed through view_tracks.php.

$page_title = 'Edit a Track';
include ('header.html');

// Check for a valid user ID, through GET or POST.
if ( (isset($_GET['id'])) && (is_numeric($_GET['id'])) ) { // Accessed through view_tracks.php
$id = $_GET['id'];
} elseif ( (isset($_POST['id'])) && (is_numeric($_POST['id'])) ) { // Form has been submitted.
$id = $_POST['id'];
} else { // No valid ID, kill the script.
echo '<h1 id="mainhead">Page Error</h1>
<p class="error">This page has been accessed in error.</p><p><br /><br /></p>';
include ('footer.html'); 
exit();
}

require_once ('mysql_connect.php'); // Connect to the db.

// Check if the form has been submitted.
if (isset($_POST['submitted'])) {

$errors = array(); // Initialize error array.

// Check for a artist name.
if (empty($_POST['artistName'])) {
	$errors[] = 'You forgot to enter the Artist name.';
} else {
	$an = ($_POST['artistName']);
}

// Check for a track #.
if (empty($_POST['SeqNum'])) {
	$errors[] = 'You forgot to enter the Track #.';
} else {
	$t = ($_POST['SeqNum']);
}
// Check for an track name.
if (empty($_POST['trackname'])) {
	$errors[] = 'You forgot to enter the Track name.';
} else {
	$tr = ($_POST['trackname']);
}
// Check for an ISRC.
if (empty($_POST['ISRC'])) {
	$errors[] = 'You forgot to enter the ISRC #.';
} else {
	$tr = ($_POST['ISRC']);
}
// Check for an time.
if (empty($_POST['duration'])) {
	$errors[] = 'You forgot to enter the Track time.';
} else {
	$tr = ($_POST['duration']);
}
// Check for an composer.
if (empty($_POST['composer'])) {
	$errors[] = 'You forgot to enter the Composer.';
} else {
	$tr = ($_POST['composer']);
}

if (empty($_POST['publisher'])) {
	$errors[] = 'You forgot to enter the Publisher.';
} else {
	$tr = ($_POST['publisher']);
}

if (empty($errors)) { // If everything's OK.

	//  Test for unique email address.
	$query = "SELECT ISRC FROM tracks WHERE trackname='$tn' AND ISRC != $id";
	$result = mysql_query($query);
	if (mysql_num_rows($result) == 0) {

		// Make the query.
		$query = "UPDATE tracks SET artistName='$an', SeqNum='$sn', trackname='$tn', ISRC='$is', duration='$du', composer='$co', publisher='$pu' WHERE ISRC=$id";
		$result = @mysql_query ($query); // Run the query.
		if (mysql_affected_rows() == 1) { // If it ran OK.

			// Print a message.
			echo '<h1 id="mainhead"></h1>
			<p><strong><font size="+1"><em>The track has been edited.</em></font></strong></p><p><br /><br /></p>';	

		} else { // If it did not run OK.
			echo '<h1 id="mainhead">System Error</h1>
			<p class="error">The user could not be edited due to a system error. We apologize for any inconvenience.</p>'; // Public message.
			echo '<p>' . mysql_error() . '<br /><br />Query: ' . $query . '</p>'; // Debugging message.
			include ('footer.html'); 
			exit();
		}

	} else { // Already registered.
		//echo '<h1 id="mainhead">Error!</h1>
		//<p class="error">The email address has already been registered.</p>';
	}

} else { // Report the errors.

	echo '<h1 id="mainhead">Error!</h1>
	<p class="error">The following error(s) occurred:<br />';
	foreach ($errors as $msg) { // Print each error.
		echo " - $msg<br />\n";
	}
	echo '</p><p>Please try again.</p><p><br /></p>';

} // End of if (empty($errors)) IF.

} // End of submit conditional.

// Always show the form.

// Retrieve the user's information.
$query = "SELECT artistName, SeqNum, trackname, ISRC, duration, composer, publisher FROM tracks WHERE ISRC=$id";		
// 
$result = @mysql_query ($query); // Run the query.

if (mysql_num_rows($result) == 1) { // Valid user ID, show the form.

// Get the user's information.
$row = mysql_fetch_array ($result, MYSQL_NUM);

// Create the form.
echo '<h2>Edit a Track</h2>
<form action="edit_tracks.php" method="post">
<table width="200" border="0">
  <tr>
    <td>Artist:</td>
    <td> </td>
    <td><input type="text" name="artistName" size="45" maxlength="45" value="' . $row[0] . '" /></td>
  </tr>
   <tr>
    <td>Track #:</td>
    <td> </td>
    <td><input type="text" name="SeqNum" size="2" maxlength="2" value="' . $row[1] . '" /></td>
  </tr>
  <tr>
    <td>Track:</td>
    <td> </td>
    <td><input type="text" name="trackname" size="45" maxlength="45" value="' . $row[2] . '"  /></td>
  </tr>
  <tr>  
<td>ISRC:</td>
    <td> </td>
    <td><input type="text" name="ISRC" size="15" maxlength="15" value="' . $row[3] . '" /></td>
  </tr>
  <tr>
    <td>Time:</td>
    <td> </td>
    <td><input type="text" name="duration" size="6" maxlength="6" value="' . $row[4] . '" /></td>
  </tr>
  <tr>
    <td>Composer:</td>
    <td> </td>
    <td><input type="text" name="composer" size="45" maxlength="45" value="' . $row[5] . '" /></td>
  <tr>
  </tr>  
<td>Publisher:</td>
    <td> </td>
 <td><input type="text" name="publisher" size="45" maxlength="45" value="' . $row[6] . '" /></td>
  </tr>
<tr></tr>
<tr>
    <td> </td>
    <td> </td>
<td><input type="submit" name="submit" value="Submit" /></td>
    <td><input type="hidden" name="submitted" value="TRUE" /></td>
    <td><input type="hidden" name="id" value="' . $id . '" /></td>
  </tr>
</table>
</form>';

} else { // Not a valid user ID.
//echo '<h1 id="mainhead">Page Error</h1>
//<p class="error">This page has been accessed in error.</p><p><br /><br /></p>';
}

mysql_close(); // Close the database connection.

include ('footer.html');
?>

Link to comment
https://forums.phpfreaks.com/topic/73774-updating-data-problem/#findComment-372239
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.