Jump to content

php code insert ino product ?? changes al products ??


lill77

Recommended Posts

Hello when i insert a new product ino my database it changes all exsisitng products to the same as the new one ??? Ive llloked at  the code i have and cant see where the problem is ??  :-[

 

<?php
include 'db.php';
// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

// select database
mysql_select_db($db) or die ("Unable to select database!");

$sql = "INSERT INTO `products` (`category_id` , `title` , `size` , `description` , `image` , `imageenlarge`)
VALUES ( '', '', 'size', '' , 'noimage.png', 'noimage_big.png');";

$reult = mysql_query($sql) or die (mysql_error());
$product_id = mysql_insert_id();

// close connection
mysql_close($connection);

?>

<META HTTP-EQUIV=Refresh CONTENT='0; URL=index.php?page=edit-product.php&product=<? echo $product_id; ?>'>
<p align="center" class="smalltext style2 style1">Saving Changes</p>

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<? include 'db.php' ?>
<script language="javascript" type="text/javascript"><!--
function popupWindow(url, width, height) {
if(width == null){	width =450; }	
if(height == null){height =150;}

  window.open(url,'popupWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width='+width+',height='+height+',screenX=150,screenY=150,top=150,left=100')
}
//--></script>
<script>
function log_out()
{
return confirm('Are you sure you want to Delete this article?');
}
</script>

<?php

// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

// select database
mysql_select_db($db) or die ("Unable to select database!");




// create query
$query = "SELECT * FROM `products` WHERE `ID` = '".$_REQUEST['product']."'";

// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
$row = mysql_fetch_row($result);
$CurrentProduct = $row[0];

// free result set memory
mysql_free_result($result);

// close connection
mysql_close($connection);
?>
<body>
<form method="GET" action="update-product.php">
<div align="center">
<table width="505" border="0" cellpadding="2" cellspacing="2" bordercolor="#FFFFFF" bordercolorlight="#C0C0C0" bordercolordark="#C0C0C0" id="table1">
	<tr>
		<td width="130" align="right"><b>
		<font face="Arial" size="2" color="#808080">Product ID:</font></b></td>
		<td width="228" align="center">
		  <p align="left">
		  	<? echo "$row[0]" ?>
		    <input name="ID" type="hidden" id="ID" value="<? echo "$row[0]" ?>" />
		  </p>
	  </td>
		<td width="127" align="right"> </td>
	</tr>
	<tr>
		<td width="130" align="right"><b>
		<font face="Arial" size="2" color="#808080">Title:</font></b></td>
		<td align="center">
		  <div align="left">
		    <input type="text" name="title" size="36" value="<? echo "$row[2]" ?>" ?>
        </div></td>
		<td width="127" align="right"> </td>
        </tr>
	<tr>
		<td width="130"><div align="right"><b><font face="Arial" size="2" color="#808080">Sizes </font></b></div></td>
		<td align="center"><div align="left">
		  <input type="text" name="size" size="36" value="<? echo "$row[3]" ?>" ? /><br />
		  (Format: <b>Small/Medium/Large</b>)
		</div></td>
		<td width="127"> </td>
	</tr>
	<tr>
		<td width="130" align="right"><b><font face="Arial" size="2" color="#808080">Description:</font></b></td>
	  <td align="center"><div align="left">
	    <textarea name="description" cols="50" rows="10" id="description"><? echo "$row[4]" ?></textarea>
      </div></td>
		<td width="127" align="right"> </td>
    </tr>
	<tr>
		<td width="130" align="right"><b><font face="Arial" size="2" color="#808080">Category:</font></b></td>
		<td align="center"><div align="left">
		  <p>			    		      </p>
		  <p><select name="category_id" id="category_id">
      <?php
$currentcat = $row[1];

// set database server access variables:

// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

// select database
mysql_select_db($db) or die ("Unable to select database!");

// create query
$query = "SELECT * FROM `category`;";

// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

// see if any rows were returned
if (mysql_num_rows($result) > 0) {
    // yes
    // print them one after another
        

    
    while($row = mysql_fetch_row($result)) {
        echo "<option value=\"".$row[0]."\"";
	if ($row[0]==$currentcat) {echo " Selected";};
	echo ">$row[1]</option>";
        
    }

}


// free result set memory
mysql_free_result($result);

// close connection
mysql_close($connection);

?>
    </select>   </p>
		</div></td>
		<td width="127" align="right"> </td>
    </tr>
	<tr>
		<td width="130" align="right"><b><font face="Arial" size="2" color="#808080">Image:</font></b></td>
	    <td align="center"><div align="left">
	      <select name="image" id="image">
	        <option value="noimage.png">No Image</option>
            <?php
if ($handle = opendir('../productimages/')) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
	$page_name = $file;
            //$page_name=substr($file, 0, strpos($file, "."));
            echo "<option";
		if($page_name==$row[5]){echo ' selected';};
		echo ">$page_name</option>";
        }
    }
}
closedir($handle);
?>
              </select>
            </div></td>

	    <td width="127" align="right"> </td>
    </tr>
	<tr>
	  <td align="right"><b><font face="Arial" size="2" color="#808080">Enlarge Image:</font></b></td>
	  <td align="center"><div align="left">
	    <select name="imageenlarge" id="imageenlarge">
	      <option value="noimage.png">No Image</option>
	      <?php
if ($handle = opendir('../productimages/')) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
	$page_name = $file;
            //$page_name=substr($file, 0, strpos($file, "."));
            echo "<option";
		if($page_name==$row[6]){echo ' selected';};
		echo ">$page_name</option>";
        }
    }
}
closedir($handle);
?>
        </select>
      </div></td>
	  <td align="right"> </td>
  </tr>
	<tr>
		<td width="130" align="right"> </td>
    <td align="center"><div align="left"></div></td>
		<td width="127" align="right"> </td>
    </tr>
	<tr>
		<td width="130"> </td>
		<td align="center"><div align="left"></div></td>
		<td width="127" align="right"> </td>
    </tr>
	<tr>
		<td width="130" align="right"> </td>
	  <td align="center"> </td>
		<td width="127" align="right"> </td>
    </tr>
	<tr>
		<td width="130" align="right"> </td>
	  <td align="center"> </td>
		<td width="127" align="right"> </td>
    </tr>
	<tr>
	  <td width="130"> </td>
		<td align="center"> </td>
		<td width="127" align="right"> </td>
    </tr>
	<tr>
	  <td> </td>
	  <td align="center"> </td>
	  <td> </td>
  </tr>
  </table>
</div>
<p align="center"><input type="submit" value="Save" name="B1">
  <input type="reset" value="Close" name="B2" onclick="history.back()">
</p>
</form>


 </p>
<p align="center"> </p>
</body>
</html>

From the second code example you have given it looks like the code is referring to a script for updating an entry rather than adding an entry or product,

 

<form method="GET" action="update-product.php">

 

If you're calling this by mistake then you'll be updating all of your products in your db to the new product you want to enter rather than inserting.

you should have a new-product.php script that collects all of your $_GET variables such as $_GET["Title"] then passes them to the query below. You should then replace action="update-product.php" with action="new-product.php".

 

$sql = "INSERT INTO `products` (`category_id` , `title` , `size` , `description` , `image` , `imageenlarge`)
VALUES ( '', $title, $size, $description , 'noimage.png', 'noimage_big.png');";

Hi again.. bit lost now. I have new product pages as below

<?php
include 'db.php';
// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

// select database
mysql_select_db($db) or die ("Unable to select database!");
$sql = "INSERT INTO `products` (`category_id` , `title` , `size` , `description` , `image` , `imageenlarge`)
VALUES ( '', '$title', '$size', '$description' , '', '');";

$sql_query = mysql_query($sql) or die (mysql_error()); 

$sql = "SELECT LAST_INSERT_ID()"; 
$result = mysql_query($sql);
$row = mysql_fetch_row($result);
$product_id = $row[0];

// free result set memory
mysql_free_result($result);

// close connection
mysql_close($connection);

?>

<META HTTP-EQUIV=Refresh CONTENT='0; URL=index.php?page=edit-product.php&product=<? echo $product_id; ?>'>
<p align="center" class="smalltext style2 style1">Saving Changes</p>

 

and also the edit page, then as below the update product

 

<? include 'db.php' ?>

<?

// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

// select database
mysql_select_db($db) or die ("Unable to select database!");
$sql_query = mysql_query("UPDATE `products` SET `title` = '".$_REQUEST['title']."',
`size` = '".$_REQUEST['size']."',
`description` = '".$_REQUEST['description']."',
`category_id` = ".$_REQUEST['category_id'].",
`Image` = '".$_REQUEST['image']."',
`Imageenlarge` = '".$_REQUEST['imageenlarge']."'") or die (mysql_error());


// close connection
mysql_close($connection);
?>
<META HTTP-EQUIV=Refresh CONTENT="0; URL=index.php?page=products.php">

Hello

 

I have a problem that updates every product in my database when i add a new 1 to the the new product details.

 

Can anyone help? Thanks Code is below

 

New product page

<?php
include 'db.php';
// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

// select database
mysql_select_db($db) or die ("Unable to select database!");

$sql = "INSERT INTO `products` (`category_id` , `title` , `size` , `description` , `image` , `imageenlarge`)
VALUES ( '', '$title', '$size', '$description' , '', '');";

$sql_query = mysql_query($sql) or die (mysql_error()); 

$sql = "SELECT LAST_INSERT_ID()"; 
$result = mysql_query($sql);
$row = mysql_fetch_row($result);
$product_id = $row[0];

// free result set memory
mysql_free_result($result);

// close connection
mysql_close($connection);

?>

<META HTTP-EQUIV=Refresh CONTENT='0; URL=index.php?page=edit-product.php&product=<? echo $product_id; ?>'>
<p align="center" class="smalltext style2 style1">Saving Changes</p>

 

Update Product

<? include 'db.php' ?>

<?

// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

// select database
mysql_select_db($db) or die ("Unable to select database!");
$sql_query = mysql_query("UPDATE `products` SET `title` = '".$_REQUEST['title']."',
`size` = '".$_REQUEST['size']."',
`description` = '".$_REQUEST['description']."',
`category_id` = ".$_REQUEST['category_id'].",
`Image` = '".$_REQUEST['image']."',
`Imageenlarge` = '".$_REQUEST['imageenlarge']."'") or die (mysql_error());


// close connection
mysql_close($connection);
?>
<META HTTP-EQUIV=Refresh CONTENT="0; URL=index.php?page=products.php">

 

Edit page

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
<title>Untitled Document</title>
</head>
<? include 'db.php' ?>
<script language="javascript" type="text/javascript"><!--
function popupWindow(url, width, height) {
if(width == null){	width =450; }	
if(height == null){height =150;}

  window.open(url,'popupWindow','toolbar=no,location=no,directories=no,status=no,menubar=no,scrollbars=yes,resizable=yes,copyhistory=no,width='+width+',height='+height+',screenX=150,screenY=150,top=150,left=100')
}
//--></script>
<script>
function log_out()
{
return confirm('Are you sure you want to Delete this article?');
}
</script>

<?php

// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

// select database
mysql_select_db($db) or die ("Unable to select database!");




// create query
$query = "SELECT * FROM `products` WHERE `ID` = '".$_REQUEST['product']."'";

// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());
$row = mysql_fetch_row($result);
$CurrentProduct = $row[0];

// free result set memory
mysql_free_result($result);

// close connection
mysql_close($connection);
?>
<body>
<form method="GET" action="update-product.php">
<div align="center">
<table width="505" border="0" cellpadding="2" cellspacing="2" bordercolor="#FFFFFF" bordercolorlight="#C0C0C0" bordercolordark="#C0C0C0" id="table1">
	<tr>
		<td width="130" align="right"><b>
		<font face="Arial" size="2" color="#808080">Product ID:</font></b></td>
		<td width="228" align="center">
		  <p align="left">
		  	<? echo "$row[0]" ?>
		    <input name="ID" type="hidden" id="ID" value="<? echo "$row[0]" ?>" />
		  </p>
	  </td>
		<td width="127" align="right"> </td>
	</tr>
	<tr>
		<td width="130" align="right"><b>
		<font face="Arial" size="2" color="#808080">Title:</font></b></td>
		<td align="center">
		  <div align="left">
		    <input type="text" name="title" size="36" value="<? echo "$row[2]" ?>" ?>
        </div></td>
		<td width="127" align="right"> </td>
        </tr>
	<tr>
		<td width="130"><div align="right"><b><font face="Arial" size="2" color="#808080">Sizes </font></b></div></td>
		<td align="center"><div align="left">
		  <input type="text" name="size" size="36" value="<? echo "$row[3]" ?>" ? /><br />
		  (Format: <b>Small/Medium/Large</b>)
		</div></td>
		<td width="127"> </td>
	</tr>
	<tr>
		<td width="130" align="right"><b><font face="Arial" size="2" color="#808080">Description:</font></b></td>
	  <td align="center"><div align="left">
	    <textarea name="description" cols="50" rows="10" id="description"><? echo "$row[4]" ?></textarea>
      </div></td>
		<td width="127" align="right"> </td>
    </tr>
	<tr>
		<td width="130" align="right"><b><font face="Arial" size="2" color="#808080">Category:</font></b></td>
		<td align="center"><div align="left">
		  <p>			    		      </p>
		  <p><select name="category_id" id="category_id">
      <?php
$currentcat = $row[1];

// set database server access variables:

// open connection
$connection = mysql_connect($host, $user, $pass) or die ("Unable to connect!");

// select database
mysql_select_db($db) or die ("Unable to select database!");

// create query
$query = "SELECT * FROM `category`;";

// execute query
$result = mysql_query($query) or die ("Error in query: $query. ".mysql_error());

// see if any rows were returned
if (mysql_num_rows($result) > 0) {
    // yes
    // print them one after another
        

    
    while($row = mysql_fetch_row($result)) {
        echo "<option value=\"".$row[0]."\"";
	if ($row[0]==$currentcat) {echo " Selected";};
	echo ">$row[1]</option>";
        
    }

}


// free result set memory
mysql_free_result($result);

// close connection
mysql_close($connection);

?>
    </select>   </p>
		</div></td>
		<td width="127" align="right"> </td>
    </tr>
	<tr>
		<td width="130" align="right"><b><font face="Arial" size="2" color="#808080">Image:</font></b></td>
	    <td align="center"><div align="left">
	      <select name="image" id="image">
	        <option value="noimage.png">No Image</option>
            <?php
if ($handle = opendir('../productimages/')) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
	$page_name = $file;
            //$page_name=substr($file, 0, strpos($file, "."));
            echo "<option";
		if($page_name==$row[5]){echo ' selected';};
		echo ">$page_name</option>";
        }
    }
}
closedir($handle);
?>
              </select>
            </div></td>

	    <td width="127" align="right"> </td>
    </tr>
	<tr>
	  <td align="right"><b><font face="Arial" size="2" color="#808080">Enlarge Image:</font></b></td>
	  <td align="center"><div align="left">
	    <select name="imageenlarge" id="imageenlarge">
	      <option value="noimage.png">No Image</option>
	      <?php
if ($handle = opendir('../productimages/')) {
    while (false !== ($file = readdir($handle))) {
        if ($file != "." && $file != "..") {
	$page_name = $file;
            //$page_name=substr($file, 0, strpos($file, "."));
            echo "<option";
		if($page_name==$row[6]){echo ' selected';};
		echo ">$page_name</option>";
        }
    }
}
closedir($handle);
?>
        </select>
      </div></td>
	  <td align="right"> </td>
  </tr>
	<tr>
		<td width="130" align="right"> </td>
    <td align="center"><div align="left"></div></td>
		<td width="127" align="right"> </td>
    </tr>
	<tr>
		<td width="130"> </td>
		<td align="center"><div align="left"></div></td>
		<td width="127" align="right"> </td>
    </tr>
	<tr>
		<td width="130" align="right"> </td>
	  <td align="center"> </td>
		<td width="127" align="right"> </td>
    </tr>
	<tr>
		<td width="130" align="right"> </td>
	  <td align="center"> </td>
		<td width="127" align="right"> </td>
    </tr>
	<tr>
	  <td width="130"> </td>
		<td align="center"> </td>
		<td width="127" align="right"> </td>
    </tr>
	<tr>
	  <td> </td>
	  <td align="center"> </td>
	  <td> </td>
  </tr>
  </table>
</div>
<p align="center"><input type="submit" value="Save" name="B1">
  <input type="reset" value="Close" name="B2" onclick="history.back()">
</p>
</form>


 </p>
<p align="center"> </p>
</body>
</html>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.