• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 258
  • Last Modified:

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

0
TonyReba
Asked:
TonyReba
  • 3
  • 3
1 Solution
 
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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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
 
TonyRebaAuthor Commented:
Thanks you were right, I was omitting the call of the function. Happy New Year Too.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now