| | |
php script
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Nov 2005
Posts: 44
Reputation:
Solved Threads: 0
Im having problems with this script. Right now it adds everthing to the database if I take this out
[php]
<p><b>Genre:</b>
<p><input type="radio" name="genre" value="existing" /> Existing =>
<select name="existing"> <option>Select One</option>
<?php // Retrieve all the artists and add to the pull-down menu.
$query = "SELECT genre_id, (genre_name) AS name FROM track_genre ORDER BY genre_name ASC";
$result = mysql_query($query);
while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {
echo "<option value=\"{$row['genre_id']}\">{$row['name']}</option>\n";
}
mysql_close(); // Close the database connection.
?>
</select>
</p>
[/php]
however i have a track_genre table in my database which is populated with genres. I want to query it to retrieve those values and add the value into the track table in my database track_ genre field I also have a track genre field in my track table. can someone point me in the right direction
[php]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Add a Print</title>
</head>
<body>
<?php # Script 14.2 - add_print.php
// This page allows the administrator to add a print (product).
require_once ('../mysql_connect.php'); // Connect to the database.
if (isset($_POST['submitted'])) { // Check if the form has been submitted.
// Validate the print_name, image, artist (existing or first_name, last_name, middle_name), size, price, and description.
// Check for a track name.
if (!empty($_POST['track_name'])) {
$tn = escape_data($_POST['track_name']);
} else {
$tn = FALSE;
echo '<p class="red"><font color="red">Please enter the track\'s name!</font></p>';
}
// Check for an image.
if (is_uploaded_file ($_FILES['image']['tmp_name'])) {
if (move_uploaded_file($_FILES['image']['tmp_name'], "../uploads/{$_FILES['image']['name']}")) { // Move the file over.
echo '<p>The file has been uploaded!</p>';
} else { // Couldn't move the file over.
echo '<p><font color="red">The file could not be moved.</font></p>';
$i = FALSE;
}
$i = $_FILES['image']['name'];
} else {
$i = FALSE;
}
// Check for a size (not required).
if (!empty($_POST['track_label'])) {
$s = escape_data($_POST['track_label']);
} else {
$s = '<i>Size information not available.</i>';
}
// Check for a price.
//if (is_numeric($_POST['price'])) {
// $p = (float) $_POST['price'];
//} else {
//$p = FALSE;
//echo '<p><font color="red">Please enter the print\'s price!</font></p>';
//}
// Check for a description (not required).
if (!empty($_POST['track_credits'])) {
$d = escape_data($_POST['track_credits']);
} else {
$d = '<i>No description available.</i>';
}
if ( ($_POST['genre'] == 'existing') && ($_POST['existing'] > 0)) { // Existing album.
$b = (int) $_POST['existing'];
} else { // No artist selected.
$b = FALSE;
echo '<p><font color="red">Please enter or select the track\'s artist!</font></p>';
}
// Validate the album.
if ($_POST['artist'] == 'new') {
// If it's a new album, add the album to the database.
$query = 'INSERT INTO artist (artist_name) VALUES (';
if (!empty($_POST['artist_name'])) {
$query .= "'" . escape_data($_POST['artist_name']) . "')";
//} else {
//$query .= 'NULL, ';
//}
//if (!empty($_POST['middle_name'])) {
//$query .= "'" . escape_data($_POST['middle_name']) . "', ";
//} else {
// $query .= 'NULL, ';
// }
// Check for a last_name.
//if (!empty($_POST['last_name'])) {
//$query .= "'" . escape_data($_POST['last_name']) . "')";
// Improved MySQL Version:
$result = mysql_query($query);
$a = mysql_insert_id();
/* Standard MySQL Version:
$result = mysql_query ($query); // Run the query.
$a = mysql_insert_id(); // Get the artist ID.
*/
} else { // No last name value.
$a = FALSE;
echo '<p><font color="red">Please enter the artist\'s name!</font></p>';
}
} elseif ( ($_POST['artist'] == 'existing') && ($_POST['existing'] > 0)) { // Existing album.
$a = (int) $_POST['existing'];
} else { // No artist selected.
$a = FALSE;
echo '<p><font color="red">Please enter or select the track\'s artist!</font></p>';
}
if ($tn && $b && $a && $i) { // If everything's OK.
// Add the print to the database.
$query = "INSERT INTO track (artist_id, track_name, track_label, track_genre, track_credits, image_name) VALUES ($a, '$tn', '$s', '$b', '$d', '$i')";
if ($result = mysql_query($query)) { // Worked.
echo '<p>The print has been added.</p>';
} else { // If the query did not run OK.
echo '<p><font color="red">Your submission could not be processed due to a system error.</font></p>';
}
} else { // Failed a test.
echo '<p><font color="red">Please click "back" and try again.</font></p>';
}
} else { // Display the form.
?>
<form enctype="multipart/form-data" action="add_track.php" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="524288"/>
<fieldset><legend>Fill out the form to create a Track:</legend>
<p><b>Track Name:</b> <input type="text" name="track_name" size="30" maxlength="100" /></p>
<p><b>Track:</b> <input type="file" name="image" /> <small>The file name should not include spaces or other invalid characters and should have a file extension.</small></p>
<p><b>Artist:</b>
<p><input type="radio" name="artist" value="existing" /> Existing =>
<select name="existing"><option>Select One</option>
<?php // Retrieve all the artists and add to the pull-down menu.
$query = "SELECT artist_id,(artist_name) AS name FROM artist";
$result = mysql_query($query);
while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {
echo "<option value=\"{$row['artist_id']}\">{$row['name']}</option>\n";
}
mysql_close(); // Close the database connection.
?>
</select></p>
<p>
<input type="radio" name="artist" value="new" /> New =>
Artist Name: <input type="text" name="artist_name" size="10" maxlength="75" />
</p>
<p><b>Genre:</b>
<p><input type="radio" name="genre" value="existing" /> Existing =>
<select name="existing"> <option>Select One</option>
<?php // Retrieve all the artists and add to the pull-down menu.
$query = "SELECT genre_id, (genre_name) AS name FROM track_genre ORDER BY genre_name ASC";
$result = mysql_query($query);
while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {
echo "<option value=\"{$row['genre_id']}\">{$row['name']}</option>\n";
}
mysql_close(); // Close the database connection.
?>
</select>
</p>
<p><b>Label:</b>
<input type="text" name="track_label" size="30" maxlength="255" /> </p>
<p><b>Track credits :</b>
<textarea name="track_credits" cols="40" rows="5"></textarea></p>
</fieldset>
<div align="center"><input type="submit" name="submit" value="Submit" /></div>
<input type="hidden" name="submitted" value="TRUE" />
</form>
<?php
} // End of main conditional.
?>
</body>
</html>[/php]
[php]
<p><b>Genre:</b>
<p><input type="radio" name="genre" value="existing" /> Existing =>
<select name="existing"> <option>Select One</option>
<?php // Retrieve all the artists and add to the pull-down menu.
$query = "SELECT genre_id, (genre_name) AS name FROM track_genre ORDER BY genre_name ASC";
$result = mysql_query($query);
while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {
echo "<option value=\"{$row['genre_id']}\">{$row['name']}</option>\n";
}
mysql_close(); // Close the database connection.
?>
</select>
</p>
[/php]
however i have a track_genre table in my database which is populated with genres. I want to query it to retrieve those values and add the value into the track table in my database track_ genre field I also have a track genre field in my track table. can someone point me in the right direction
[php]
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="content-type" content="text/html; charset=iso-8859-1" />
<title>Add a Print</title>
</head>
<body>
<?php # Script 14.2 - add_print.php
// This page allows the administrator to add a print (product).
require_once ('../mysql_connect.php'); // Connect to the database.
if (isset($_POST['submitted'])) { // Check if the form has been submitted.
// Validate the print_name, image, artist (existing or first_name, last_name, middle_name), size, price, and description.
// Check for a track name.
if (!empty($_POST['track_name'])) {
$tn = escape_data($_POST['track_name']);
} else {
$tn = FALSE;
echo '<p class="red"><font color="red">Please enter the track\'s name!</font></p>';
}
// Check for an image.
if (is_uploaded_file ($_FILES['image']['tmp_name'])) {
if (move_uploaded_file($_FILES['image']['tmp_name'], "../uploads/{$_FILES['image']['name']}")) { // Move the file over.
echo '<p>The file has been uploaded!</p>';
} else { // Couldn't move the file over.
echo '<p><font color="red">The file could not be moved.</font></p>';
$i = FALSE;
}
$i = $_FILES['image']['name'];
} else {
$i = FALSE;
}
// Check for a size (not required).
if (!empty($_POST['track_label'])) {
$s = escape_data($_POST['track_label']);
} else {
$s = '<i>Size information not available.</i>';
}
// Check for a price.
//if (is_numeric($_POST['price'])) {
// $p = (float) $_POST['price'];
//} else {
//$p = FALSE;
//echo '<p><font color="red">Please enter the print\'s price!</font></p>';
//}
// Check for a description (not required).
if (!empty($_POST['track_credits'])) {
$d = escape_data($_POST['track_credits']);
} else {
$d = '<i>No description available.</i>';
}
if ( ($_POST['genre'] == 'existing') && ($_POST['existing'] > 0)) { // Existing album.
$b = (int) $_POST['existing'];
} else { // No artist selected.
$b = FALSE;
echo '<p><font color="red">Please enter or select the track\'s artist!</font></p>';
}
// Validate the album.
if ($_POST['artist'] == 'new') {
// If it's a new album, add the album to the database.
$query = 'INSERT INTO artist (artist_name) VALUES (';
if (!empty($_POST['artist_name'])) {
$query .= "'" . escape_data($_POST['artist_name']) . "')";
//} else {
//$query .= 'NULL, ';
//}
//if (!empty($_POST['middle_name'])) {
//$query .= "'" . escape_data($_POST['middle_name']) . "', ";
//} else {
// $query .= 'NULL, ';
// }
// Check for a last_name.
//if (!empty($_POST['last_name'])) {
//$query .= "'" . escape_data($_POST['last_name']) . "')";
// Improved MySQL Version:
$result = mysql_query($query);
$a = mysql_insert_id();
/* Standard MySQL Version:
$result = mysql_query ($query); // Run the query.
$a = mysql_insert_id(); // Get the artist ID.
*/
} else { // No last name value.
$a = FALSE;
echo '<p><font color="red">Please enter the artist\'s name!</font></p>';
}
} elseif ( ($_POST['artist'] == 'existing') && ($_POST['existing'] > 0)) { // Existing album.
$a = (int) $_POST['existing'];
} else { // No artist selected.
$a = FALSE;
echo '<p><font color="red">Please enter or select the track\'s artist!</font></p>';
}
if ($tn && $b && $a && $i) { // If everything's OK.
// Add the print to the database.
$query = "INSERT INTO track (artist_id, track_name, track_label, track_genre, track_credits, image_name) VALUES ($a, '$tn', '$s', '$b', '$d', '$i')";
if ($result = mysql_query($query)) { // Worked.
echo '<p>The print has been added.</p>';
} else { // If the query did not run OK.
echo '<p><font color="red">Your submission could not be processed due to a system error.</font></p>';
}
} else { // Failed a test.
echo '<p><font color="red">Please click "back" and try again.</font></p>';
}
} else { // Display the form.
?>
<form enctype="multipart/form-data" action="add_track.php" method="post">
<input type="hidden" name="MAX_FILE_SIZE" value="524288"/>
<fieldset><legend>Fill out the form to create a Track:</legend>
<p><b>Track Name:</b> <input type="text" name="track_name" size="30" maxlength="100" /></p>
<p><b>Track:</b> <input type="file" name="image" /> <small>The file name should not include spaces or other invalid characters and should have a file extension.</small></p>
<p><b>Artist:</b>
<p><input type="radio" name="artist" value="existing" /> Existing =>
<select name="existing"><option>Select One</option>
<?php // Retrieve all the artists and add to the pull-down menu.
$query = "SELECT artist_id,(artist_name) AS name FROM artist";
$result = mysql_query($query);
while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {
echo "<option value=\"{$row['artist_id']}\">{$row['name']}</option>\n";
}
mysql_close(); // Close the database connection.
?>
</select></p>
<p>
<input type="radio" name="artist" value="new" /> New =>
Artist Name: <input type="text" name="artist_name" size="10" maxlength="75" />
</p>
<p><b>Genre:</b>
<p><input type="radio" name="genre" value="existing" /> Existing =>
<select name="existing"> <option>Select One</option>
<?php // Retrieve all the artists and add to the pull-down menu.
$query = "SELECT genre_id, (genre_name) AS name FROM track_genre ORDER BY genre_name ASC";
$result = mysql_query($query);
while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) {
echo "<option value=\"{$row['genre_id']}\">{$row['name']}</option>\n";
}
mysql_close(); // Close the database connection.
?>
</select>
</p>
<p><b>Label:</b>
<input type="text" name="track_label" size="30" maxlength="255" /> </p>
<p><b>Track credits :</b>
<textarea name="track_credits" cols="40" rows="5"></textarea></p>
</fieldset>
<div align="center"><input type="submit" name="submit" value="Submit" /></div>
<input type="hidden" name="submitted" value="TRUE" />
</form>
<?php
} // End of main conditional.
?>
</body>
</html>[/php]
Last edited by cscgal; May 22nd, 2006 at 12:02 pm.
![]() |
Similar Threads
- How can I send variables from a PHP script to another URL using POST without......... (PHP)
- how to make member expire after so many days with php script (PHP)
- How many clients can access a PHP script at a time from a MySQL server? (MySQL)
- How many clients can access a PHP script at a time? (PHP)
- how to find stock information with php script (PHP)
- need help with PHP Script translation (PHP)
Other Threads in the PHP Forum
- Previous Thread: tables
- Next Thread: PHP's include function ???
| Thread Tools | Search this Thread |
301 access apache api array autocomplete beginner binary broken button cakephp checkbox class cms code compression cron curl data database date display dropdown dropdownlist duplicates dynamic echo email error execution file files folder form forms function functions google href htaccess html htmlspecialchars httppost image include insert integration ip javascript joomla jquery limit link links login mail md5 menu methods mlm multiple mysql oop paypal pdf pdfdownload php phpvotingscript problem query radio random recursion remote script search secure server session sessions sms source space sql subscription syntax system table tutorial update upload url validator variable video virus volume votedown web youtube





