Deleting Record from Database Problem using Javascript Box.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mmc01ms
Forum Commoner
Posts: 97
Joined: Wed Dec 01, 2004 3:33 am
Location: Nottingham, UK

Deleting Record from Database Problem using Javascript Box.

Post by mmc01ms »

Hi guys, the problem i have is that when a user wants to delete a vinyl from my database they click on the button and a javascript box pops up asking if they are sure. Once yes is clicked it should delete from the database however it does not. The form is sent to delete_vinyl.php however before i put in the javascript box it would delete the record no it does not any idea why?

code is:


delete_vinyl_form.php

Code: Select all

<?php 
	
	//display_vinyl_form.php
	
	require_once('shopping_cart_fns.php');
	
	?>
	
	
<script> 
		 function checkIt()&#123; 
			if(confirm("Delete Vinyl from Shop"))&#123; 
				document.MyForm.submit(); 
			&#125; 
		&#125; 
</script> 
		
		
		
		
		
		<?php
		
		function display_vinyl_form($vinyl = '')
	&#123;
		
		$edit = is_array($vinyl);
?>
<center><form align="center" name="newrecord" method="post" enctype="multipart/form-data" action="<?php echo $edit?'edit_vinyl.php' : 'insert_vinyl.php';?>">
		<table bordercolor="#FFFFFF" border="1" bgcolor="#7b9815">
			<font color="#000000" size="2" face="Courier New, Courier, mono">
				<tr>
					<td><font color="#FFFFFF" size="2" face="Arial, Helvetica, sans-serif">Cat_No: </font></td>
					<td><input name="cat_no" type="text" maxlength="6" value="<?php echo $vinyl&#1111;'cat_no']; ?>"></td>
				</tr>
				<tr>
					<td><font color="#FFFFFF" size="2" face="Arial, Helvetica, sans-serif">Title: </font></td>
					<td><input name="title" type="text" maxlength="40" value="<?php echo $edit?$vinyl&#1111;'title']:' '; ?>"></td>
				</tr>
				<tr>
					<td><font color="#FFFFFF" size="2" face="Arial, Helvetica, sans-serif">Artist: </font></td>
					<td><select name="artist_id">
							<?php
		$artist_array=get_artists();
		foreach ($artist_array as $thisartist)
		&#123;
			echo '<option value="';
			echo $thisartist&#1111;'artist_id'];
			echo '"';
			if ($edit && $thisartist&#1111;'artist_id'] == $vinyl&#1111;'artist_id'])
				echo 'selected';
			echo '>';
			echo $thisartist&#1111;'artist_id'];
			echo'</option>';
			echo"\n";
		&#125;
							?>
						</select>
					</td>
				</tr>
				
				<tr>
					<td><font color="#FFFFFF" size="2" face="Arial, Helvetica, sans-serif">Genre: </font></td>
					<td><select name="genre_ref">
							<?php
		$genre_array=get_genres();
		foreach ($genre_array as $thisgenre)
		&#123;
			echo '<option value="';
			echo $thisgenre&#1111;'genre_ref'];
			echo '"';
			if ($edit && $thisgenre&#1111;'genre_ref'] == $vinyl&#1111;'genre_ref'])
				echo 'selected';
			echo '>';
			echo $thisgenre&#1111;'genre_ref'];
			echo'</option>';
			echo"\n";
		&#125;
							?>
						</select>
					</td>
				</tr>
				<tr>
					<td><font color="#FFFFFF" size="2" face="Arial, Helvetica, sans-serif">Formats: </font></td>
					<td><select name="format">
							<?php
		$format_array=get_formats();
		foreach ($format_array as $thisformat)
		&#123;
			echo '<option value="';
			echo $thisformat&#1111;'format_type'];
			echo '"';
			if ($edit && $thisformat&#1111;'format_type'] == $vinyl&#1111;'format_type'])
				echo 'selected';
			echo '>';
			echo $thisformat&#1111;'format_type'];
			echo'</option>';
			echo"\n";
		&#125;
							?>
						</select>
					</td>
				</tr>
				<tr>
					<td><font color="#FFFFFF" size="2" face="Arial, Helvetica, sans-serif">Month: </font></td>
					<td><select name="month"><?php 
		$nums = range(1, 12);
		foreach ($nums as $num) 
		&#123;
			echo '<option value="';
			echo $num;
			echo '"';
			echo '>';
			echo $num;
			echo '</option>';
			echo "\n";
		&#125; ?>
						</select>
					</td>
				</tr>
				<tr>
					<td><font color="#FFFFFF" size="2" face="Arial, Helvetica, sans-serif">Year: </font></td>
					<td><select name="year"><?php 
		$nums = range(1994, 2006);
		foreach ($nums as $num) 
		&#123;
			echo '<option value="';
			echo $num;
			echo '"';
			echo '>';
			echo $num;
			echo '</option>';
			echo "\n";
		&#125; ?>
						</select>
					</td>
				</tr>
				<tr>
					<td><font color="#FFFFFF" size="2" face="Arial, Helvetica, sans-serif">Vinyl Status: </font></td>
					<td><select name="status_code">
							<?php
		$status_array=get_status();
		foreach ($status_array as $thisstatus)
		&#123;
			echo '<option value="';
			echo $thisstatus&#1111;'status_code'];
			echo '"';
			echo '>';
			echo $thisstatus&#1111;'status_description'];
			echo'</option>';
			echo"\n";
		&#125;
							?>
						</select>
					</td>
				</tr>
				<tr>
					<td><font color="#FFFFFF" size="2" face="Arial, Helvetica, sans-serif">Price: </font></td>
					<td><input name="price" type="text" maxlength="5" id="price" value="<?php echo $edit?$vinyl&#1111;'price']: ' '; ?>"></td>
				</tr>
				<tr>
					<td><font color="#FFFFFF" size="2" face="Arial, Helvetica, sans-serif">Qty: </font></td>
					<td><input name="qty" type="text" maxlength="3" id="qty" value="<?php echo $edit?$vinyl&#1111;'stock_level']: ' '; ?>"></td>
				</tr>
				<tr>
					<td><font color="#FFFFFF" size="2" face="Arial, Helvetica, sans-serif">Label: </font></td>
					<td><input name="label" type="text" maxlength="30" id="qty" value="<?php echo $edit?$vinyl&#1111;'label']: ' '; ?>"></td>
				</tr>
				
				<tr>
					<td><font color="#FFFFFF" size="2" face="Arial, Helvetica, sans-serif">Comments: </font></td>
					<td><input name="short_comments" type="text" maxlength="60" id="short_comments" value="<?php echo $edit?$vinyl&#1111;'comments']: ' '; ?>"></td>
				</tr>
				<tr>
					<td><font color="#FFFFFF" size="2" face="Arial, Helvetica, sans-serif">Vinyl Information:</font></td><td><textarea name="comments" maxlength="225" cols="60" rows="10"><?php echo $vinyl&#1111;'vinyl_information']?></textarea></td>
				</tr>
				<tr>
					<td><font color="#FFFFFF" size="2" face="Arial, Helvetica, sans-serif">Image Link:</font></td>
				<td align="left"><input type="file" name="picture" size="40"></td>
			</tr>
			
			<?php
						
						if($vinyl&#1111;'image']) &#123;
							$size = getImageSize($vinyl&#1111;'image']);
							$width = $size&#1111;0];
							$height = $size&#1111;1];
			?>
			
			<tr>
					<td>Image: </td>
				<td align="center">
					<?php echo '<img src"'.$vinyl&#1111;image].'"';?>
						width="<?php print $width;?>" height="<?php print $height;?>">
				</td>
			</tr>
			<?php &#125; ?>
				<tr>
					<td <?php if(!$edit) echo 'colspan="2"'; ?> align="center">
						<?php
		if ($edit)
			echo '<input type="hidden" name="oldcatno" value="'.$vinyl&#1111;'cat_no'].'">';
		
						?>
						<td><input type="submit" value="<?php echo $edit?'Update' : 'Add';?>Record"></form><?php if($edit)
		&#123;
			echo '';
			echo '<form name="MyForm" action="delete_vinyl.php"> ';
			echo '<input type="button" value="Delete Vinyl" onClick="checkIt();"> ';
			echo '<input type="hidden" name="cat_no" value="'.$vinyl&#1111;'cat_no'].'">';
			echo '</form></td>';
		&#125; ?>
					</tr>
					<table></form>	</center>
				
				<div align="left"></div></td>
		</tr>
	</table></td>

			</tr>
			</table>
			<?
	&#125;
		
		do_html_footer();
					?>

delete_vinyl.php

Code: Select all

<?php
	session_start();
	
	require('page.inc');
	require_once('update_delete_fns.php');
	
	checkAdminLogin();

	
	$editform = new Page();
	
	$editform -> Display();
	
	display_main_menu();
	
	//$catno = $_GET&#1111;'cat_no'];

	
  if (isset($_POST&#1111;'cat_no'])) 
  &#123;
    $catno = $_POST&#1111;'cat_no'];
    if(delete_vinyl($_POST&#1111;'cat_no']))
      echo '<center>Book '.$catno.' was deleted.</center><br />';
    else
      echo '<center>Book '.$catno.' could not be deleted.</center><br />';
  &#125; 
  

do_html_footer();

?>
delete_vinyl function

Code: Select all

function delete_vinyl($catno)
		// deletes vinyl identfied by it's cat_no from the database
	&#123;
		$link_id = db_connect();
		
		$query = "delete * from vinyls
             where cat_no='$catno'";
			$result = @mysql_query($query, $link_id);
		if (!$result)
			return false;
		else
			return true;
	&#125;
hunterhp
Forum Commoner
Posts: 46
Joined: Sat Jan 22, 2005 5:20 pm
Contact:

Post by hunterhp »

What kindn of error is it giving you when you submit?

Does it give you the 'Book .$catno. could not be deleted?

Have you tried return true, instead of document.MyForm.submit() ?
mmc01ms
Forum Commoner
Posts: 97
Joined: Wed Dec 01, 2004 3:33 am
Location: Nottingham, UK

Post by mmc01ms »

it returns nothing on screen no message at all.
hunterhp
Forum Commoner
Posts: 46
Joined: Sat Jan 22, 2005 5:20 pm
Contact:

Post by hunterhp »

I think I know what's wrong.

make an else statement after if (isset($_POST[cat_no])) and try echoing a warning message. See if the warning message comes up, and if it does, it means the javascript isn't transfering the forms values.

Did you try return true instead of document.MyForm.submit()?
mmc01ms
Forum Commoner
Posts: 97
Joined: Wed Dec 01, 2004 3:33 am
Location: Nottingham, UK

Post by mmc01ms »

the value of $catno is getting past through however it is now going to the message that the vinyl couldn't be deleted instead of showing nothing, and the vinyl still hasn't been deleted. I used var_export() to check if the result from the query returned true or false and it returned false. So i've narrowed it down, however the query is fine as i used var_export on this as well and it reutrned the correct query with the catno being passed through to the function correctly. I used the return true and it made no diffrence. Any other ideas?
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

if the query call returns false, you have an issue with it.. it is advisable to always have debugging code attached to any database call during development time. e.g.

Code: Select all

$result = mysql_query(...) or die(mysql_error());
mmc01ms
Forum Commoner
Posts: 97
Joined: Wed Dec 01, 2004 3:33 am
Location: Nottingham, UK

Post by mmc01ms »

the error i get is:

Code: Select all

You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '* from vinyls where cat_no = '100019'' at line 1
The syntax is fine as far as im aware the tables vinyls does exist and cat_no is right as well.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

DELETE FROM `tablename WHERE ...

not DELETE * FROM...


maybe you should bookmark this
http://dev.mysql.com/doc/mysql/en/index.html
Post Reply