Decrease field using down button

Novice to php / mysql: I have a table with several fields , two of them  (id, seats). I want to create a form with 1 button that essentially update the 'seats' field of the rendered row.

Here is what I have been trying to make:

$database->setQuery("SELECT training.training, 
DATE_FORMAT(training.trainingDate, '%W, %M %e, %Y'),
training.trainingHour, training.course_location,
date_format(jos_jquarks_persontraining.timeStamp,'%m/%d/%Y')  
FROM jos_jquarks_persontraining 
INNER JOIN training ON  jos_jquarks_persontraining.training_id = training.training_id
WHERE
  jos_jquarks_persontraining.user_id =  " .$id  ) ;


if (!$database->query()) { //write data and if error occurs alert
    echo "<script> alert('".$database->getErrorMsg()."'); </script>";
}
$tableStyle = "padding: 5px;border:1px solid black"; 
$tdStyle = "padding:5px"; 
$thStyle = "padding:7px";

$row = $database->loadRowList();
if ( 0<count($row)) {
	echo '<table style="', $tableStyle, '" cellpadding="7" cellspacing="7">'; // with echo, commas are slightly more effective than dots 
	echo '<tr><th style="', $thStyle, '" align=center>Course Title</th><th style="', $thStyle, '" align=center>Date </th>
	<th style="', $thStyle, '" align=center>Time</th>
<th style="', $thStyle, '" align=center>Location</th>

<th style="', $thStyle, '" align=center>Enrollment Date</th>
<th style="', $thStyle, '" align=center>Action</th>'; 
 
	
	$row = $database->loadRowList();
	foreach($row as $valuearray) {
		echo '<tr align="center">';
		
		foreach($valuearray as $field){
			echo '<td style="', $tdStyle, '" align=center>', $field, '</td>'; 
		} // field
		echo '</tr>';
	} // $valuearray
	echo '</table>'; 
} // if count

Open in new window

LVL 9
TonyRebaAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Ray PaseurCommented:
Suggest you get this little book - it will bring you up to speed quickly on the basics of PHP and MySQL, especially useful if you have some other programming experience.
http://www.sitepoint.com/books/phpmysql4/

To the instant question, this might best be done in jQuery.  Hit the request attention button and ask a moderator to add your question to the jQuery and JavaScript zones.  I think you'll get good answers there.

Best regards, ~Ray
0
TonyRebaAuthor Commented:
yes. forgot to delete.
0
Become a Certified Penetration Testing Engineer

This CPTE Certified Penetration Testing Engineer course covers everything you need to know about becoming a Certified Penetration Testing Engineer. Career Path: Professional roles include Ethical Hackers, Security Consultants, System Administrators, and Chief Security Officers.

TonyRebaAuthor Commented:
this is my latest try,  does this code makes sense, can you please tell me if there is something wrong?
/* New Code */

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

function update() 
{  
// Delete and decrease seats
$database =& JFactory::getDBO();
$database->setQuery("DELETE * FROM jos_jquarks_persontraining WHERE
  user_id = " .$id  ) or die(mysql_error()); 
}

}
$database =& JFactory::getDBO();
// Display enrollments
echo "<h1>";
echo "<span style='text-decoration: underline;'>";
echo "Courses : " ;
echo "</h1>";
echo "</b>";

echo "</br>";
echo "</br>";

$database->setQuery("SELECT training.training, 
DATE_FORMAT(training.trainingDate, '%W, %M %e, %Y'),
training.trainingHour, training.course_location,
date_format(jos_jquarks_persontraining.timeStamp,'%m/%d/%Y')  
FROM jos_jquarks_persontraining 
INNER JOIN training ON  jos_jquarks_persontraining.training_id = training.training_id
WHERE
  jos_jquarks_persontraining.user_id =  " .$id  ) ;

if (!$database->query()) 
{ 
    echo "<script> alert('".$database->getErrorMsg()."'); </script>";
}

$tableStyle = "padding: 5px;border:1px solid black"; 
$tdStyle = "padding:5px"; 
$thStyle = "padding:7px";

$row = $database->loadRowList();
if ( 0<count($row)) {
	echo '<table style="', $tableStyle, '" cellpadding="7" cellspacing="7">';  
	echo '<tr><th style="', $thStyle, '" align=center>Course</th><th style="', $thStyle, '" align=center>Date</th>
	<th style="', $thStyle, '" align=center>Time</th>
<th style="', $thStyle, '" align=center>Location</th>

<th style="', $thStyle, '" align=center>Enrollment Date</th>
<th style="', $thStyle, '" align=center>Action</th>'; 
 
	
	$row = $database->loadRowList();
	foreach($row as $valuearray) {
		echo '<tr align="center">';
		
		foreach($valuearray as $field){
			echo '<td style="', $tdStyle, '" align=center>', $field, '</td>'; 

		} // field
		echo '</tr>';

echo '<tr align="center">';
echo'<td>';
echo '<input type="button" id="btn_update" name="btn_update" value="Unenroll">';
echo '</td>';
echo '</tr>';


	} // $valuearray
	echo '</table>'; 
} // if count
?>

Open in new window

0
Ray PaseurCommented:
Looking at this again with a fresh set of eyes... Is this Joomla? If so, we have a Joomla Zone here at EE.  You might ask a moderator to add this question to that zone.  

But seriously, buy that SitePoint book.  Here are some examples of why you need to get an underpinning of general knowledge about PHP.  Consider the code snippet.
function update() 
{  
// Delete and decrease seats
$database =& JFactory::getDBO();
$database->setQuery("DELETE * FROM jos_jquarks_persontraining WHERE
  user_id = " .$id  ) or die(mysql_error()); 
}

Open in new window

It looks as if the script defines the function update() but never uses it.  When you get around to using it, there will be some issues.  Look at the $id variable.  It is undefined in the scope of the function, so the function will almost certainly not do what you expect.  You might need to pass it into the function from the calling statement, something like function update($id) {...
http://php.net/manual/en/language.variables.scope.php

However, there is still another issue here, and that is that $id is undefined in the logic flow of the entire script.  You can add error_reporting(E_ALL) to the top of your scripts and PHP will tell you when you have relied on an undefined variable.  But without that, PHP will just ignore undefined variables, silently substituting zero, NULL, FALSE or something like that.  That is a pretty good way to ensure that catastrophe is not left to chance!

Consider this statement: $database->setQuery(...  What does that do?  Does it set the query or run the query?  If it sets the query, and your script does not call something like $database->query(), there is not much meaning or effect.  That kind of makes it unlikely that you would ever get to mysql_error()

Not sure about the query syntax, either.  It might work with DELETE * FROM, but I have always seen it written DELETE FROM... and the manual seems to suggest that the * should be omitted.
http://dev.mysql.com/doc/refman/5.0/en/delete.html

If it seems like there are a lot of issues with a function that only executes two statements, it's because of the complex interplay of many factors in programming.  The SitePoint book will help you get past those issues and build a good foundation for your future work with PHP and MySQL.

Best regards, and Happy New Year, ~Ray

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
TonyRebaAuthor Commented:
Thanks you were right, I was omitting the call of the function. Happy New Year Too.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
PHP

From novice to tech pro — start learning today.