Jump to content

I think I have an array error, please help


webznz

Recommended Posts

Hi there Im currently working on a website for a friend. A, to help him out B, to give me some experiance.

 

I have created a mySql database that has a table called Products in it, there are several columns like ID, Name, Description.

I have also made a back end system that updates the database with information entered into an html form please see code for this below...

<html>
<head>
<title> MTS Controll panle </title>
<link rel="stylesheet" type="text/css" href="../css/backend.css" />
</head>

<body>

<div id="wrap">
<div id="header"></div>
<div id="nav"></div>
<div id="sidebar">

<!-- start of controll links -->
<a href="bk_index.html">Insert new Products</a></br>
<a href="php/Delete.php">Delete Products</a></br>
<a href="bk_services.html">Insert New Service</a></br>
<a href="php/Delete_services.php">Delete Services</a></br>

<!-- controll links end here -->
</div>
<div id="main">
<!-- controll panel start here -->
<form method="post" action="php/update_products.php">

	Item Type:</br>
	<input type="text" name="Name" size="25" /></br></br>

	Modle:</br>
	<input type="text" name="ModelName" size="25" /></br></br>

	Description:</br>
	<textarea rows="10" cols="20" wrap="physical" name="Description">
	</textarea></br>

	Slideshow:</br>
	<textarea rows="10" cols="20" wrap="physical" name="SlideShow">
	</textarea></br>

	Price:</br>
	<input type="text" name="Price" size="25" /></br></br>

	<input type="submit" value="Update Database" />

</form>
<!--controllpanel links start here -->
</div>
<div align="center">
<div id="footer">Controll Panel Developed by Casey Smith</div>
</div>
</div>

</body>
</html>

 

Here is update_products.php

<?php
$Name =$_POST['Name'];
$ModelName =$_POST['ModelName'];
$Description =$_POST['Description'];
$SlideShow =$_POST['SlideShow'];
$Price =$_POST['Price'];

mysql_connect ("localhost", "root", "maryanne") or die ('Error: ' . mysql_error());
mysql_select_db ("mtsproducts");

$query="INSERT products (Name, ModelName, Description, SlideShow, Price)VALUES ('".$Name."', '".$ModelName."', '".$Description."', '".$SlideShow."', '".$Price."')";

mysql_query($query) or die ('Error updating database');

echo "1 entry entered" .$Name. " " .$ModelName. " " .$Description. " " . htmlspecialchars($SlideShow) . " " .$Price ;


?>

 

From here the database is now updated with the information entered by the user.

At the front end the first thing Im trying to create with this data from the database is a type of user menu generated from

the Name column in Products, just nice and simple for now however *as you will see in the code below* I have a print loop

that dosnt seem to want to print the first entry in the database.

i.e. if this was the array[0], array[1], array[2]. Only array[1] & array[2] would be echoed to the screen?!

Here is the code for the client side page.

<html>
<head>
<title>MTS - Mag - Tyers - Suspension</title>
<link rel="stylesheet" type="text/css" href="css/main.css" />

<!-- Mouse over buttons -->
<SCRIPT LANGUAGE="JavaScript"> 

<!-- Hide from old browsers

var NN3 = false;
image1 = new Image();
image1.src = "images/menu/home_button1.png";
image1on = new Image();
image1on.src = "images/menu/home_button2.png";

image2 = new Image();
image2.src = "images/menu/service_button1.png";
image2on = new Image();
image2on.src = "images/menu/service_button2.png";

image3 = new Image();
image3.src = "images/menu/shop_button1.png";
image3on = new Image();
image3on.src = "images/menu/shop_button2.png";

image4 = new Image();
image4.src = "images/menu/gallery_button1.png";
image4on = new Image();
image4on.src = "images/menu/gallery_button2.png";

image5 = new Image();
image5.src = "images/menu/videos_button1.png";
image5on = new Image();
image5on.src = "images/menu/videos_button2.png";

image6 = new Image();
image6.src = "images/menu/contact_button1.png";
image6on = new Image();
image6on.src = "images/menu/contact_button2.png";

function on3(name)   {
        document[name].src = eval(name + "on.src");
}
function off3(name)  {
        document[name].src = eval(name + ".src");
}
NN3 = true;

function on(name)  {
        if (NN3) on3(name);
}
function off(name)  {
        if (NN3) off3(name);
}
// --> 

</SCRIPT> 
<!-- Mouse over Button END -->

</head>
<body onload="initialize()">


<div id="top-centerbar">
	<div id="container">
<!-- AddThis Button starts here -->
		<a class="addthis_button" href="http://www.addthis.com/bookmark.php?v=250&username=xa-4b60df317994ba46"><img src="http://s7.addthis.com/static/btn/sm-share-en.gif" width="83" height="16" alt="Bookmark and Share" style="border:0"/></a><script type="text/javascript" src="http://s7.addthis.com/js/250/addthis_widget.js#username=xa-4b60df317994ba46"></script>
<!-- AddThis Button ends here -->
	</div id>
</div id>

<div id="inside-main_b">
	<div id="services_main">
		<div align="left">
<!-- php code from here -->
<?php
mysql_connect ("localhost", "root", "verysecret") or die ('Error: ' . mysql_error());
mysql_select_db ("mtsproducts") or die('Database Error' . mysql_error());
?>

<?php
$data = mysql_query("SELECT name FROM products") 
or die(mysql_error());

$info = mysql_fetch_array( $data );
Print "<b>Products:</b></br> ";
while($info = mysql_fetch_array( $data )){ 
Print "" .$info[0]. "</br> ";
}

?>
<!-- php code to here -->
		</div>
	</div id>
</div id>


<div align="center">
<div id="bottom-centerbar">
<!-- buttons start -->
<a href="index.html" onmouseover="on('image1');" onmouseout="off('image1')"><img src="images/menu/home_button1.png"  border="0" name="image1" ALT="Click for next page" ALIGN=ABSMIDDLE></a>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
<a href="services.php" onmouseover="on('image2');" onmouseout="off('image2')"><img src="images/menu/service_button1.png"  border="0" name="image2" ALT="Click for next page" ALIGN=ABSMIDDLE></a>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
<a href="shop.html" onmouseover="on('image3');" onmouseout="off('image3')"><img src="images/menu/shop_button1.png"  border="0" name="image3" ALT="Click for next page" ALIGN=ABSMIDDLE></a>&nbsp&nbsp&nbsp&nbsp&nbsp
<a href="gallery.html" onmouseover="on('image4');" onmouseout="off('image4')"><img src="images/menu/gallery_button1.png"  border="0" name="image4" ALT="Click for next page" ALIGN=ABSMIDDLE></a>&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp
<a href="videos.html" onmouseover="on('image5');" onmouseout="off('image5')"><img src="images/menu/videos_button1.png"  border="0" name="image5" ALT="Click for next page" ALIGN=ABSMIDDLE></a>&nbsp&nbsp&nbsp&nbsp&nbsp
<a href="contact.html" onmouseover="on('image6');" onmouseout="off('image6')"><img src="images/menu/contact_button1.png"  border="0" name="image6" ALT="Click for next page" ALIGN=ABSMIDDLE></a>
<!-- buttons end -->


</div id>


</body>
</html>


 

any help would be greatly appreciated.

Regards

Casey.

 

 

Link to comment
Share on other sites

after doing that this is what was printed to the screen.

Array ( [name] => Mag Wheel ) Array ( [name] => Bags ) Array ( [name] => Wheel Nuts ) Array ( [name] => Steering Wheel )

 

 

However there is still an entry missing before Mag Wheel, here are the database entries (buffer is the one missing)

Buffer

 

Mag Wheel

 

Bags

 

Wheel Nuts

 

Steering Wheel

 

Link to comment
Share on other sites

Nevermind, spotted your error. Change your code to the following. Make sure you copy all of it. you don't need the initial $info = mysql_fetch_array(); that's what's screwing up your results.

 

<?php
$data = mysql_query("SELECT name FROM products") or die(mysql_error());

Print "<b>Products:</b></br> ";
while($info = mysql_fetch_assoc( $data )){
   Print $info['name']. "</br> ";
}
?>

Link to comment
Share on other sites

Just some background as to why this would cause it to miss the first entry.

 

calling mysql_fetch_array before the while loop sets array pointer to the first key in the array, which is your first value of 'buffer'. Then doing the while loop, because you do not reset the pointer location, starts the while from $array['1'] rather than array['0']. Doing the following will prove this:

 

$info = mysql_fetch_array( $data );

Print "This is the error point and your missing value ".$info['0'];

Print "<b>Products:</b></br> ";

while($info = mysql_fetch_array( $data )){ 
Print "" .$info[0]. "</br> ";
}

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.