PDA

View Full Version : first mysql row ONLY shows 2x


gavimobile
06-16-2008, 12:36 AM
hey does anyone see a bug here in this code?
my code works fine when i simply select * from table, but when i
try to DESC ANY column, it displays the first row (oldest row) from
mysql database 2 times. for eg. instead of 5,4,3,2,1 it displays
1,5,4,3,2,1. I have a feeling its something to do with my loop.
here is the code. tia!

p.s. the code was comming out weird when i pasted it in here.
if you see a line that looks seperated its most likley not an error
for example
eg. <?php $echo $my_Super_Long_
Row_Name['my_field']; ?>
which really is
<?php $echo $my_Super_Long_Row_Name['my_field']; ?>


<?

require_once('includes/config.php'); // my connection


// NOTE: these are variables that i have in my config

// Item Variables
$item_id = $_POST['item_id'];
$item_name = $_POST['item_name'];
$item_price = $_POST['item_price'];
$item_desc = $_POST['item_desc'];
$item_pic = ($_FILES['item_pic']['name']);
$item_date = $_POST['item_date'];
// End of Store Variables session_start();

// Security to make sure no unwanted users log in
include('includes/security.php');
// end of security


if ($del){
// my delete query
$query = sprintf("DELETE FROM item where item_id='$del'");
mysql_query($query)or die(mysql_error());
// end of my delete query
}

// This is my select query
$query_sel = sprintf("SELECT * FROM item ORDER BY item_id desc");
$results_sel = @mysql_query($query_sel);
$row_item = @mysql_fetch_array($results_sel);
//end of select query
?>

<html>
<body>
<h3>Add New User</h3>

<form name="form1" method="POST" action="includes/scripts/upload_file.php"
enctype="multipart/form-data">
<p>

<label>Title</label>
<input type="text" name="item_name" id="item_name" size="32" />

<label>Price</label>
<input type="text" name="item_price" id="item_price" size="32" />

<label>Description</label>
<textarea name="item_desc" id="item_desc" rows="5"
cols="10"></textarea>

<label>Picture</label>
<input type="file" name="item_pic" id="item_pic" size="32" />

<br />
<br />

<input type="submit" class="add_item_btn" value="Add This Item" />
<input type="hidden" name="item_date" id="item_date" value="
<? echo date("Y-m-d"); ?>" />
</p>
</form>

<br />

<h3>Manage Items</h3>

<?
do {
?>

<img class='item_image' src="../images/item/
<? echo $row_item['item_pic']; ?>" />

<br />

Name: <? echo $row_item['item_name']; ?>

<br />

Item ID: <? echo $row_item['item_id']; ?>

<br />

Price: ₪ <? echo $row_item['item_price']; ?>

<br />

<!-- MY DELETE BUTTON-->
<input type="submit" class="del_btn" name="del_btn"
id="del_btn" value="Delete"onClick="document.location.href=
'manage_store.php?del=<? echo $row_item['item_id']?>'" />
Date Added: <? echo $row_item['item_date']; ?>

<?
}
while ($row_item = @mysql_fetch_array($results_sel));
?>

</body>
</html>

davidj
06-16-2008, 06:46 AM
do you have short tags set

if you dont then this wont work ...

<? ?>

you need to do this


<?php ?>

davidj
06-16-2008, 06:51 AM
not sure if this is valid

just looks weird


}
while ($row_item = @mysql_fetch_array($results_sel));


try to keep the bracket on the same line as the while.

davidj
06-16-2008, 07:01 AM
you have used a do while loop

try using a while loop


<?php while ($row_item = @mysql_fetch_array($results_sel)){ ?> ///<< start loop

/// HTML HERE TO LOOP

<?php } ?> ///<< close loop

gavimobile
06-16-2008, 09:55 AM
thanks for the reply dj,
for every tag that said <? ?> i changed to <?php ?>
and i added the while look you posted, still no luck.
i thought <?php ?> was just to help the coder find
the php easier! Does it really make a difference.
Any other suggestions?


Thanks
Gavimobile

davidj
06-16-2008, 12:55 PM
i thought <?php ?> was just to help the coder find
the php easier! Does it really make a difference.

Your server may be set up to accept short tags so fine but yes it does make a difference. I'm sure i read somewhere that the short tag option is being depreciated in the later versions of PHP! (This could just be the ASP style of tag though <% %>)

try echoing the SQL to the page then copy and paste it into your MYSQL IDE (console) and query it from there to see what returns

gavimobile
06-16-2008, 08:55 PM
Your server may be set up to accept short tags so fine but yes it does make a difference. I'm sure i read somewhere that the short tag option is being depreciated in the later versions of PHP! (This could just be the ASP style of tag though <% %>)

try echoing the SQL to the page then copy and paste it into your MYSQL IDE (console) and query it from there to see what returns

interesting! I am so used to doing <? ?> rather than <?php ?> even it if might be bad practice. it looks cleaner for me. i hope php6 wont kill me later on!

mysql console shot back the most beautiful set of results! i knew this was a php problem from the begining, and i still stick with my theory that its something to do with the loop!

lux
06-17-2008, 12:03 PM
Try the code below and add in your other stuff. I removed the first mysql_fetch_array becasue you were fetching the same query result twice


<?php

require_once('includes/config.php'); // my connection


// NOTE: these are variables that i have in my config

// Item Variables
$item_id = $_POST['item_id'];
$item_name = $_POST['item_name'];
$item_price = $_POST['item_price'];
$item_desc = $_POST['item_desc'];
$item_pic = ($_FILES['item_pic']['name']);
$item_date = $_POST['item_date'];
// End of Store Variables session_start();

// Security to make sure no unwanted users log in
include('includes/security.php');
// end of security


// This is my select query
$query_sel = sprintf("SELECT * FROM item ORDER BY item_id desc");
$results_sel = @mysql_query($query_sel);
//end of select query
?>

<html>
<body>
<h3>Manage Items</h3>

<?php
while($row_item = @mysql_fetch_array($results_sel))
?>

Name: <?php echo $row_item['item_name']; ?>


<?php
}
?>

</body>
</html>

davidj
06-17-2008, 01:16 PM
I was just going to say that

lux
06-17-2008, 02:20 PM
of course you were ! hahaha

gavimobile
06-17-2008, 03:40 PM
SOLVED

yup, you called it! I did my accomplishments of learning something new today! im not gona forget this one for the future!

thanks dj!

btw, were your last two comment sarcastic?, LOL
cause i really did know it was a php problem from the begining which is why i posted in php topic rather than mysql/sql topic.

speaking of topics, not sure if i should post my next question in a new topic, eventhough it isnt really a problem, i want to start looking in to page controlling. for example item 1-10 is page 1 item 11-20 is page 2 item 21-30 is page 3. i think the term is called page navigation but im not sure! you have a tutorial on this in the tutorials section? if not maybe you can give me a few pointers before i go on my google search research campaign ;-). what am i searching for anyways?

lux
06-17-2008, 03:47 PM
the term your looking for is: php pagination

if your interested and want to really improve your understanding of php and the technologies that work along side it i would reconmend this book:

PHP and MySQL Web Development (3rd Edition) (Developer's Library)

and show you love for the guys who set the site up by buying it here:

http://www.dreamweaverclub.com/books/0672326728/PHP_and_MySQL_Web_Development_3rd_Edition_Develope r_s_Library.html

gavimobile
06-17-2008, 04:55 PM
thanks man!

oh i forgot to mention you missed a curley brace in the while statement!

gavimobile
06-17-2008, 06:50 PM
lux i didnt realize that you posted the code!
i dont know how i missed that, i thought dj made the post!

thank you very much, and you deserve alot of credit!!!

excuese me for my ignorance :-p

davidj
06-17-2008, 07:27 PM
err i was gona post that

lux pinched my thoughts so his post does not count

lux
06-18-2008, 09:04 AM
stop clogging up the internet with your comments davidj! haha

no worries gavimobile, good luck with your project