Click to See Complete Forum and Search --> : Functions problems


dz_boy
08-18-2007, 08:58 PM
I think I'm doing something wrong. The function at the top was not meant to be executed unless called upon, but it has decided that it will execute regardless of whether ist is called upon or not. Also, when it does execute, it wipes every record, instead of just the one intended to be deleted (also ignoring the LIMIT phrase). What the heck is going wrong??? I'm befuddled!

<?php

function delete_item($itm_no)
{
$sql2 = 'DELETE FROM `Item` WHERE `item_id` = '. $itm_no .' LIMIT 1';
$sql3 = 'DELETE FROM `List_Xref` WHERE `item_id` = '. $itm_no .' LIMIT 1';

$result2 = mysql_query($sql2)
or die ("Could not execute query: ".mysql_error());
$result3 = mysql_query($sql3)
or die ("Could not execute query: ".mysql_error());

}

$sql1 = 'SELECT c.item_id , c.cost , c.quantity , c.acquired , c.item_name , c.description , d.location_name , d.location_link FROM `Customer` a , `List_Xref` b , `Item` c , `Location` d WHERE a.customer_id = '. $_COOKIE['user_id'] .' and a.list_id = b.list_id and b.item_id = c.item_id and c.location_id = d.location_id';
$result = @mysql_query($sql1);

while($row = mysql_fetch_array($result))
{
$item_number = $row['item_id'];
echo '<tr>
<td align="right"><b>Item Name:</b></td>
<td><input type="text" style="text-transform: uppercase; text-decoration: bold;" name="item_name" value="'. $row['item_name'] .'"> <b>Quantity:</b>&nbsp;<input type="text" name="quantity" value="'. $row['quantity'] .'" size="1"></td>
<td rowspan="4">&nbsp;&nbsp;<font size="5" face="Comic Sans MS" color="#FF0000"><a href="'. delete_item($item_number) .'">X</a>
<br />'. $row['item_id'] .'</font>&nbsp;&nbsp;</td>
</tr>
<tr>
<td align="right"><b>Cost and Store:</b></td>
<td><input type="text" name="cost" value="'. $row['cost'] .'" size="4"> @ <input type="text" name="location_name" value="'. $row['location_name'] .'" size="6"></td>
</tr>
<tr>
<td align="right"><b>Link:</b></td>
<td><input type="text" name="location_link" value="'. $row['location_link'] .'"></td>
</tr>
<tr>
<td align="right"><b>Description:</b></td>
<td><textarea name="description" rows="2" cols="35">'. $row['description'] .'</textarea></td>
</tr>
<tr><td colspan="3"><hr size="1" noshade></td></tr>';
}
?>

shane.carr
08-19-2007, 01:39 AM
I'm not very experienced with MySQL, but the reason that delete_item() is running every time is because you execute it in this strip of code:

'<a href="'. delete_item($item_number) .'">X</a>'

You can't place PHP code within links, and then expect the code to run when the link is clicked upon. The way that you have this code set up will make a link linking to nowhere, because "delete_item()" returns no value. To do what you are trying to do, you need to either go into detailed javascript programming, or, change that code to something like this:

'<a href="'.$_SERVER['PHP_SELF'].'?do=deleteitem&amp;itemnum='.$item_number.'">X</a>'

Then add this to the beginning of the PHP code:

if($_GET['do']=="deleteitem"){
delete_item($_GET['itemnum']); //run stripslashes() on this if necessary
}


This doesn't explain the second problem that you mentioned, but I hope it helps.

Declan1991
08-19-2007, 07:11 AM
I think the problem is that you don't surround the variable with ''. Try

$sql2 = "DELETE FROM Item WHERE item_id = '$itm_no' LIMIT 1";
$sql3 = "DELETE FROM List_Xref WHERE item_id = '$itm_no' LIMIT 1";

dz_boy
08-19-2007, 12:51 PM
Okay... I've changed it to a button. But I can't seem to carry the item_id value into the function from isset(delete)... the hidden input doesn't seem to cut it. Is there a way to assign the item_id to the button and then have it relayed through the isset() function? Here's the code:

<fieldset><center>
<form method="post">
<table border="0">
<tr><td colspan="3">&nbsp;</td></tr>
<?php

if(isset($_POST['delete'])){
$sql2 = 'DELETE FROM `Item` WHERE `item_id` = '. $itm_no .' LIMIT 1';
$sql3 = 'DELETE FROM `List_Xref` WHERE `item_id` = '. $itm_no .' LIMIT 1';

$result2 = mysql_query($sql2)
or die ("Could not execute query: ".mysql_error());
$result3 = mysql_query($sql3)
or die ("Could not execute query: ".mysql_error());

echo 'You have chosen to delete number: '.$_POST['itm_no'];
}
else
{
}

$sql1 = 'SELECT c . item_id , c . cost , c . quantity , c . acquired , c . item_name , c . description, c . location_link , c . location_name FROM `Customer` a , `List_Xref` b , `Item` c WHERE a . customer_id = 1 and a . list_id = b . list_id and b . item_id = c . item_id';
$result = @mysql_query($sql1);

while($row = mysql_fetch_array($result))
{
$item_number = $row['item_id'];
echo '<tr>
<td align="right"><b>Item Name:</b></td>
<td><input type="text" style="text-transform: uppercase; text-decoration: bold;" name="item_name" value="'. $row['item_name'] .'"> <b>Quantity:</b>&nbsp;<input type="text" name="quantity" value="'. $row['quantity'] .'" size="1"></td>
<td rowspan="4">&nbsp;&nbsp;<input type="hidden" name="itm_no" value="'. $row['item_id'] .'">
<input type="button" value="X" style="padding: 4px; size: 20px; color: #CC0000; background-color: #FFFFFF; border-style: solid; border-width: 1px; text-decoration: bold;" name="delete">&nbsp;&nbsp;</td>
</tr>
<tr>
<td align="right"><b>Cost and Store:</b></td>
<td><input type="text" name="cost" value="'. $row['cost'] .'" size="4"> @ <input type="text" name="location_name" value="'. $row['location_name'] .'" size="6"></td>
</tr>
<tr>
<td align="right"><b>Link:</b></td>
<td><input type="text" name="location_link" value="'. $row['location_link'] .'"></td>
</tr>
<tr>
<td align="right"><b>Description:</b></td>
<td><textarea name="description" rows="2" cols="35">'. $row['description'] .'</textarea></td>
</tr>
<tr><td colspan="3"><hr size="1" noshade></td></tr>';
}
?>

</table>
</form>
</fieldset>

So, if three items are echoed using the loop, each of them will have unique item_id's. If I click the X button, I only want that one to be deleted, meaning the item_id will need to be attached somehow. That's where I need help.

tca
08-19-2007, 03:10 PM
Do it like this:


$sql2 = "DELETE FROM `Item` WHERE `item_id` = '$itm_no' LIMIT 1";



<input type="text" name="cost" value="<?php echo $row['cost']; ?>" size="4">

shane.carr
08-19-2007, 03:15 PM
So, when you click the "X" button, you want it to delete the item? The way you have this set up, clicking the button won't do anything. Also, why do yo have a form with no submit button? I would change these parts of the code:

<fieldset><center>
<!-- REMOVE: <form method="post"> -->
<table border="0">
<tr><td colspan="3">&nbsp;</td></tr>
<?php

if(isset($_POST['item_no'])){
$item_no = $_POST['item_no'];
$sql2 = 'DELETE FROM `Item` WHERE `item_id` = '. $itm_no .' LIMIT 1';
$sql3 = 'DELETE FROM `List_Xref` WHERE `item_id` = '. $itm_no .' LIMIT 1';

$result2 = mysql_query($sql2)
or die ("Could not execute query: ".mysql_error());
$result3 = mysql_query($sql3)
or die ("Could not execute query: ".mysql_error());

echo 'You have chosen to delete number: '.$_POST['itm_no'];
}
else
{
}

$sql1 = 'SELECT c . item_id , c . cost , c . quantity , c . acquired , c . item_name , c . description, c . location_link , c . location_name FROM `Customer` a , `List_Xref` b , `Item` c WHERE a . customer_id = 1 and a . list_id = b . list_id and b . item_id = c . item_id';
$result = @mysql_query($sql1);

while($row = mysql_fetch_array($result))
{
$item_number = $row['item_id'];
echo '<form method="post" action="'.$_SERVER['PHP_SELF'].'">
<tr>
<td align="right"><b>Item Name:</b></td>
<td><input type="text" style="text-transform: uppercase; text-decoration: bold;" name="item_name" value="'. $row['item_name'] .'"> <b>Quantity:</b>&nbsp;<input type="text" name="quantity" value="'. $row['quantity'] .'" size="1"></td>
<td rowspan="4">&nbsp;&nbsp;<input type="hidden" name="itm_no" value="'. $row['item_id'] .'">
<input type="submit" value="X" style="padding: 4px; size: 20px; color: #CC0000; background-color: #FFFFFF; border-style: solid; border-width: 1px; text-decoration: bold;" name="delete">&nbsp;&nbsp;</td>
</tr>
<tr>
<td align="right"><b>Cost and Store:</b></td>
<td><input type="text" name="cost" value="'. $row['cost'] .'" size="4"> @ <input type="text" name="location_name" value="'. $row['location_name'] .'" size="6"></td>
</tr>
<tr>
<td align="right"><b>Link:</b></td>
<td><input type="text" name="location_link" value="'. $row['location_link'] .'"></td>
</tr>
<tr>
<td align="right"><b>Description:</b></td>
<td><textarea name="description" rows="2" cols="35">'. $row['description'] .'</textarea></td>
</tr>
<tr><td colspan="3"><hr size="1" noshade></td></tr></form>';
}
?>

</table>
<!-- REMOVE: </form> -->
</fieldset>

dz_boy
08-19-2007, 04:32 PM
I am planning on having a Submit button. The idea of the page is to allow people to add more items, edit or remove existing items. Since all the values from the database are displayed in text fields, my next task was to allow those fields to be edited and the changes update the database. I'm going to do that once I can get one item to delete when the button is clicked.

shane.carr
08-19-2007, 05:40 PM
Then you'll have to call PHP via javascript, which I've never done before, but try using the XMLHTTPRequest (http://www.google.com/search?q=XMLHTTPRequest+javascript) method.

dz_boy
08-19-2007, 06:38 PM
The XMLHTTP thing would work, but I don't mind if the page reloads. Is there a simpler way to just run that function with a specific item_id in PHP? I could add the item_id to the name of the button (ie. '<input type="button" name="delete_'.$row['item_id'].'>') but I don't know how to call the function using that additional information.

shane.carr
08-19-2007, 10:05 PM
Rewind a bit to my first post (#2). That will solve your problem. All you have to do to make it work is change the if with the isset() to if($_GET['do']=="deleteitem"){

dz_boy
08-20-2007, 11:14 AM
Oops. Sorry- I'd forgotten that we'd come from there. I entered the code from the second post and I am getting one error. It's saying that the if($_GET['do'])=='deleteitem') has an undefined variable 'do.' Should I just delcare 'do' before that line or should 'do' be defined somewhere?

dz_boy
08-20-2007, 12:38 PM
I just need to initialize or set $_GET['do'] somehow... hidden inputs are not working and neither is a simple "$do = '';" What am I missing?

shane.carr
08-20-2007, 05:18 PM
What version of PHP are you using? You may have to use $HTTP_GET_VARS instead of $_GET. Also:

if($_GET['do'])=='deleteitem')

should be

if($_GET['do']=='deleteitem')

What's your exact code in this line, and can you post the error that PHP outputs?

dz_boy
08-20-2007, 08:11 PM
Here is my code:



if($_GET['do']=="delete_item")

'<a style="color: #CC0000; text-decoration: none;" href="'. $_SERVER['PHP_SELF'] .'?do=delete_item&amp;item_no='. $row['item_id'] .'">
<b><font size="4"><div style="padding: 6px; border-style: solid; border-width: 1px; border-color: #FF0000;">
X DELETE</div></font></b></a>'



...and the error. I see why, but I don't know how to fix it.

Notice: Undefined index: do in d:\websites\larsonforcouncilcom\CIT\mylist.php on line 48

shane.carr
08-20-2007, 08:21 PM
Hmm... I've never had an error like that before. Is it possible that it's due to $_GET vs. $HTTP_GET_VARS? I can't explain that error, though I wish I could. I might recommend starting a new tread just for that problem.