jakebur01 Posted February 27, 2009 Share Posted February 27, 2009 How can I insert data only into specific columns and have it null whatever other fields are on the row? Please see the else statement below which is incorrect but I am working on it. <?php $sql = "SELECT * FROM {$tablename} WHERE ($part LIKE '%-1' OR $part LIKE '%-2' OR $part LIKE '%-3' OR $part LIKE '%-0')"; $result = mysql_query($sql) OR DIE(mysql_error()); while ($row = mysql_fetch_assoc($result)) { $itemNum = substr($row[$part], 0, -2); $sql2 = "SELECT * FROM {$tablename} WHERE $part = $itemNum"; $result2 = mysql_query($sql2) OR DIE(mysql_error()); $num_rows = mysql_num_rows($result2); if($num_rows>0) { $update = "UPDATE {$tablename} SET $list = '{$row[$list]}', $dealer = '{$row[$dealer]}', $distributor = '{$row[$distributor]}', $sort = '{$row[$sort]}', $stdpack = '{$row[$stdpack]}' WHERE $part LIKE '{$itemNum}%'"; mysql_query($update) or die(mysql_error()); } else { $insert = "INSERT INTO {$tablename} VALUES $part = $itemnum, $list = '{$row[$list]}', $dealer = '{$row[$dealer]}', $distributor = '{$row[$distributor]}', $sort = '{$row[$sort]}', $stdpack = '{$row[$stdpack]}'"; mysql_query($update) or die(mysql_error()); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/147104-solved-insert-into-specified-columns-and-null-the-rest/ Share on other sites More sharing options...
dezkit Posted February 27, 2009 Share Posted February 27, 2009 , $otherfield1 = '', $otherfield2 ='' Quote Link to comment https://forums.phpfreaks.com/topic/147104-solved-insert-into-specified-columns-and-null-the-rest/#findComment-772304 Share on other sites More sharing options...
jakebur01 Posted February 27, 2009 Author Share Posted February 27, 2009 Yea, but I don't know what those other fields are. There could be 20 other columns. But I am trying insert into those few and put NULL into whatever else is out there. Quote Link to comment https://forums.phpfreaks.com/topic/147104-solved-insert-into-specified-columns-and-null-the-rest/#findComment-772309 Share on other sites More sharing options...
haku Posted February 27, 2009 Share Posted February 27, 2009 A well designed database shouldn't need any NULLs in it, or at least very few. What exactly is it you are trying to do? Some database re-structuring may be a good idea. Quote Link to comment https://forums.phpfreaks.com/topic/147104-solved-insert-into-specified-columns-and-null-the-rest/#findComment-772395 Share on other sites More sharing options...
jakebur01 Posted February 27, 2009 Author Share Posted February 27, 2009 The table was created based on the number of columns the text file had in it and they were generated based on that flat file. ie. col1, col2, col3, col4, etc. On page 2 I select which columns contain which like list, dealer, distributor and so on. So in this line: INSERT INTO {$tablename} VALUES $part = $itemnum, $list = '{$row[$list]}', $dealer = '{$row[$dealer]}', $distributor = '{$row[$distributor]}', $sort = '{$row[$sort]}', $stdpack = '{$row[$stdpack]}' I am wanting to insert the list, dealer, distributor, sort, and stdpack values into the appropriate columns. How can I get this done and still take care of the columns I don't know about that are in the table? Quote Link to comment https://forums.phpfreaks.com/topic/147104-solved-insert-into-specified-columns-and-null-the-rest/#findComment-772401 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.