Jump to content

Having trouble with insert and update


tobitimac

Recommended Posts

I want this code below to simply update the current data in MySQL if changes were made to the excel sheet, else if there is a new row added to the excel sheet i want it to add the new row into MySQL.

 

all i want is simply update or insert. Please can someone help me out..

 


if (($handle = fopen('inventorylist.csv', "r")) !== FALSE) {

    while (($data = fgetcsv($handle, 900000, ",")) !== FALSE) {
    	var_dump($data);
        $num = count($data);
	$result = mysql_query("SELECT * FROM inventory WHERE itemNumber='$data[0]' AND itemDesc='$data[1]'AND quantityHand='$data[2]' AND category='$data[3]' AND Whse='$data[4]'");
	if ($result) {
		$recordCount = mysql_num_rows($result);
		//echo "$recordCount Rows\n";
		if ($recordCount > 0) {
			// rest of your current code loop here.
			$sql1 = "UPDATE inventory SET itemNumber='$data[0]',itemDesc='$data[1]',quantityHand='$data[2]',category='$data[3]',Whse='$data[4]' WHERE itemNumber='$data[0]' AND itemDesc='$data[1]'AND quantityHand='$data[2]' AND category='$data[3]'AND Whse='$data[4]'";
			mysql_query($sql1) or die(mysql_error());
		} else {
			// your current code.
			$sql="INSERT into inventory(itemNumber,itemDesc,quantityHand,category,Whse) values ('$data[0]','$data[1]','$data[2]','$data[3]','$data[4]')";
			mysql_query($sql) or die(mysql_error());
		}
	} else {
		echo mysql_error();
	}
    }
    fclose($handle);
}
?>

 

 

Link to comment
Share on other sites

You need to determine something that makes a row unique. How do you know if a row is changed or if it was added? Your select statement is looking for a row that matches ALL of the CURRENT values (which might have changed). So, if someone changes the description for item number 4, your select will NOT find the row and will insert it.

 

If you choose something that is unique -- I would guess the itemNumber -- then select JUST on the item number, you can do the INSERT or UPDATE based on whether you find it or not.

Link to comment
Share on other sites

You need to determine something that makes a row unique. How do you know if a row is changed or if it was added? Your select statement is looking for a row that matches ALL of the CURRENT values (which might have changed). So, if someone changes the description for item number 4, your select will NOT find the row and will insert it.

 

If you choose something that is unique -- I would guess the itemNumber -- then select JUST on the item number, you can do the INSERT or UPDATE based on whether you find it or not.

 

i have changed my select statement but still nothing is updated or inserted..

	$result = mysql_query("SELECT * FROM inventory WHERE itemNumber='$data[0]'");

 

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.