Jump to content

Recommended Posts

Hi everyone,

 

The following pages are not inserting information into the database as intended.

 

I have a feeling that the ISSET function at the top of tracks.php is not firing -  the POSTed values seem to be being sent as they are prefilled into the form after submitting.

 

Can anyone tell me why it isn't working?

 

Thanks

KN

 

tracks.php

<?php


define ('ROOT', $_SERVER['DOCUMENT_ROOT']);

// start session. Put it above define
/*
session_start('username');


$username = ($_SESSION['username']);

*/




?>


<!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">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<meta name="Keywords" content="mojo, cd, cds, free, compact, disc, album, record, magazine, mag, cover, covers" />
<meta name="Description" content="Mojo Magazine's Free Cover CDs" />
<meta http-equiv="Content-Language" content="EN" />
<meta name="charset" content="ISO-8859-1" />
<meta name="robots" content="index,follow" />
<meta name="Author" content="Gary Carpenter" />
<meta name="copyright" content="Copyright © 2010" />
<title>Mojo Cover CDs - Every CD, every track. Your search results.</title>
<link rel="shortcut icon" href="../images/favicon.ico" type="images/favicon" />
<link type="text/css" href="/style/mojo_home.css" rel="stylesheet" />
<script src="/style/jquery-1.4.2.min.js" type="text/javascript"></script>
<script type="text/javascript" src="/style/prepareImageSwap.js"></script>
</head>

<body>

<?php


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

// nothing happens with these errors yet. Could apend to form or just highlight bad fields in red.

$errorList = array();

	if (trim($_POST['track_no']) == '')
		{
			$errorList[] = 'Invalid Entry: Track No.';
		}

	if (trim($_POST['track_title']) == '')
		{
			$errorList[] = 'Invalid Entry: Track Title';
		}

	if (trim($_POST['track_artist']) == '')
		{
			$errorList[] = 'Invalid Entry: Track Artist';
		}

// Check for errors. If none found go to add_to_db page and insert data.

	if (sizeOf($errorlist) == 0)
		{


			$moj_id = $_POST['id'];
			$track_no = mysql_real_escape_string($_POST['track_no']);
			$track_title = mysql_real_escape_string($_POST['track_title']);
			$track_artist = mysql_real_escape_string($_POST['track_artist']);
			$track_asinuk = mysql_real_escape_string(trim($_POST['track_asinuk']));
			$track_no = mysql_real_escape_string(trim($_POST['track_asinus']));
			include (ROOT.'/includes/add_track_to_db.php');
		// } dont forget to put this back when reinstating error check

}

}


?>

<div id = "wrapper">
<div id = "left">

<?php

include (ROOT.'/includes/header.php');
include (ROOT.'/includes/conf.php');




// Check for issue number

if(!isset($_GET['id']) || trim($_GET['id'] == ''))
	{
		trigger_error('No issue  present' . mysql_error($db), E_USER_ERROR);
	}

// Open DB connection
	$connection = mysql_connect($host, $user, $pass)
	or trigger_error('Unable to connect: ' . mysql_error($db), E_USER_ERROR);

// Select database
	mysql_select_db($db) or trigger_error('Unable to select database: ' . mysql_error($db), E_USER_ERROR);

// Retrieve current "issue" (Will be project dependent too)
$id = $_GET['id'];

	$query = "
	SELECT moj_title, moj_date, moj_issue, moj_summary
	FROM mojocd
	WHERE moj_id = '$id'";

	$result = mysql_query($query) or trigger_error('Query failed: ' . mysql_error($db), E_USER_ERROR);

// There should only be one issue
		if (mysql_num_rows($result) > 0) {

			while($row = mysql_fetch_object($result)){ ?>

			<p />

<div id="cover">

			<img src="../images/<?php echo $id; ?>.jpg" width="200" height="200" alt="Mojo CD: <?php echo $row->moj_title; ?>" title="mojo CD: <?php echo $row->moj_title; ?>"/>

</div> <!-- closes cover -->

<div id="cover-text">

			<h1 class="top"><?php echo $row->moj_title; ?></h1>
			<h2 class="top"><font size="4">Issue No. <?php echo $row->moj_issue; ?>  (<?php echo $row->moj_date; ?>) </font></h2>
			<b><?php echo $row->moj_summary; ?></b>

<?php			} // end while

		} // end if

		else { ?>

			<font size="-1">NO ISSUE PRESENT</font>

<?php		} // end else  ?>

</div> <!-- close cover-text -->


<div id = "content">

<?php

// Retrieve the tracks

	$id = $_GET['id'];
	$query = "
	SELECT track_id, track_no, track_title, track_artist, track_asinuk, track_asinus
	FROM tracks
	WHERE moj_id = '$id'
	ORDER BY track_no
	";

	$result = mysql_query($query)
	or trigger_error('Query failed: ' . mysql_error($db), E_USER_ERROR); ?>

<p />



<?php

// if records present

	if (mysql_num_rows($result) > 0) { ?>

		<p class="strap"><b>Links go directly to full artist's album on which each track appears</b>

<!-- removed to allow simple list while experimenting 
<div="tracks-table">
<p>
<table class = "tracks">
-->	

			<ul id="tracks">

<?php 			while($row = mysql_fetch_object($result)) {

				echo '<li>' . $row->track_no . ': ' . $row->track_title . ' - ' . $row->track_artist . ' ::: UK:' . $row->track_asinuk . ' ::: US:' . $row->track_asinus . '</li>';

			} // end while

		echo '</ul>';

	} // end if (rows present)

	else {

	echo '<p /><b>NO TRACKS ENTERED.</b>';

	} // end else 

		?>


<!-- FORM CODE GOES HERE. IDEALLY IT WILL DROP OUT AS ANOTHER TABLE ITEM IN REAL THING $_SERVER['PHP_SELF']-->


<form action="tracks.php?id=<?php echo $id; ?>" method="post">

			<input type="hidden" name="moj_id" id="moj_id" value="<?php echo $id; ?>">

			<label for="track_no">Track No:</label>
			<input type="text" size="4" maxlength="4" name="track_no" id="track_no" value="<?php echo $_POST['track_no']; ?>" /><br />

			<label for="track_title">Track Title:</label> 
			<input type="text" size="50" maxlength="254" name="track_title" id="track_title" value="<?php echo $_POST['track_title']; ?>" /><br />

			<label for="track_artist">Track Artist:</label> 
			<input type="text" size="50" maxlength="254" name="track_artist" id="track_artist" value="<?php echo $_POST['track_artist']; ?>" /><br />

			<label for="track_asinuk">Amazon Asin UK:</label>
			<input type="text" size="50" maxlength="254" name="track_asinuk" id="track_asinuk" value="<?php echo $_POST['track_asinuk']; ?>" /><br />

			<label for="track_asinus">Amazon Asin US:</label>
			<input type="text" size="50" maxlength="254" name="track_asinus" id="track_asinus" value="<?php echo $_POST['track_asinus']; ?>" />

			<input type="submit" name="submit" id="submit" value="Add" />

	</form>



<?php

if (isset ($_GET['fromsearch'])) { ?>

	<br />
		<form>
		<input type="button" value="Back to search results" onClick="history.back()">
		</form>

<?php

} //end if (fromsearch) 

	include(ROOT.'/includes/random_cds.php'); ?>

</div> <!-- closes Content -->

<!-- PAGE SPECIFIC CONTENT ENDS HERE -->

<?php

// Sidebar, Tag Cloud and Footer as include

	include(ROOT.'/includes/footer.php'); ?>

</div> <!-- closes left -->

<div id = "right">

<?php
	include (ROOT.'/includes/extra.php'); ?>

</div> <!-- closes right -->
</div> <!-- closes wrapper -->

<?php	include_once(ROOT.'/includes/analyticstracking.php'); ?>


</body>
</html>


 

add_track_to_db.php

<?php

require (ROOT.'/includes/conf.php');

/*
if(IS_AJAX) {
$moj_id = $_POST['moj_id'];
$track_no = $_POST['track_no'];
$track_title= $_POST['track_title'];
$track_artist= $_POST['track_artist'];
$track_asinuk= $_POST['track_asinuk'];
$track_asinus= $_POST['track_asinus'];
}
*/


$connection = mysql_connect($host, $user, $pass)
or trigger_error('Unable to connect: ' . mysql_error($db), E_USER_ERROR);

// select database
mysql_select_db($db)
or trigger_error('Query failed: ' . mysql_error($db), E_USER_ERROR);



	// generate and execute query
$query = "INSERT INTO
tracks(moj_id, track_no, track_title, track_artist, track_asinuk, track_asinus)
VALUES ('$moj_id', '$track_no', '$track_title', '$track_artist', '$track_asinuk', '$track_asinus')";


$result = mysql_query($query)
or trigger_error('Query failed: ' . mysql_error($db), E_USER_ERROR);

if (!$result){

echo "There was an error on the add_track_to_db page";

}


/*
else{

if(IS_AJAX) {
	echo '<h3>' . $track_title . '</h3>';
	echo '<p>' . $track_artist . '</p>';
}
else{ 

	header("location:ROOT.'/pages/tracks.php?id=$moj_id'");

}   

*/

?>

Link to comment
https://forums.phpfreaks.com/topic/201085-form-submit-problems/
Share on other sites

$moj_id = $_POST['id'];

 

Your form does not have a POST field named 'id', so the above line of code won't have a value and in fact the reference to $_POST['id'] would be generating a php error.

 

Are you developing and debugging php code on a system with error_reporting set to E_ALL and display_errors set to ON so that all the php errors would be reported and displayed, including the output from your trigger_error() code?

 

The above would insert an empty value if your INSERT query is being executed. Have you determined (by echoing some debugging messages) that the code where the add_track_to_db.php code is being included is actually being executed and the include is working? Having error_reporting/display_errors set as suggested would be telling you if the include is not working due to a path issue.

Thanks for the reply.

 

I am updating a website to include jquery. When I made the site live I was advised not to use "or die" in the event of errors and to change them all to E_USER_ERROR, though to be honest I didn't know how to access these reports.

 

When I find out I'll use them to find the problems and come back if I'm still stuck.

 

Cheers.

In checking what your code is actually doing, it is unlikely that you have ever successfully executed that code.

 

You are using the variable $db in the mysql_error() statements, but $db holds your db name, not the connection variable $connection and in the form processing section, you are using mysql_real_escape_string() without having a mysql connection present first.

 

You also have a mysql_error() in the code that is executed if $_GET['id'] does not have any value.

I didn't know how to access these reports.

 

My post asked (told) you how -

Are you developing and debugging php code on a system with error_reporting set to E_ALL and display_errors set to ON so that all the php errors would be reported and displayed, including the output from your trigger_error() code?

The preferred method would be to set them in the master php.ini, followed by either a local php.ini (when php is running as a CGI application) or a .htaccess file (when php is running as an Apache Module), or lastly by putting them into your script (since fatal parse errors won't be reported/displayed when the two settings are put into a script.)

 

The display_errors setting causes errors to be displayed. The log_errors setting would cause errors to be logged.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.