Jump to content

Recommended Posts

Can someone please help me figure out how to update multiple values all at one time? I have a database that holds the following values: item, part_number, price. I want to be able to update the part_numbers and prices all at once but I cannot find anything anywhere that tells me how to accomplish this. At first I stumbled across this script that seemed promissing:

 

<?php

$dbServer = \"myserver

$dbUser = \"user\";

$dbPass = \"password\";

$dbName = \"mydb\";

 

function ConnectToDb($server, $user, $pass, $database)

{

$s = @mysql_connect($server, $user, $pass);

$d = @mysql_select_db($database, $s);

 

if(!$s || !$d)

return false;

else

return true;

}

$cxn = @ConnectToDb($dbServer, $dbUser, $dbPass, $dbName);

 

$part_numbers=array(\"3930222\",\"493097\");

$prices=array(\"383.00\",\"493.89\");

 

$TheCount=count($prices);

 

for($a=0; $a < $TheCount; $a++)

{

$SQL= \"UPDATE xc90 SET price=\'$prices[$a]\' WHERE part_number=\'$part_numbers[$a]\'\";

}

if ( mysql_query($SQL) )

{

echo(\"<P>Prices successfully updated!</P>\");}

else

{

echo(\"<P>Error updating prices: \" . mysql_error() . \"</P>\");

}

?>

 

This works partially because it only updates the very last value to the right in both arrays and ignores all those before it.

 

I was also wondering if the is there a way to use the LOAD DATA IN FILE command to accomplish this task? I have a .txt file with the part_numbers and prices, but when I try to use the command LOAD DATA INFILE \"xc90.txt\" INTO TABLE xc90 (part_number,price) it appends the data as opposed to overwrite all records from the beggining with the new ones.

 

If someone could please help me I\'d be very appreciative. Thanks in advance for any help.

Link to comment
https://forums.phpfreaks.com/topic/666-update-multiple-records/
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.