Jump to content

[SOLVED] Updating records


Donovan

Recommended Posts

I'm trying to update these records to cleanse them from a scantron I imported to a table.

 

Here is a sample of the data.

1,000388419  ,"AMPONSAH, EUGEN  ",10,100,75,10,100

1,000553939  ,"ANKROM, SEAN T",8,80,14,8,80

1,000034194  ,"BALCIK, BRENDE  ",9,90,35,9,90

1,000065368  ,"BARNES, NATHAN  ",10,100,75,10,100

1,000345478  ,"BARTON, LUCAS  ",10,100,75,10,100

1,000114715  ,"BAXTER, COREY  ",9,90,35,9,90

1,xxxx75481  ,"LEE, LANCE  ",9,90,35,9,90

1,000546179  ,"LEVY, JESS D",8,80,14,8,80

 

I needed to remove any leading x from the StudentID which I did with this.

$replacexuid = $db->sql_query("UPDATE ".$prefix."$table_name SET StudentID = REPLACE(StudentID,'x','0')");

 

Then I need to strip off the first character in the 9 character field and replace with a U. (For example U00388419)

 

$i = "U";
$alteruid = $db->sql_query("SELECT StudentID FROM ".$prefix."$table_name");
while ($row = $db->sql_fetchrow($alteruid)) { 
$clean_UID = substr($row['StudentID'], -;
   $new_UID = $i.$clean_UID;
$db->sql_query("UPDATE ".$prefix."$table_name SET StudentID = '$new_UID'");
  }

 

The code is currently updating the very last record out of 104 from the query and updating all student grades with that same UID.  This is incorrect as each student has their own StudentID.

 

I am missing something that seems pretty easy.

 

I dumped what the query was interpreting by doing this.

 

while ($row = $db->sql_fetchrow($alteruid)) {
   $clean_UID = substr($row['StudentID'], -;
   $new_UID = $i.$clean_UID;
   $sql = "UPDATE ".$prefix."$table_name SET StudentID = '$new_UID'";
   die('<br />$sql = '.$sql.'<br />');
   $db->sql_query($sql);
}

 

The result was this

 

$sql = UPDATE atlas_tl_session_grade_import SET StudentID = 'U00077193' 

 

Which is the first record in the imported table.

 

But at the end of the while loop it actually takes the last record read and then updates all records with 'U00000110'

 

I think I need a foreach() but I don't know how to implement it.

 

I need all records updated regardless so I don't think I need a WHERE clause.

Link to comment
Share on other sites

The table name is defined in the beginning of the code.

 

$table_name = "_tl_session_grade_import";

 

The $prefix is global and it is defined as atlas.

 

The following displays how many records are imported.

 

$import_total = $db->sql_numrows($db->sql_query("SELECT * FROM ".$prefix."$table_name"));
Opentable();
echo" $import_total records were imported \n";

 

I am importing 104 records from a scantron file which is a csv file.

 

 

Link to comment
Share on other sites

I need all rows updated... regardless.  The field I am updating is the only field where I could place a WHERE clause but it doesn't match any other value I have in other tables.

 

The UID in my students table all are 9 characters starting with a U

 

The StudentID in the imported table are 8 character and need a U appended to the beginning.

 

Could I somehow change this and instead load an array with all StudentID from the imported table and just loop thru it and add a U.

 

$numgrades = $db->sql_query("SELECT StudentID FROM ".$prefix."$table_name");
$grade_total = $db->sql_numrows($numgrades);
for ($i=0; $i <= $grade_total; $i++) {
Code for adding U
}

 

 

 

 

 

Link to comment
Share on other sites

I tried the following:

 

$i = "U";
$alteruid = $db->sql_query("SELECT StudentID FROM ".$prefix."$table_name");
while ($row = $db->sql_fetchrow($alteruid)) { 
$clean_UID = substr($row['StudentID'], -;
   $new_UID = $i.$clean_UID;
$db->sql_query("UPDATE ".$prefix."$table_name SET StudentID = '$new_UID' WHERE StudentID != 0");
  }

 

And it updated all rows with the StudentID of the "first" record read.

Link to comment
Share on other sites

Replace all that php with a query that does this -

 

UPDATE $prefix$table_name SET StudentID = INSERT(StudentID, 1, 1, 'U')

 

 

I think you are a life saver.

 

This

 

$replacexuid = $db->sql_query("UPDATE ".$prefix."$table_name SET StudentID = REPLACE(StudentID,'x','0')");

 

along with your code

 

$db->sql_query("UPDATE ".$prefix."$table_name SET StudentID = INSERT(StudentID, 1, 1, 'U')");

 

I think has done the trick.

 

Can you explain this method?  It is not INSERT INTO, nor is it INSERT VALUES.

 

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.